#C14462. Evaluate Mathematical Expression
Evaluate Mathematical Expression
Evaluate Mathematical Expression
You are given a string expression
that represents a mathematical expression containing integer numbers, the binary operators +
, -
, *
, /
, and parentheses ( )
. Your task is to evaluate the expression following the correct order of operations, i.e. the PEMDAS rules (i.e. Parentheses, Exponents (not used here), Multiplication and Division, Addition and Subtraction). Note that division is performed in floating point and then truncated to an integer (i.e. using the operation \(int(\cdot)\)). If the expression is invalid, output Invalid mathematical expression
.
For example, given the expression 2 + 3 * (2 + 3) / 2 - 5
, the evaluation order is:
\[ 2 + \frac{3 \times (2+3)}{2} - 5 = 2 + \frac{15}{2} - 5 = 2 + 7.5 - 5 = 4.5 \quad\text{and after truncation, the result is } 4. \]
inputFormat
The input consists of a single line containing the mathematical expression as a string.
outputFormat
Output a single integer which is the result of the evaluated expression. For invalid expressions, output Invalid mathematical expression
.
3 + 5
8