1 class Solution {
2 stack<int>str;
3 public:
4 int evalRPN(vector<string>& tokens) {
5 string c;
6 int opa,opb;
7 for(auto c:tokens)
8 {
9 if(c.size()>1||isdigit(c[0]))str.push(stoi(c)); //isdigit()是把字符读取后变成整数再判断
10 else
11 {
12 opb=str.top();str.pop();
13 opa=str.top();str.pop();
14 switch(c[0]) //switch 只能读取一个字符,不能读取字符串,会报错
15 {
16 case '*':
17 {
18 opa=opa*opb;
19 str.push(opa);
20 break;
21 }
22 case '/':
23 {
24 opa=opa/opb;
25 str.push(opa);
26 break;
27 }
28 case '+':
29 {
30 opa=opa+opb;
31 str.push(opa);
32 break;
33 }
34 case '-':
35 {
36 opa=opa-opb;
37 str.push(opa);
38 break;
39 }
40 }
41 }
42
43 }
44
45 return str.top();
46 }
47 };