#K82022. Evaluate Mathematical Expression
Evaluate Mathematical Expression
Evaluate Mathematical Expression
You are given a string representing a mathematical expression containing non-negative integers, the operators +
and -
, and parentheses. The expression is guaranteed to be well-formed (i.e. no spaces and matching parentheses). Your task is to evaluate the expression and output the resulting integer.
The grammar for the expression can be described in LaTeX as follows:
$$\text{Expression} \to \text{Term} \;(|\; \text{Expression} \;\pm\; \text{Term})$$
where a Term is either a number or an expression wrapped in parentheses, i.e., $$\text{Term} \to \text{Number} \;|\; (\text{Expression}).$$
Note that all numbers are non-negative and there will be no spaces in the input.
inputFormat
The input consists of a single line containing the mathematical expression as a string.
Input Format:
EXPRESSION
For example:
2-(3+4)
outputFormat
Output a single integer which is the result of the evaluated expression.
Output Format:
RESULT
For example, for the input 2-(3+4)
, the output should be -5
.
42
42