-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path150.cpp
More file actions
68 lines (65 loc) · 1.75 KB
/
Copy path150.cpp
File metadata and controls
68 lines (65 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<stdio.h>
#include<vector>
#include<stack>
#include<string>
using namespace std;
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> works;
for (int i = 0; i < tokens.size(); ++i) {
const string& s = tokens[i];
if (s.length() == 1) {
int a;
int b;
switch (s.c_str()[0]) {
#define GETVALUES b = works.top(); works.pop(); a = works.top(); works.pop();
#define SETVALUE(x) works.push(x);
case '+':
GETVALUES;
SETVALUE(a+b);
break;
case '-':
GETVALUES;
SETVALUE(a-b);
break;
case '*':
GETVALUES;
SETVALUE(a*b);
break;
case '/':
GETVALUES;
SETVALUE(a/b);
break;
default:
SETVALUE(stoi(s));
break;
}
} else {
SETVALUE(stoi(s));
}
}
return works.top();
}
};
int main()
{
vector<string> in;
Solution s;
#if 0
in.push_back("2");
in.push_back("1");
in.push_back("+");
in.push_back("3");
in.push_back("*");
#endif
#if 1
in.push_back("4");
in.push_back("13");
in.push_back("5");
in.push_back("/");
in.push_back("+");
#endif
printf("%d\n", s.evalRPN(in));
return 0;
}