-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackMachine.h
334 lines (256 loc) · 5.57 KB
/
StackMachine.h
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include <string>
#include "bits-stdc++.h"
using namespace std;
class StackMachine {
private:
stack<int> javaStack;
public:
int fullPop(stack<int>* stackParam)
{
int auxRegister = stackParam->top();
stackParam->pop();
return auxRegister;
}
void push(int n) {
javaStack.push(n);
}
void add() {
javaStack.push(fullPop(&javaStack) + fullPop(&javaStack));
}
void sub() {
int y = fullPop(&javaStack);
int x = fullPop(&javaStack);
javaStack.push(x - y);
}
void mul() {
int x = fullPop(&javaStack);
int y = fullPop(&javaStack);
javaStack.push(x * y);
}
void div() {
int y = fullPop(&javaStack);
int x = fullPop(&javaStack);
javaStack.push(x / y);
}
void swap() {
int x = fullPop(&javaStack);
int y = fullPop(&javaStack);
javaStack.push(x);
javaStack.push(y);
}
void drop() {
javaStack.pop();
}
void dup() {
javaStack.push(javaStack.top());
}
void over() {
int first = fullPop(&javaStack);
int second = javaStack.top();
javaStack.push(first);
javaStack.push(second);
}
bool jz() {
return fullPop(&javaStack) == 0;
}
bool jnz() {
return !jz();
}
bool jgz() {
return fullPop(&javaStack) > 0;
}
bool jlz() {
return fullPop(&javaStack) < 0;
}
void clear() {
stack<int>().swap(javaStack);
}
/*
bool matches(int a[]) {
if ((*(&a + 1) - a) != javaStack.size()) {
return false;
}
for (int i = 0; i < javaStack.size(); i++) {
if (javaStack.get(i) != a[i]) {
return false;
}
}
return true;
}
*/
void run();
void print() {
for(int i = 0; i < javaStack.size(); i++)
{
cout << fullPop(&javaStack) << " " << endl;
}
}
void printStackItens() {
stack<int> printStack;
string vectoItens = "{";
int javaStackSize = javaStack.size();
for (int i = 0; i < javaStackSize; i++)
{
int item = fullPop(&javaStack);
printStack.push(item);
vectoItens = vectoItens + " " + to_string(item);
}
for (int i = 0; i < javaStackSize; i++)
{
javaStack.push(printStack.top());
printStack.pop();
}
vectoItens = vectoItens + " }";
cout << vectoItens << endl;
}
vector<string> split(string str, char del) {
vector<string> strVec;
string temp = "";
for (int i = 0; i < (int)str.size(); i++) {
// If cur char is not del, then append it to the cur "word", otherwise
// you have completed the word, print it, and start a new word.
if (str[i] != del) {
temp += str[i];
}
else {
strVec.push_back(temp);
temp = "";
}
}
strVec.push_back(temp);
return strVec;
}
/*
vector<string> infixToPostfix(string infixExpr)
{
map<string, int> prec;
prec["*"] = 3;
prec["/"] = 3;
prec["+"] = 2;
prec["-"] = 2;
prec["("] = 1;
stack<string> opStack;
vector<string> postfixList;
postfixList.push_back(" ");
vector<string> tokenList = split(infixExpr, ' ');
string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string numbers = "0123456789";
for (string token : tokenList)
if (letters.find(token) != string::npos || numbers.find(token) != string::npos)
postfixList.push_back(token);
else if (token == "(")
opStack.push(token);
else if (token == ")")
{
string topToken = opStack.top();
opStack.pop();
while (topToken != "(")
{
postfixList.push_back(topToken);
topToken = opStack.top();
opStack.pop();
}
}
else
{
while (!opStack.empty() && prec[opStack.top()] >= prec[token])
{
postfixList.push_back(opStack.top());
opStack.pop();
}
opStack.push(token);
}
while (!opStack.empty())
{
postfixList.push_back(opStack.top());
opStack.pop();
}
string space = " ";
return postfixList;
}*/
// Function to return precedence of operators
int prec(char c)
{
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
// The main function to convert infix expression
// to postfix expression
vector<string> infixToPostfix(string s)
{
stack<char> st; // For stack operations, we are using
// C++ built in stack
string result;
for (int i = 0; i < s.length(); i++) {
char c = s[i];
// If the scanned character is
// an operand, add it to output string.
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9'))
result += c;
// If the scanned character is an
// ‘(‘, push it to the stack.
else if (c == '(')
st.push('(');
// If the scanned character is an ‘)’,
// pop and to output string from the stack
// until an ‘(‘ is encountered.
else if (c == ')') {
while (st.top() != '(') {
result += st.top();
st.pop();
}
st.pop();
}
// If an operator is scanned
else {
while (!st.empty()
&& prec(s[i]) <= prec(st.top())) {
if (c == '^' && st.top() == '^')
break;
else {
result += st.top();
st.pop();
}
}
st.push(c);
}
}
// Pop all the remaining elements from the stack
while (!st.empty()) {
result += st.top();
st.pop();
}
vector<string> postfixList;
int N = result.length();
string strAuxiliar;
strAuxiliar = "";
for (int i = 0; i < N; i++)
{
if (result[i] == '+' || result[i] == '-' || result[i] == '*' || result[i] == '/')
{
postfixList.push_back(strAuxiliar);
strAuxiliar = "";
string signal;
signal += result[i];
postfixList.push_back(signal);
}
else
{
strAuxiliar += result[i];
}
}
postfixList.push_back(strAuxiliar);
return postfixList;
}
};