> For the complete documentation index, see [llms.txt](https://nataliekung.gitbook.io/solutions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nataliekung.gitbook.io/solutions/evaluate_reverse_polish_notation.md).

# Evaluate Reverse Polish Notation

int evalRPN(vector\<string>& tokens) {

stack\<int> s;

int a,b;

for(auto t : tokens){

if(isOperator(t)){

b=s.top();

s.pop();

a=s.top();//ab顺序反了！！

s.pop();

switch (t\[0]){//必须是char

case '+':

a+=b;

break;

case '-':

a-=b;

break;

case '\*':

a\*=b;

break;

case '/':

a/=b;

break;

}

s.push(a);

}

else{

s.push(stoi(t));

}

}

return s.top();

}

bool isOperator(const string \&o){

return o.size()==1&\&string("+-\*/").find(o)!=string::npos;

}
