标签:int pre str col dig == style stack ++
1 class Solution 2 { 3 void calc(stack<char>& op,stack<int>& num) 4 { 5 int y = num.top(); 6 num.pop(); 7 int x = num.top(); 8 num.pop(); 9 if(op.top() == ‘+‘) num.push(x + y); 10 else num.push(x - y); 11 op.pop(); 12 } 13 public: 14 int calculate(string s) 15 { 16 stack<char> op;//符号栈 17 stack<int> num;//数字栈 18 19 for(int i = 0;i < s.size();i ++) 20 { 21 char c = s[i]; 22 if(c == ‘ ‘) continue; 23 if(c == ‘(‘ || c == ‘+‘ || c == ‘-‘) op.push(c); 24 else if(c == ‘)‘) 25 { 26 op.pop(); 27 if(!op.empty() && op.top() != ‘(‘) calc(op,num); 28 } 29 else 30 { 31 int j = i; 32 while(j < s.size() && isdigit(s[j])) j ++; 33 num.push(atoi(s.substr(i,j - i).c_str())); 34 i = j - 1; 35 if(!op.empty() && op.top() != ‘(‘) calc(op,num); 36 } 37 } 38 return num.top(); 39 } 40 };
标签:int pre str col dig == style stack ++
原文地址:https://www.cnblogs.com/yuhong1103/p/12805584.html