#C13115. Evaluating Mathematical Expressions
Evaluating Mathematical Expressions
Evaluating Mathematical Expressions
You are given a mathematical expression as a string. The expression can include non-negative numbers (which may contain decimal points), the operators \( + \), \( - \), \( \times \), and \( \div \), as well as parentheses \( ( \) and \( ) \). You must evaluate the expression and output the result as a floating point number.
Operator precedence follows standard mathematical conventions: Multiplication and division have higher precedence than addition and subtraction, and parentheses can be used to alter this precedence.
For example:
- Expression:
2+3
produces result5.0
. - Expression:
2+3*4
produces result14.0
. - Expression:
2*(3+4)
produces result14.0
.
Implement a program that reads the expression from standard input (stdin) and writes the computed result to standard output (stdout).
inputFormat
The input consists of a single line containing the mathematical expression string. The expression may contain spaces.
Example:
2 + 3 * ( 4 - 2 ) / 2
outputFormat
Output the computed result as a floating point number. No extra spaces or lines should be printed.
Example:
5.0## sample
2+3
5.0
</p>