-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cpp
200 lines (166 loc) · 4.81 KB
/
Stack.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "Stack.hpp"
Stack::Stack() {
return;
}
Stack::Stack(Stack const &stack) {
*this = stack;
}
Stack &Stack::operator=(Stack const &stack) {
this->c = stack.c;
return *this;
}
Stack::~Stack() {
return;
}
Stack::iterator Stack::begin() {
return this->c.rbegin();
}
Stack::iterator Stack::end() {
return this->c.rend();
}
IOperand const & Stack::at(size_type pos) {
return *(this->c.at(this->size() - (pos + 1)));
}
void Stack::pop() {
if (this->empty() == true)
throw StackSizeException();
std::stack<const IOperand*>::pop();
}
void Stack::dump() {
for (Stack::iterator iterator = this->begin(); iterator != this->end(); iterator++) {
std::cout << (*iterator)->toString() << std::endl;
}
}
void Stack::vdump() {
for (Stack::iterator iterator = this->begin(); iterator != this->end(); iterator++) {
std::cout << "type: " << (*iterator)->getType() << "\tvalue: " << (*iterator)->toString() << std::endl;
}
}
void Stack::assert(IOperand const & iOperand) {
if (this->empty() == true)
throw StackSizeException();
if (this->at(0).getType() != iOperand.getType() || this->at(0).toString() != iOperand.toString()) {
throw StackAssertException(true);
}
}
void Stack::print() {
if (this->empty() == true)
throw StackSizeException();
if (this->at(0).getType() != Int8)
throw StackAssertException(false);
std::cout << static_cast<int8_t>(std::stoi(this->at(0).toString())) << std::endl;
}
void Stack::add() {
if (this->size() < 2)
throw StackSizeException();
IOperand const * iOperand = this->at(1) + this->at(0);
this->insert(iOperand);
}
void Stack::sub() {
if (this->size() < 2)
throw StackSizeException();
IOperand const * iOperand = this->at(1) - this->at(0);
this->insert(iOperand);
}
void Stack::mul() {
if (this->size() < 2)
throw StackSizeException();
IOperand const * iOperand = this->at(1) * this->at(0);
this->insert(iOperand);
}
void Stack::div() {
if (this->size() < 2)
throw StackSizeException();
IOperand const * iOperand = this->at(1) / this->at(0);
this->insert(iOperand);
}
void Stack::mod() {
if (this->size() < 2)
throw StackSizeException();
IOperand const * iOperand = this->at(1) % this->at(0);
this->insert(iOperand);
}
void Stack::max() {
Stack::iterator finalIterator;
double value;
if (this->empty() == true) {
throw StackSizeException();
}
finalIterator = this->begin();
value = std::stod(this->at(0).toString());
for (Stack::iterator iterator = this->begin() + 1; iterator != this->end(); iterator++) {
if (std::stod((*iterator)->toString()) > value) {
value = std::stod((*iterator)->toString());
finalIterator = iterator;
}
}
std::cout << (*finalIterator)->toString() << std::endl;
}
void Stack::min() {
Stack::iterator finalIterator;
double value;
if (this->empty() == true) {
throw StackSizeException();
}
finalIterator = this->begin();
value = std::stod(this->at(0).toString());
for (Stack::iterator iterator = this->begin() + 1; iterator != this->end(); iterator++) {
if (std::stod((*iterator)->toString()) < value) {
value = std::stod((*iterator)->toString());
finalIterator = iterator;
}
}
std::cout << (*finalIterator)->toString() << std::endl;
}
void Stack::drop() {
while (this->empty() == false) {
std::stack<const IOperand*>::pop();
}
}
void Stack::insert(IOperand const * iOperand) {
this->pop();
this->pop();
std::stack<const IOperand*>::push(iOperand);
}
void Stack::exit() {
std::exit(EXIT_SUCCESS);
}
Stack::StackSizeException::StackSizeException() {
return;
}
Stack::StackSizeException::~StackSizeException() throw() {
return;
}
const char *Stack::StackSizeException::what() const throw() {
return "StackSizeException: the number of element in the stack is too small for this action";
}
Stack::StackSizeException & Stack::StackSizeException::operator=(StackSizeException const &stackSizeException) {
(void)stackSizeException;
return *this;
}
Stack::StackSizeException::StackSizeException(StackSizeException const &stackSizeException) {
*this = stackSizeException;
return;
}
Stack::StackAssertException::StackAssertException() {
return;
}
Stack::StackAssertException::StackAssertException(bool isAssertInstruction) : _isAssertInstruction(isAssertInstruction){
return;
}
Stack::StackAssertException::~StackAssertException() throw() {
return;
}
const char *Stack::StackAssertException::what() const throw() {
if (this->_isAssertInstruction == true)
return "StackAssertException: The assert is false";
return "StackAssertException: The first element on the stack is not an int8 value";
}
Stack::StackAssertException & Stack::StackAssertException::operator=(StackAssertException const &stackAssertException) {
(void)stackAssertException;
return *this;
}
Stack::StackAssertException::StackAssertException(StackAssertException const &stackAssertException) {
*this = stackAssertException;
return;
}