-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd.cpp
53 lines (41 loc) · 996 Bytes
/
d.cpp
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
#include <fstream>
const int MAX_STACK_SIZE = 1e4;
class Stack{
public:
void push(const int& new_value) {
data[++head] = new_value;
}
int pop() {
return data[head--];
}
private:
int head = -1;
int data[MAX_STACK_SIZE];
};
int main() {
std::ifstream fin("postfix.in");
std::ofstream fout("postfix.out");
char e;
Stack stack;
while (fin >> e) {
if (e == '+') {
int b = stack.pop();
int a = stack.pop();
stack.push(a + b);
} else if (e == '-') {
int b = stack.pop();
int a = stack.pop();
stack.push(a - b);
} else if (e == '*') {
int b = stack.pop();
int a = stack.pop();
stack.push(a * b);
} else if (e >= '0' && e <= '9') {
stack.push(e - '0');
}
}
fout << stack.pop() << '\n';
fin.close();
fout.close();
return 0;
}