-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatement.cpp
More file actions
261 lines (242 loc) · 7.88 KB
/
statement.cpp
File metadata and controls
261 lines (242 loc) · 7.88 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
#include "statement.h"
#include <QMap>
#include <QStack>
#include <QStringList>
statement::statement(QObject *parent) : QObject(parent) {
staTable["REM"] = REM;
staTable["LET"] = LET;
staTable["PRINT"] = PRINT;
staTable["INPUT"] = INPUT;
staTable["GOTO"] = GOTO;
staTable["IF"] = IF;
staTable["END"] = END;
eva = new EvalState();
}
/**区分不同command**/
QString statement::classify(QString s) {
QString firstWord; // 接受lineNumber
QString otherWord; // statement
firstWord = s.section(' ', 0, 0); //接受字符串的第一段
otherWord = s.section(' ', 1, -1); //接受字符串的第二段
bool flag = false;
otherWord.toInt(&flag);
if (!staTable.contains(firstWord)) { //若不包含 返回wrong
return "WRONG";
} else {
try {
switch (staTable[firstWord]) { //识别命令
case REM:
return "";
case LET:
convertToExp(otherWord);
if (expression == nullptr)
return QString("WRONG EXPRESSION : ") + s;
expression->eval(*eva);
delete expression;
break;
case PRINT: {
convertToExp(otherWord);
if (expression == nullptr)
return QString("WRONG EXPRESSION : ") + s;
int num = expression->eval(*eva);
delete expression;
return "PRINT " + QString::number(num);
}
case INPUT:
if (flag) throw QString(" can't be a number.");
return (QString("INPUT ") + otherWord);
case GOTO:
if (!flag) throw QString(" no such number.");
return s.section(' ', 1, 1);
case IF: {
bool _flag(false);
s.section(' ', -1, -1).toInt(&_flag);
// THEN 之后不是数字的情况
if (!_flag) {
return "WRONG";
}
//没有THEN的情况
if (s.section(' ', -2, -2) != "THEN") {
return "WRONG";
}
int exp1;
int exp2;
QString exp1_str;
QString exp2_str;
QStringList l = s.split(' ');
int i = 0;
for (; i < l.size(); i++)
if (l[i] == "<" || l[i] == ">" || l[i] == "<=" ||
l[i] == ">=" || l[i] == "=" || l[i] == "!=") //=
break;
exp1_str = s.section(' ', 1, i - 1);
exp2_str = s.section(' ', i + 1, -3);
convertToExp(exp1_str);
exp1 = expression->eval(*eva);
delete expression;
convertToExp(exp2_str);
exp2 = expression->eval(*eva);
delete expression;
if (l[i] == "<" && exp1 < exp2)
return s.section(' ', -1, -1);
else if (l[i] == ">" && exp1 > exp2)
return s.section(' ', -1, -1);
else if (l[i] == "<=" && exp1 <= exp2)
return s.section(' ', -1, -1);
else if (l[i] == ">=" && exp1 >= exp2)
return s.section(' ', -1, -1);
else if (l[i] == "=" && exp1 == exp2)
return s.section(' ', -1, -1);
else if (l[i] == "!=" && exp1 != exp2)
return s.section(' ', -1, -1);
else
break;
}
case END:
return "END";
}
} catch (QString s) {
return QString("WRONG : ") + s;
}
}
return "";
}
//将expression转化
void statement::convertToExp(QString s) {
QStringList wholeExpression;
QVector<QString> operators;
QVector<QString> operands;
// 将输入进来的字符串放在wholeExpression里
for (int i = 0; i < s.size(); i++) {
if (s[i] == '*' && s[i + 1] == '*') {
s.insert(i, ' ');
s.insert(i += 3, ' ');
s.insert(i + 1, ' ');
}
if (s[i] == '=' || s[i] == '+' || s[i] == '-' || s[i] == '*' ||
s[i] == '/' || s[i] == '(' || s[i] == ')') {
s.insert(i++, ' ');
s.insert(i + 1, ' ');
}
}
s = s.trimmed();
s.replace(QRegExp("[\\s]+"), " ");
wholeExpression = s.split(' ');
QStringList *tmp = infixToSuffi(wholeExpression);
if (tmp == nullptr) {
expression = nullptr;
} else
expression = combineExp(*tmp, 0, (*tmp).size());
}
//工具函数, 中缀转后缀时使用
/*判断是否是数字*/
bool isDigit(QString str) {
bool flag(false);
str.toInt(&flag);
return flag;
}
/*返回该运算符的优先级*/
int getPrioraty(QString op) {
if (op == "+" || op == "-") return 1;
if (op == "*" || op == "/") return 2;
if (op == "**") return 3;
return 0;
}
/*判断是否是运算符*/
bool isOperator(QString op) {
if (op == "+" || op == "-" || op == "*" || op == "/" || op == "**" ||
op == "=")
return true;
return false;
}
/*将表达式组合*/
Expression *statement::combineExp(QStringList &exp, int x, int y) {
QStack<Expression *> elem;
for (; x < y; x++) {
if (isDigit(exp[x])) {
Expression *e = new ConstantExp(exp[x].toInt());
elem.push(e);
} else if (isOperator(exp[x])) {
if (elem.empty()) {
return nullptr;
}
Expression *r = elem.pop();
if (elem.empty()) {
return nullptr;
}
Expression *l = elem.pop();
Expression *c = new CompoundExp(exp[x], l, r);
elem.push(c);
} else {
Expression *e = new IdentifierExp(exp[x]);
elem.push(e);
}
}
if (y == 0) {
Expression *e = new IdentifierExp(exp[x]);
return e;
}
return elem.pop();
}
/*中缀转后缀*/
QStringList *statement::infixToSuffi(QStringList &exp) {
QVector<QString> num;
QStack<QString> op;
QString tmp;
QStringList *suffi;
suffi = new QStringList;
int index = 0;
int count = 0;
bool flag(false);
if (exp.size() == 1) return &exp;
if (exp.contains("=")) {
flag = true;
index = 2;
}
while (index < exp.size()) {
if (exp[index] == "(") {
count++;
op.push(exp[index]);
index++;
} else if (isOperator(exp[index])) {
if (op.empty()) {
op.push(exp[index]);
index++;
} else {
while (!op.empty()) {
tmp = op.top();
if (getPrioraty(exp[index]) <= getPrioraty(tmp)) {
suffi->push_back(tmp);
op.pop();
} else
break;
}
op.push(exp[index]);
index++;
}
} else if (exp[index] == ")") {
count--;
while (op.top() != "(") {
suffi->push_back(op.top());
op.pop();
}
op.pop();
index++;
} else {
suffi->push_back(exp[index]);
index++;
}
}
while (!op.empty()) {
suffi->push_back(op.top());
op.pop();
}
if (count != 0) {
return nullptr;
}
if (flag) {
suffi->push_front(exp[0]);
suffi->push_back(exp[1]);
}
return suffi;
}