Simplest and easy to understand c++ solution

programming
Tags: #<Tag:0x00007f2421f434d0>

#1

int fun(int a,int b,string& op) // need to use string& op because op is pointer
{
if(op=="+") return(a+b);
else if(op=="-") return(a-b);
else if(op=="") return(ab);
else if(op=="/") return(a/b);
}
int Solution::evalRPN(vector &A) {
stack x;
for(string& op: A) //op iterate untill the it reaches to last element
{ if(op=="+"||op=="-"||op=="*"||op=="/")
{
int b=x.top();
x.pop();
int a=x.top();
x.pop();
int result=fun(a,b,op);
x.push(result);
}
else
x.push(stoi(op));
}
return x.top();
}