-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvaluate_Reverse_Polish_Notation.cpp
More file actions
36 lines (36 loc) · 1.16 KB
/
Evaluate_Reverse_Polish_Notation.cpp
File metadata and controls
36 lines (36 loc) · 1.16 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
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> memory;
int i;
int op1, op2, result;
for (i = 0;i<tokens.size();i++){
if (((tokens[i][0] == '+') || (tokens[i][0] == '-') || (tokens[i][0] == '*') || (tokens[i][0] == '/')) && (tokens[i].length()==1)){
op2 = memory.top();
memory.pop();
op1 = memory.top();
memory.pop();
switch(tokens[i][0]){
case '+':
result = op1 + op2;
break;
case '-':
result = op1 - op2;
break;
case '*':
result = op1 * op2;
break;
case '/':
result = op1 / op2;
break;
default:
break;
}
memory.push(result);
}
else
memory.push(stoi(tokens[i], nullptr, 10));
}
return memory.top();
}
};