-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpression.cpp
225 lines (200 loc) · 5.03 KB
/
Expression.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <QDebug>
#include "Queue.h"
#include "Expression.h"
#include "Random.h"
#define ready 0
#define division_by_zero 1
#define negative_log 2
#define mul_overflow 3
#define div_overflow 4
#define exp_overflow 5
#define unknown_funct 6
#define stack_underflow 7
QString error_array[8] = {
"Ready", "division by zero", "negative log", "multiplication overflow",
"division overflow", "exp overflow", "unknown function",
"Stack underflow"
};
#define max_op 19
static char op[max_op][7] = {
"AND", "OR", "XOR",
"LE", "LT", "EQ", "NE", "GT", "GE",
"+", "-", "%",
"*", "/",
"RND", "STATE", "SUM", "ABS",
"^"
};
/*
static long op_pr[max_op] = {
0,0,0,
1,1,1,1,1,1,
2,2,2,
3,3,
4,4,4,4,
5
};
*/
static long values[max_op] = {
2,2,2,
2,2,2,2,2,2,
2,2,2,
2,2,
1,1,1,1,
2
};
Expression::Expression() : tree(NULL)
{
}
Expression::~Expression()
{
}
QString Expression::getError()
{
return exp_error;
}
void Expression::showTreeInternal (treeatom *tree, QString &resStr)
{
bool showBrace = false;
if (tree->left != NULL)
showTreeInternal (tree->left, resStr);
switch (tree->what) {
case 1 : resStr += QString("var(%1) ").arg(tree->value);
break;
case 2 : resStr += QString("%1 ").arg(tree->value);
break;
case 3 :
showBrace = values[tree->value] == 1;
resStr += QString("%1%2").arg(op[tree->value]).arg(showBrace?"(":" ");
}
if (tree->right != NULL)
showTreeInternal (tree->right,resStr);
if (showBrace) resStr += ") ";
}
void Expression::show()
{
QString str;
showTreeInternal (tree, str);
qDebug() << str;
}
void Expression::clearTreeInternal(treeatom *tree)
{
if (tree->left != nullptr)
clearTreeInternal (tree->left);
if (tree->right != nullptr)
clearTreeInternal (tree->right);
free (tree);
}
void Expression::clear()
{
clearTreeInternal(tree);
tree = nullptr;
}
void Expression::importQueueInternal(Queue *expr_queue, treeatom **tree)
{
cell *temp;
temp = expr_queue->getHead();
*tree = new treeatom;
(*tree)->left = NULL;
(*tree)->right = NULL;
(*tree)->what = temp->type;
(*tree)->value = temp->value;
/* loeschen des Kopfes der Schlange */
expr_queue->dequeue();
if (((*tree)->what == 3) && (values[(*tree)->value]>0)) {
/* garantiert einen Operanden */
importQueueInternal (expr_queue, &((*tree)->right));
if (values[(*tree)->value]==2)
importQueueInternal (expr_queue, &((*tree)->left));
}
}
void Expression::importQueue (Queue *expr_queue)
{
if (NULL != tree) clear();
importQueueInternal(expr_queue, &tree);
}
int Expression::power (int a, int p)
{
if (p==0)
return 1;
else
for (;p>1; a *= a, p--);
return a;
}
int Expression::computeTreeInternal(treeatom *tree)
{
treeatom *left,*right;
int vResult;
if (tree->what == 2) {
/* konstanter Wert */
return (tree->value);
}
else if (tree->what == 1) {
/* Variable */
return varList.at(tree->value);
}
else {
left = tree->left;
right = tree->right;
switch (tree->value) {
case 0 /* AND */:
if (computeTreeInternal(left))
return (computeTreeInternal(right));
return (0);
case 1 /* OR */:
if (computeTreeInternal(left))
return (1);
return (computeTreeInternal(right));
case 2 /* XOR */:
if (computeTreeInternal(left))
return (!computeTreeInternal(right));
else
return (computeTreeInternal(right));
case 3 /* <= */:
return (computeTreeInternal(left)<=computeTreeInternal(right)?1:0);
case 4 /* < */:
return (computeTreeInternal(left)<computeTreeInternal(right)?1:0);
case 5 /* = */:
return (computeTreeInternal(left)==computeTreeInternal(right)?1:0);
case 6 /* != */:
return (computeTreeInternal(left)!=computeTreeInternal(right)?1:0);
case 7 /* > */:
return (computeTreeInternal(left)>computeTreeInternal(right)?1:0);
case 8 /* >= */:
return (computeTreeInternal(left)>=computeTreeInternal(right)?1:0);
case 9 /* + */:
return (computeTreeInternal(left)+computeTreeInternal(right));
case 10 /* - */:
return (computeTreeInternal(left)-computeTreeInternal(right));
case 11 /* % */:
return (computeTreeInternal(left)%computeTreeInternal(right));
case 12 /* * */:
return (computeTreeInternal(left)*computeTreeInternal(right));
case 13 /* / */:
return (computeTreeInternal(left)/computeTreeInternal(right));
case 14 /* RND */:
return (my_random(computeTreeInternal(right)));
case 15 /* STATE */:
return currentField->getNeighborState(computeTreeInternal(right));
case 16 /* SUM */:
vResult = computeTreeInternal(right);
return (currentField->getNeighborSum(true).at(vResult));
case 17 /* ABS */:
return (abs(computeTreeInternal(right)));
case 18 /* ^ */:
return (power(computeTreeInternal(left),computeTreeInternal(right)));
default:
exp_error = unknown_funct;
}/* switch */
}
/* falls was schiefl"auft, gib Null zur"uck */
return 0;
}
int Expression::compute(Field *aField, QList<int> &vVars)
{
currentField = aField;
varList = vVars;
return computeTreeInternal(tree);
}