forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0227.cpp
More file actions
48 lines (36 loc) · 1.19 KB
/
0227.cpp
File metadata and controls
48 lines (36 loc) · 1.19 KB
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
47
48
class Solution {
public:
int calculate(string s) {
stack<int> result;
char opr = '+';
int temp = 0;
for(int i = 0; i < s.size(); i++){
if(isdigit(s[i]))
temp = (temp * 10) + (s[i] - '0');
if(i == s.size()-1 || s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/'){
if(opr == '+')
result.push(temp);
else if(opr == '-')
result.push(-temp);
else if(opr == '*'){
temp *= result.top();
result.pop();
result.push(temp);
}
else if(opr == '/'){
temp = result.top() / temp;
result.pop();
result.push(temp);
}
opr = s[i];
temp = 0;
}
}
int ans = 0;
while(!result.empty()){
ans += result.top();
result.pop();
}
return ans;
}
};