-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostfix-notation.cpp
More file actions
79 lines (74 loc) · 1.55 KB
/
postfix-notation.cpp
File metadata and controls
79 lines (74 loc) · 1.55 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
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <queue>
typedef long long ll;
typedef double ld;
typedef unsigned long long ull;
using namespace std;
void file() {
freopen("postfix.in", "r", stdin);
freopen("postfix.out", "w", stdout);
}
template <typename T>
struct new_knot {
T x;
new_knot *next;
};
template <typename T>
class _stack {
private:
new_knot<T> *head;
public:
_stack():head(nullptr){};
void add(T x) {
new_knot<T> *tmp;
tmp = new new_knot<T>;
tmp -> x = x;
tmp -> next = head;
head = tmp;
}
T pop_back() {
if (head != nullptr) {
new_knot<T> *tmp = head;
T ans = head -> x;
head = head -> next;
delete tmp;
return ans;
}
}
T top() {
if(head != nullptr)
return head -> x;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
file();
_stack<int> my_stack;
char x;
while(cin >> x) {
if(x >= '0' && x <= '9') {
my_stack.add(x - '0');
}
else {
int y1 = my_stack.pop_back();
int y2 = my_stack.pop_back();
if(x == '+') {
my_stack.add(y1 + y2);
}
else if(x == '-') {
my_stack.add(y2 - y1);
}
else {
my_stack.add(y1 * y2);
}
}
}
cout << my_stack.top() << endl;
}