-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_evalRPN.cpp
35 lines (34 loc) · 1.11 KB
/
2_evalRPN.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
// Things to notice:
// 1) remember the order of the operands in the stack
// 2) remember the positive and negative sign of operands to confuse operator
bool isOperator(string token) {
if(token.size()!=1) return false;
switch(token[0]) {
case '+':
case '-':
case '*':
case '/': return true;
default: return false;
}
}
int evalRPN(vector<string> &tokens) {
if (tokens.size()==0) return 0;
stack<string> s;
for(auto token: tokens) {
if(isOperator(token)) {
int o2 = stoi(s.top()); s.pop();
int o1 = stoi(s.top()); s.pop();
int result;
switch(token[0]) {
case '+': result = o1+o2; break;
case '-': result = o1-o2; break;
case '*': result = o1*o2; break;
case '/': result = o1/o2; break;
}
s.push(to_string(result));
}else {
s.push(token);
}
}
return stoi(s.top());
}