#C13909. Evaluate Arithmetic Expressions
Evaluate Arithmetic Expressions
Evaluate Arithmetic Expressions
You are given a list of arithmetic expressions. Each expression is a simple binary operation involving two integer operands and one of the four operators: +, -, *, or /. The expressions may contain extraneous spaces. Your task is to evaluate each expression and print the result on a new line.
For division, if the divisor is zero, output ERROR
. For any expression that does not strictly follow the format of operand1 operator operand2
(with exactly one operator among the four), output ERROR
. Note that division always produces a floating-point result even if it is a whole number (i.e. $8/4$ yields 2.0
).
Input Format:
- The first line contains an integer $T$, the number of expressions.
- The next $T$ lines each contain a single arithmetic expression.
Output Format:
- Output $T$ lines, where each line contains the evaluation result of the corresponding expression. For invalid expressions or division by zero, print
ERROR
.
inputFormat
The first line is an integer $T$ indicating the number of expressions. Each of the following $T$ lines contains one arithmetic expression which may include spaces.
outputFormat
Output $T$ lines, each corresponding to the result of evaluating the arithmetic expression from the input. For division, the result must be in floating-point format (e.g. 2.0
), and if an expression is invalid or attempts division by zero, output ERROR
.
7
2+3
4-5
6*7
8/4
10/0
2**3
5 +
5
-1
42
2.0
ERROR
ERROR
ERROR
</p>