#C12481. Evaluating Mathematical Expression
Evaluating Mathematical Expression
Evaluating Mathematical Expression
Given a mathematical expression as a string, compute its numerical result. The expression may include non‐negative numbers (integers or decimals), operators +
, -
, *
, /
and parentheses ( )
. The operators obey the standard precedence rules: multiplication and division are performed before addition and subtraction. For example, the expression \(2 + 3 * 4\) evaluates to \(14\), while \((2 + 3) * 4\) evaluates to \(20\). The input may contain arbitrary whitespace which should be ignored.
If the provided expression is invalid (for example, empty or with misplaced operators/parentheses), your program should output Invalid expression
.
inputFormat
The input is given from standard input (stdin) as a single line containing the mathematical expression. The expression will only contain non-negative numbers, the operators +
, -
, *
, /
and parentheses.
outputFormat
Output the result of evaluating the mathematical expression on standard output (stdout). The result should be printed as a floating-point number. In case of an invalid expression, output exactly Invalid expression
.
2 + 3
5.0