#C7548. Arithmetic Expression Evaluator
Arithmetic Expression Evaluator
Arithmetic Expression Evaluator
You are given a mathematical expression as a string. The expression consists of non-negative integers, the four basic arithmetic operations +, -, *, /, and parentheses ( ). Your task is to evaluate the expression following the standard operator precedence and using integer division for the '/' operator (i.e. flooring the result).
The evaluation should respect the following rules:
- Parentheses have the highest precedence.
- The multiplication and division operators have higher precedence than addition and subtraction.
- When operators have the same precedence, evaluate them from left to right.
For example, the expression 3 + 2 * 2
evaluates to 7
and 10 + (2 * 3)
evaluates to 16
.
Your program should read the input expression from standard input and write the result to standard output.
inputFormat
The input consists of a single line containing the arithmetic expression.
The expression includes non-negative integers, spaces, the operators +
, -
, *
, /
and parentheses (
and )
. You can assume the expression is valid.
outputFormat
Output a single integer which is the result of evaluating the arithmetic expression.
## sample3 + 2 * 2
7