#130Medium
Evaluating a bracketed expression
Time: 1 s · Memory: 256 MB
- Recursion
Statement
A formula is defined as follows:
- a single digit (
0–9) is a formula; - if and are formulas, then
(A+B),(A-B)and(A*B)are formulas too.
For example, (2+(3*4)) and ((9-7)*(1-6)) are formulas. Compute the value of the given formula.
Input format
A single line with the formula, without spaces, of length up to . All intermediate results do not exceed in absolute value.
Output format
A single integer — the value of the formula.
Examples
Input
(2+(3*4))
Output
14
Input
((9-7)*(1-6))
Output
-10
Note
The definition of a formula is recursive, and so is the evaluation. The function reads one formula starting at the current character: if it is a digit, return it; if it is (, read the left formula (calling itself), the sign and the right formula, skip ) and return the result. Keeping the current position in a global variable is convenient.
Second example: . A 64-bit type is needed.
