#C2780. Evaluate Mathematical Expressions
Evaluate Mathematical Expressions
Evaluate Mathematical Expressions
You are given a set of mathematical expressions. Each expression consists of two integer operands and an operator separated by spaces. The valid operators are +, -, *, and /. Your task is to evaluate each expression and output the result. The division operator performs floating‐point division. If the expression is invalid, you must return a specific error message.
The evaluation rules are as follows:
- If the expression has exactly three components (operand operator operand), evaluate it.
- If the operator is
+
,-
,*
, or/
, perform the respective operation. - If the operator is
/
and division by zero is attempted, outputError: Division by zero
. - If the operator is not one of the valid ones, output
Error: Invalid operator 'op'
(where op is the provided operator). - If the syntax is not exactly in the required format, output
Error: Invalid syntax
.
All numerical results (even when whole numbers) should be printed as floats if computed via division. You should read input from stdin
and write output to stdout
.
inputFormat
The first line contains an integer T (T ≥ 1), representing the number of expressions. Each of the following T lines contains a mathematical expression in the format: "operand operator operand" with a single space between tokens.
outputFormat
For each expression, output a single line with the evaluated result or the corresponding error message.## sample
1
3 + 5
8.0
</p>