#C12711. Evaluate Mathematical Expression
Evaluate Mathematical Expression
Evaluate Mathematical Expression
You are given a string representing a mathematical expression which may include non-negative floating point numbers, the binary operators +
, -
, *
, /
and parentheses ( )
. You are required to evaluate the expression following the normal operator precedence. In case a division by zero occurs, output None
. Note that an empty input should be interpreted as the value 0.0.
Formally, if the expression is denoted by E, you must compute its value. For example, using the standard operator precedence we have:
- $E = 3+5$ results in $8.0$
- $E = (2+3)\times4$ results in $20.0$
- $E = \frac{10}{0}$ results in
None
- $E = \frac{((2+3)\times(4-1))}{5}$ results in $3.0$
Your task is to write a program which reads the expression from standard input and writes the evaluated result to standard output.
inputFormat
The input is provided via standard input (stdin) and consists of a single line containing a mathematical expression. The expression may contain spaces which should be ignored.
outputFormat
Output the evaluated result to standard output (stdout). If the expression is valid and no division by zero occurs, print the result as a floating point number. Otherwise, if a division by zero is detected, print None
.
3 + 5
8.0