Problem 396
Basic Calculator II
Medium · 20 pts · Math, String, Stack
Problem
Evaluate an expression that contains non-negative integers, the operators '+', '-', '*' and '/', and spaces, but no parentheses. Multiplication and division are done before addition and subtraction, and operators of the same priority are applied from left to right. Integer division truncates toward zero, so 7 / 2 = 3. Do not use a built-in eval function; a stack of terms (or a running last term) handles the priorities.
Input
A single line containing the expression.
Output
Print the value of the expression.
Constraints
1 <= length <= 300000; every intermediate value fits in a 32-bit signed integer; no division by zero
Example input
3+2*2
Example output
7
Explanation
In '3+2*2' the multiplication goes first: 2 * 2 = 4, and then 3 + 4 = 7.