#C14435. Basic Calculator
Basic Calculator
Basic Calculator
This problem requires you to implement a basic calculator that evaluates a mathematical expression given as a string. The expression may contain the binary operators \(+\), \(-\), \(\times\) (written as *), \(\div\) (written as /), as well as parentheses \((\) and \()\). The expression may also contain negative numbers. You must respect the standard precedence rules where multiplication and division are evaluated before addition and subtraction. Your program should read the expression from stdin
and output a single floating point number to stdout
representing the result.
Note: If the expression is invalid, you can assume that such input will not be provided.
inputFormat
The input consists of a single line containing a valid mathematical expression. The expression will only include digits, decimal points, binary operators (+, -, *, /), and parentheses. White spaces may be present and should be ignored.
Example: 2 + 3 * (4 - 1)
outputFormat
Output a single floating point number which is the result of evaluating the expression. The result should be printed to stdout
.
Example: For the input 2 + 3
, the output should be 5.0
.
2 + 3
5.0
</p>