#C14229. Evaluate Mathematical Expression
Evaluate Mathematical Expression
Evaluate Mathematical Expression
You are given a string representing a mathematical expression. Your task is to evaluate the expression using the correct order of operations (i.e. PEMDAS, which stands for Parentheses, Exponents, Multiplication/Division, Addition/Subtraction). The expression may contain integers, decimal numbers, and the operators +
, -
, *
, /
as well as parentheses ( )
.
If the expression is empty, output The expression is empty.
. If the expression contains any character other than digits, spaces, + - * / ( ) .
then output The expression contains invalid characters.
.
In case of a syntax error (such as an incomplete expression) or a division by zero, output the corresponding error message (invalid syntax
or division by zero
respectively).
Note: When reporting errors, the output should exactly match the prescribed error messages.
Input: A single line from standard input containing the expression string.
Output: The evaluation result of the expression or the appropriate error message on standard output.
inputFormat
The input consists of one line which is the mathematical expression string. It may contain spaces. For example: 2 + 3
.
outputFormat
The output is a single line that contains either the numerical result of the expression or an error message if the expression is empty, contains invalid characters, or has a syntax/division error.
## sample2 + 3
5