#K42687. Evaluate Mathematical Expression
Evaluate Mathematical Expression
Evaluate Mathematical Expression
Given a string s representing a valid mathematical expression, evaluate it and return its integer result. The expression contains non-negative integers, and the operators +
, -
, *
, and /
. The division operator should truncate toward zero. The expression may contain whitespace characters, which should be ignored.
For example, using the expression 3+2*2
, the result is 7, and for the expression 3-5/2
, the result is 1.
Note that the evaluation follows the standard operator precedence: multiplication and division have a higher precedence than addition and subtraction. Formally, the expression is evaluated as per the following rules:
\( \text{result} = \sum_{i} \text{term}_i \), where each term is computed by applying the operators in the correct order.
inputFormat
The input is a single line string representing the mathematical expression.
Input Format:
s
outputFormat
Output a single integer which is the result of evaluating the expression.
Output Format:
result## sample
3+2*2
7