Problem 395
Basic Calculator
Hard · 30 pts · Math, String, Stack, Recursion
Problem
Evaluate an arithmetic expression that contains non-negative integers, '+', '-', parentheses and spaces. A '-' can also be unary, meaning it negates what follows: this happens at the very start of the expression or right after '(', as in '-(2 + 3)' or '(-4 + 1)'. Spaces carry no meaning. Do not use a built-in eval function. Keep a running result and a stack of signs for the parentheses you are inside.
Input
A single line containing the expression.
Output
Print the value of the expression.
Constraints
1 <= length <= 300000; every intermediate value fits in a 32-bit signed integer
Example input
(1+(4+5+2)-3)+(6+8)
Example output
23
Explanation
In '(1+(4+5+2)-3)+(6+8)', the first group is 1 + 11 - 3 = 9 and the second is 14, so the result is 23.