#C8376. Evaluate Mathematical Expression
Evaluate Mathematical Expression
Evaluate Mathematical Expression
You are given a string representing a mathematical expression that consists of numbers (which could be floating point), the operators +
, -
, *
, and /
, and possibly whitespace. Your task is to evaluate the expression and output the result as a float.
The expression should be evaluated according to the standard operator precedence rules (i.e. multiplication and division take precedence over addition and subtraction). Negative numbers and floating point numbers are supported. In case of an invalid expression, the program should report an error.
The expression may also contain parentheses for grouping. However, for the given test cases, you can assume they are simple expressions without nested parentheses.
Note: When performing division, use the standard floating-point division. All evaluations follow the standard arithmetic rules.
For example, if the input is:
3+5
then the output should be:
8.0
inputFormat
The input consists of a single line containing a valid mathematical expression. The expression is a string that may include digits, a decimal point, spaces, and the operators +
, -
, *
, and /
.
For instance:
45 + 35.2 / 7
outputFormat
Output the result of evaluating the expression as a floating-point number. The result should be printed to standard output.
For example:
50.028571428571426## sample
3+5
8.0