#C9336. Evaluate Mathematical Expression
Evaluate Mathematical Expression
Evaluate Mathematical Expression
Given a string representing a mathematical expression, your task is to evaluate it correctly. The expression may contain non-negative integers, decimal numbers, operators +
, -
, *
, /
, and parentheses ( )
. The evaluation must follow the standard operator precedence rules, i.e. $$\text{PEMDAS}$$ (or BODMAS).
For example, the expression 3 + 5 * 2 / (7 - 3)
evaluates to 5.5. Note that spaces can appear arbitrarily in the input expression.
inputFormat
The input consists of a single line read from standard input (stdin
), which is the expression to evaluate.
Example:
2 + 3 * (4 - 1)
outputFormat
Output a single number to standard output (stdout
) representing the evaluated result of the expression. If the result is mathematically an integer, you may output it without any decimal point; otherwise, include the fractional part.
2 + 3
5