#C14574. Mathematical Expression Evaluation
Mathematical Expression Evaluation
Mathematical Expression Evaluation
You are given a string representing a mathematical expression and an integer representing the number of decimal places (precision). The expression may contain integers, decimals, the operators +
, -
, *
, /
, as well as parentheses. Your task is to evaluate the expression according to the standard operator precedence rules and output the result rounded to the given precision.
If the expression is invalid (for example, it has incomplete syntax or attempts division by zero), output Invalid input
.
For valid expressions, the result should be printed using fixed-point notation. For rounding, you may refer to the formula $$rounded\ result = round(result, n)$$ where n is the specified precision.
inputFormat
The input is given via stdin and consists of two lines:
- The first line contains a string representing the mathematical expression.
- The second line contains an integer indicating the number of decimal places for the result.
outputFormat
Output the evaluated result rounded to the specified number of decimal places via stdout. If the expression is invalid or an error occurs (such as division by zero), output exactly Invalid input
.
3 + 2
2
5.00