I have updated the code witch now works.
The method to solve this problem is:
when meet a number, push it into the stack.
when meet a operator(+, -, *, /), pop two number from the stack, do the math and push the result into the stack again. that's how to Evaluate Reverse Polish Notation.
#include <stack>
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> st;
int a, b;
for (int i = 0; i < tokens.size(); i++){
if (tokens[i] != "+" && tokens[i] != "-" && tokens[i] != "*" && tokens[i] != "/"){
st.push(atoi(tokens[i].c_str()));
continue;
}
b = st.top();st.pop();
a = st.top();st.pop();
switch(tokens[i][0]){
case '+':
st.push(a+b);break;
case '-':
st.push(a-b);break;
case '*':
st.push(a*b);break;
case '/':
st.push(a/b);break;
}
}
return st.top();
}
};