-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path150.cpp.old
More file actions
69 lines (66 loc) · 1.8 KB
/
Copy path150.cpp.old
File metadata and controls
69 lines (66 loc) · 1.8 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
69
#include<stdio.h>
#include<vector>
#include<stack>
#include<string>
using namespace std;
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<string> 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 = stoi(works.top()); works.pop(); a = stoi(works.top()); works.pop();
#define SETVALUE(x) works.push(to_string(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:
works.push(s);
break;
}
} else {
works.push(s);
}
}
const string& sr = works.top();
return stoi(sr);
}
};
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;
}