-
Notifications
You must be signed in to change notification settings - Fork 12
/
evalRPN.cpp
28 lines (28 loc) · 1 KB
/
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
int evalRPN(vector<string> &tokens) {
stack<int> st;
for(int i = 0; i < tokens.size(); ++i) {
if(tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
int v1=st.top();
st.pop();
int v2=st.top();
st.pop();
switch(tokens[i][0]) {
case '+':
st.push(v2 + v1);
break;
case '-':
st.push(v2 - v1);
break;
case '*':
st.push(v2 * v1);
break;
case '/':
st.push(v2 / v1);
break;
}
} else {
st.push(atoi(tokens[i].c_str()));
}
}
return st.top();
}