-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExprStruct.cpp
More file actions
401 lines (311 loc) · 7.79 KB
/
ExprStruct.cpp
File metadata and controls
401 lines (311 loc) · 7.79 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "ExprStruct.h"
#include <stack>
#include <stdexcept>
double calculate(char P, double val1, double val2)
{
double ans = 0;
switch(P)
{
case '+':
ans = val1 + val2;
break;
case '-':
ans = val1 - val2;
break;
case '*':
ans = val1 * val2;
break;
case '/':
ans = val1 / val2;
break;
case '^':
ans = pow(val1,val2);
break;
}
return ans;
}
Node::Node(char P, bool isOperator)
{
isOperand = !isOperator;
c = P;
if(isOperator)
{
existVar = false;
} else if('0' <= P && P <= '9')
{
existVar = false;
val = static_cast<double>(P - '0');
} else if('a' <= P && P <= 'z')
{
existVar = true;
}
}
Expr::Expr(const Expr& other):existVar(other.existVar),prefix(other.prefix)
{
varMap = other.varMap;
this->root = clone(other.root);
}
Expr& Expr::operator=(const Expr& other)
{
if(this != &other)
{
Node* newNode = clone(other.root);
destroy(this->root);
this->root = newNode;
this->varMap = other.varMap;
prefix = other.prefix;
this->existVar = other.existVar;
}
return *this;
}
Expr::~Expr()
{
destroy(root);
}
QString Expr::toInfix() const
{
QString ans = toInfix(root);
return ans;
}
void Expr::setVarValue(char c,double v){
varMap[c] = v;
}
double Expr::value()
{
return calculate(root);
}
QString Expr::toInfix(Node* root) const
{
if(!root) return "";
if(root->isOperand) return QString(root->c);
QString ans = "";
const std::unordered_map<char,int> priority = {{'+',1},{'-',1},{'*',2},{'/',2},{'^',3}};
QString L = toInfix(root->left);
QString R = toInfix(root->right);
int P_parent = priority.at(root->c);
// 左子树加括号判断
bool leftBracket = false;
if (!root->left->isOperand)
{
int P_left = priority.at(root->left->c);
if (P_parent > P_left) {
leftBracket = true;
}
else if (P_parent == P_left && root->c == '^') {
leftBracket = true;
}
}
// 组合左子树部分
if (leftBracket) ans += "(";
ans += L;
if (leftBracket) ans += ")";
// 插入父节点运算符
ans += root->c;
// 右子树加括号判断
bool rightBracket = false;
if (!root->right->isOperand)
{
int P_right = priority.at(root->right->c);
if (P_parent > P_right) {
rightBracket = true;
}
else if (P_parent == P_right && root->c != '^') {
rightBracket = true;
}
}
// 组合右子树部分
if (rightBracket) ans += "(";
ans += R;
if (rightBracket) ans += ")";
return ans;
}
double Expr::calculate(Node* root)
{
if(root->isOperand)
{
if(!('0'<= root->c && root->c <= '9')) root->val = varMap[root->c];
return root->val;
}
double left = this->calculate(root->left);
double right = this->calculate(root->right);
char c = root->c;
if(c == '/' && right == 0) throw std::runtime_error("除数出现0,请检查表达式");
double ans = ::calculate(c,left,right);
return ans;
}
Node* Expr::clone(const Node* other)
{
if(!other)
{
return nullptr;
}
Node* newNode = new Node((*other).c,!(*other).isOperand);
newNode->left = clone(other->left);
newNode->right = clone(other->right);
newNode->val = other->val;
newNode->existVar = other->existVar;
return newNode;
}
bool validPrefix(QString s)
{
std::stack<double> stk;
std::stack<bool> Var;
for(int i = s.length()-1; i >= 0; --i)
{
char c = s.at(i).toLatin1();
if(isOperator(c))
{
if(stk.size() < 2) return false;
double left = stk.top();
bool leftVar = Var.top();
stk.pop();
Var.pop();
double right = stk.top();
bool rightVar = Var.top();
stk.pop();
Var.pop();
if(c == '/' && !rightVar && right == 0) return false;
double res = 0;
if(!leftVar && !rightVar)
{
res = calculate(c,left,right);
}
stk.push(res);
Var.push(leftVar || rightVar);
} else {
// 若为变量则初始为0
double val = 0;
bool isCurrentVar = false;
if('0' <= c && c <= '9')
{
val = static_cast<double> (c - '0');
}
else if('a' <= c && c <= 'z')
{
val = 0;
isCurrentVar = true;
}
else
{
return false;
}
stk.push(val);
Var.push(isCurrentVar);
}
}
return stk.size() == 1 && Var.size() == 1;
}
Expr createExpr(QString prefix)
{
/*
*由一个合法的波兰表达式转为二叉树存储
*/
Expr ans;
ans.prefix = prefix;
std::stack<Node*> s;
for(int i = prefix.length()-1; i >= 0; i--)
{
QChar P = prefix.at(i);
char c = P.toLatin1();
if(isOperator(c))
{
Node* leftNode = s.top();
s.pop();
Node* rightNode = s.top();
s.pop();
Node* newNode = new Node(c);
newNode->left = leftNode;
newNode->right = rightNode;
newNode->existVar = leftNode->existVar || rightNode->existVar;
if (!newNode->existVar) {
newNode->val = calculate(c, leftNode->val, rightNode->val);
} else {
ans.existVar = true;
}
s.push(newNode);
} else {
Node* newNode = new Node(c,false);
if('a' <= c && c <= 'z')
{
ans.varMap[c] = 0;
ans.existVar = true;
}
s.push(newNode);
}
}
if(s.size() == 1)
{
ans.root = s.top();
s.pop();
}
else
{
qDebug() << "波兰表达式转化失败";
while(!s.empty())
{
Node* toClearNode = s.top();
s.pop();
destroy(toClearNode);
}
}
return ans;
}
Expr compoundExpr(char P, const Expr& e1,const Expr& e2)
{
/*
* 合成两个表达式
*/
Expr ans;
ans.existVar = e1.existVar || e2.existVar;
ans.root = new Node(P);
ans.root->left = ans.clone(e1.root);
ans.root->right = ans.clone(e2.root);
ans.root->existVar = e1.existVar || e2.existVar;
if(!ans.existVar)
{
ans.root->val = calculate(P,e1.root->val,e2.root->val);
}
else
{
ans.root->val = 0;
}
ans.prefix = QString(P) + e1.prefix + e2.prefix;
return ans;
}
QString Expr::toTree() const
{
if(!root) return "";
QString ret = "";
generateTreeString(root, "",true, ret);
return ret;
}
void Expr::generateTreeString(Node* root,QString pre, bool isTail, QString &str) const
{
if(!root) return;
QString nodeText;
if(!root->isOperand)
{
nodeText = QString("Operator: %1").arg(root->c);
}
else
{
nodeText = QString("Operand: %1").arg(root->c);
}
str += pre;
if (isTail) {
str += "└── "; // 右孩子,或者独生子
pre += " ";
} else {
str += "├── "; // 左孩子
pre += "│ "; // 下一层的缩进带竖线
}
str += nodeText + "\n";
if (root->left && root->right) {
generateTreeString(root->left, pre, false, str);
generateTreeString(root->right, pre, true, str);
} else if (root->left) {
generateTreeString(root->left, pre, true, str);
} else if (root->right) {
generateTreeString(root->right, pre, true, str);
}
}