-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathep2.cpp
More file actions
111 lines (91 loc) · 2.07 KB
/
ep2.cpp
File metadata and controls
111 lines (91 loc) · 2.07 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <string>
using namespace std;
typedef struct link {
int element; // E type, value stored in this link/node
struct link* next; // Reference to the next link/node
} Link;
typedef struct stack {
Link* top; // reference to the first element
int size; // number of elements
} Stack;
void clear(Stack* s);
void push(Stack* s, int it);
int pop(Stack* s);
int topValue(Stack* s);
int length(Stack* s);
Stack* create_stack();
Link* create_link(int it, Link* nextval);
int main(void) {
string input;
Stack* s = create_stack();
do {
cin >> input;
if(input != "+" && input != "-" && input != "*" && input != "END" && input != "EOF") {
int it = stoi(input);
push(s, it);
}
else if(input == "+") {
int v1, v2;
v2 = pop(s);
v1 = pop(s);
push(s, v1+v2);
}
else if(input == "*") {
int v1, v2;
v2 = pop(s);
v1 = pop(s);
push(s, v1*v2);
}
else if(input == "-") {
int v1, v2;
v2 = pop(s);
v1 = pop(s);
push(s, v1-v2);
}
else if(input == "END" && s->size > 0) {
int r = topValue(s);
cout << r << endl;
pop(s);
}
} while(input != "EOF");
return 0;
}
void clear(Stack* s) {
while(s->size != 0) {
pop(s);
}
}
void push(Stack* s, int it) {
s->top = create_link(it, s->top);
s->size++;
}
int pop(Stack* s) {
Link* t = s->top;
if(s->top == NULL) {
return -1; // Error
}
int pop = s->top->element;
s->top = s->top->next;
s->size--;
delete t;
return pop;
}
int topValue(Stack* s) {
return s->top->element;
}
int length(Stack* s) {
return s->size;
}
Stack* create_stack() {
Stack* s = (Stack *) new int;
s->top = NULL;
s->size = 0;
return s;
}
Link* create_link(int it, Link* nextval) {
Link* n = (Link *) new int;
n->element = it;
n->next = nextval;
return n;
}