#K84702. Evaluate Arithmetic Expression
Evaluate Arithmetic Expression
Evaluate Arithmetic Expression
You are given a string expression
representing an arithmetic expression. The expression contains non-negative integers and the binary operators +
, -
, *
, and /
. The expression does not include any spaces, parentheses, or any invalid characters, and it will not have leading or trailing operators.
Your task is to evaluate the expression and output the integer result. The expression should be evaluated according to standard operator precedence rules: multiplication and division have higher precedence than addition and subtraction. Note that for division, the quotient should be obtained by truncating toward zero.
Mathematically, if we denote the expression by \(E\), compute the result as \(\text{result} = E\), where for division \(a/b\), the result is \(\lfloor a/b \rfloor\) if \(a/b\) is positive and follows truncation toward zero in all cases.
For example:
- Input:
3+5*2
→ Output:13
- Input:
7-3/2
→ Output:6
- Input:
10+6/3-4*2
→ Output:4
inputFormat
The input consists of a single line containing a valid arithmetic expression without spaces.
For example:
3+5*2
outputFormat
Output a single integer which is the evaluation result of the expression.
For example:
13## sample
3+5*2
13
</p>