题目链接:https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
这个题目也是常见题目,很简单
int evalRPN(vector<string>& tokens) {
stack<int> s;
int num;
int a, b;
for (auto x : tokens){
if (x == "*"){
a = s.top();
s.pop();
b = s.top();
s.pop();
s.push(b*a);
}
else if (x == "/"){
a = s.top();
s.pop();
b = s.top();
s.pop();
s.push(b/a);
}
else if (x == "+"){
a = s.top();
s.pop();
b = s.top();
s.pop();
s.push(b + a);
}
else if (x == "-"){
a = s.top();
s.pop();
b = s.top();
s.pop();
s.push(b - a);
}
else{
istringstream is(x);//将string转int
is >> num;
s.push(num);
}
}
return s.top();
}
if else语句我本来想用switch代替的,但是c++不同于java,c++中switch只支持整型和枚举类型,不支持string。