#C12675. Arithmetic Expression Evaluator
Arithmetic Expression Evaluator
Arithmetic Expression Evaluator
You are given an arithmetic expression as a string. Your task is to evaluate the expression following the standard order of operations, namely PEMDAS/BODMAS.
The expression may include non-negative integers, the operators +
, -
, *
, /
, and parentheses ( )
. You must ensure that the expression contains only valid characters. Any expression containing invalid characters should result in the output Invalid characters in the expression
.
If the expression attempts to divide by zero, output Division by zero error
. Similarly, if the expression has a syntax error (for example, missing operands or misplaced operators), output Syntax error in the expression
.
For operator precedence, note that the evaluation follows the rules: $$ \text{Expression} = \text{Term} \;\pm\; \text{Term} $$ $$ \text{Term} = \text{Factor} \;\times\/\; \text{Factor} $$ $$ \text{Factor} = \text{Number} \;\text{or}\; (\text{Expression}) $$
Implement your solution so that it reads the input from standard input (stdin) and writes the result to standard output (stdout).
inputFormat
The input consists of a single line containing the arithmetic expression string. The string may include digits, spaces, and the following characters: +
, -
, *
, /
, (
, )
.
outputFormat
Output a single line which is the result of the evaluated expression. If an error is encountered, output one of the following messages exactly:
Division by zero error
Invalid characters in the expression
Syntax error in the expression
2 + 3
5