-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathBasic Calculator II.cpp
46 lines (45 loc) · 1.58 KB
/
Basic Calculator II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
int calculate(string s) {
stack<int> nums;
stack<char> ops;
int n = 0;
for(int i = 0; i < s.size(); ++i){
if(s[i] >= '0' && s[i] <= '9'){
string t(1, s[i]);
n = n*10 + stoi(t);
}else if( s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/'){
nums.push(n);
n = 0;
if(!ops.empty() && (ops.top() == '*' || ops.top() == '/') ){
int n2 = nums.top(); nums.pop();
int n1 = nums.top(); nums.pop();
if(ops.top() == '*') nums.push(n1*n2);
else nums.push(n1/n2);
ops.pop();
}
ops.push(s[i]);
}
}
nums.push(n);
if(!ops.empty() && (ops.top() == '*' || ops.top() == '/') ){
int n2 = nums.top(); nums.pop();
int n1 = nums.top(); nums.pop();
if(ops.top() == '*') nums.push(n1*n2);
else nums.push(n1/n2);
ops.pop();
}
stack<int> tnums;
stack<char> tops;
while(!nums.empty()){ tnums.push(nums.top()); nums.pop();}
while(!ops.empty()) { tops.push(ops.top()); ops.pop(); }
while(!tops.empty()){ //cout<<tops.top()<<" " ;
int n1 = tnums.top(); tnums.pop();
int n2 = tnums.top(); tnums.pop();
if(tops.top() == '+') tnums.push(n1 + n2);
else tnums.push(n1 - n2);
tops.pop();
}
return tnums.top();
}
};