#C9064. Arithmetic Expression Evaluator
Arithmetic Expression Evaluator
Arithmetic Expression Evaluator
You are given a list of simple arithmetic expressions in which each expression consists of positive integers and the operators +
, -
, *
, and /
. The expressions follow standard operator precedence rules: multiplication and division are computed before addition and subtraction. The division operator /
indicates integer division (i.e. floor division).
Your task is to evaluate each expression and output its result.
Note: Each expression is valid and does not contain any parentheses. The operators are left-associative.
The arithmetic evaluation can be formally described by the following steps:
1. Tokenize the expression into numbers and operators.
2. Compute all multiplications and divisions first (scanning from left to right):
\(a \times b\) or \(a \div b\).
3. Then, compute all additions and subtractions from left to right:
\(a + b\) or \(a - b\).
For example, given the expression 3+5*2
, you should first compute 5*2 = 10
, and then add 3
resulting in 13
.
inputFormat
The first line contains a single integer n (1 ≤ n ≤ 100), the number of arithmetic expressions. The next n lines each contain a non-empty string representing an arithmetic expression. Each expression contains only digits and the operators +
, -
, *
, and /
without any spaces.
It is guaranteed that the expressions are valid.
outputFormat
For each arithmetic expression, output its evaluated result on a separate line.
## sample1
1+2-3
0
</p>