#B4278. Evaluate Unparenthesized Mixed Arithmetic Expression
Evaluate Unparenthesized Mixed Arithmetic Expression
Evaluate Unparenthesized Mixed Arithmetic Expression
Given an arithmetic expression consisting of integers and the four basic operators (+, -, *, /) without any parentheses, compute the result following these rules:
- If the expression contains both multiplication/division and addition/subtraction, evaluate all multiplication and division operations first.
- For operations at the same level, evaluate them from left to right.
- All division operations discard the fractional part (i.e. perform integer division by truncating the result).
For example, consider the expression: 2 + 3*4 - 10/6 + 1/2*4
Step 1: Evaluate multiplication and division:
- \(3*4 = 12\)
- \(10/6 = 1\) (since 10/6=1.666..., which truncates to 1)
- \(1/2*4\) is evaluated as \((1/2)=0\) then \(0*4=0\)
Step 2: Evaluate addition and subtraction: \(2 + 12 - 1 + 0 = 13\).
The output is 13.
inputFormat
The input consists of a single line containing the arithmetic expression. The expression may contain extra spaces.
It is guaranteed that the expression is a valid mixed arithmetic expression without parentheses and only contains non-negative integers and the operators '+', '-', '*', '/'.
outputFormat
Output a single integer which is the result of evaluating the expression following the rules described above.
sample
2 + 3*4 - 10/6 + 1/2*4
13