#C12231. Arithmetic Expression Evaluator
Arithmetic Expression Evaluator
Arithmetic Expression Evaluator
You are given an arithmetic expression as a string which contains positive integers and the operators \(+, -, \times, \div\). The expression may have any number of spaces. Your task is to evaluate the expression by respecting the usual operator precedence: multiplication and division come before addition and subtraction.
In addition, if the expression is empty or contains any character other than digits, spaces, or the symbols +
, -
, *
, and /
, you must output an error message. Specifically, output "Error: Empty expression" if the given string is empty (or consists solely of spaces); output "Error: Invalid characters in expression" if invalid characters are present; and if the expression cannot be evaluated because of an invalid format (e.g. incomplete expression), output "Error: Invalid expression".
Note: All formulas should be written in \(\LaTeX\) format. For example, the operator precedence can be described as \(\times,\div > +,-\).
inputFormat
A single line from standard input (stdin) containing an arithmetic expression composed of positive integers and the operators (+, -, \times, \div). The expression may include spaces.
outputFormat
Print the result of evaluating the arithmetic expression to standard output (stdout). If the expression is empty, print "Error: Empty expression"; if the expression contains invalid characters, print "Error: Invalid characters in expression"; if the expression is improperly formed, print "Error: Invalid expression". If the result is an integer, print it without a decimal point; otherwise print the floating-point result.## sample
2 + 3
5