Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
Some examples:
"1 + 1" = 2 " 2-1 + 2 " = 3 "(1+(4+5+2)-3)+(6+8)" = 23
Note: Do not use the eval built-in library function.
class Solution {
public:
int calculate(string s) {
stack<int> calcst;
calcst.push(1);
s += ')';
int sign = 1, num = 0, i = 0, ans = 0;
for (i = 0; i < s.size(); i++){
if (s[i] >= '0' && s[i] <= '9'){
num = num * 10 + s[i] - '0';
}else if (s[i] == '+' || s[i] == '-' || s[i] == ')'){
ans += calcst.top() * sign * num;
if (s[i] == ')') calcst.pop();
sign = s[i] == '-' ? -1 : 1;
num = 0;
}else if (s[i] == '('){
calcst.push(calcst.top() * sign);
sign = 1;
}
}
return ans;
}
};
浙公网安备 33010602011771号