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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425#include "frac.h"
Frac::Frac(int value, const QString &key) {
if(value != 0)
mMapPoly[key] = value;
}
Frac::Frac(int a, int b) {
if(b == 0)
throw FracError("cannot div 0");
if(a != 0)
mMapPoly[""] = a;
this->b = b;
reduct();
}
Frac::Frac(QString str, bool *ok) {
str.replace(' ', "");
int indexOfDiv = str.indexOf("/");
if(indexOfDiv == -1) {
bool ok2;
int value = str.toInt(&ok2);
if(ok2) {
mMapPoly[""] = value;
} else {
SET_PTR(ok, false);
return;
}
} else {
bool ok2;
int top = str.left(indexOfDiv).toInt(&ok2);
if(!ok2) {
SET_PTR(ok, false);
return;
}
int bottom = str.right(str.length() - indexOfDiv - 1).toInt(&ok2);
if(!ok2 || bottom == 0) {
SET_PTR(ok, false);
return;
}
mMapPoly[""] = top;
b = bottom;
}
reduct();
SET_PTR(ok, true);
}
Frac::Frac(const PlainFrac &plain) {
if(plain.a == 0)
return;
mMapPoly[""] = plain.a;
b = plain.b;
}
PlainFrac Frac::toPlain() {
int a = mMapPoly.value("", 0);
return PlainFrac(a, b);
}
//void Frac::checkZero() {
// QStringList lNeedRemoveMono;
// for(auto iter = mapPoly.begin(); iter != mapPoly.end(); ++iter)
// if(iter.value() == 0)
// lNeedRemoveMono << iter.key();
// for(QString& mono : lNeedRemoveMono)
// mapPoly.remove(mono);
// if(mapPoly.isEmpty())
// b = 1;
//}
//#define DEBUG_FRAC_SOLVINGRQUATIONS
#ifdef DEBUG_FRAC_SOLVINGRQUATIONS
#include <QDebug>
#endif
QList<Frac> Frac::SolvingEquations(const QList<Frac> &lFracs, const QStringList &lUnkNumbers, SolvingError *err) {
return SolvingEquations(lFracs, lUnkNumbers, QStringList(), err);
}
QList<Frac> Frac::SolvingEquations(QList<Frac> lFracs, const QStringList &lUnkNumbers,
const QStringList &lRemoveLetters, SolvingError *err)
{
//对于可以直接判断无解的情况,就结束该函数
if(lUnkNumbers.isEmpty()) {
SET_PTR(err, SolvingError::Unsolvable);
return QList<Frac>();
}
if(lFracs.size() < lUnkNumbers.size()) {
SET_PTR(err, SolvingError::Insufficient);
return QList<Frac>();
}
#ifdef DEBUG_FRAC_SOLVINGRQUATIONS
qDebug().noquote() << "\033[35m初始:\033[0m";
for(Frac &frac : lFracs)
qDebug().noquote() << frac.format(true, true) << "= 0";
#endif
for(const QString &remove : lRemoveLetters) {
//如果未知数中存在该字母,则跳过本次循环
if(lUnkNumbers.contains(remove))
continue;
auto iter = lFracs.end();
//查找第一个该字母出现的位置
for(auto listIter = lFracs.begin(); listIter != lFracs.end(); ++listIter) {
if((*listIter).mMapPoly.contains(remove)) {
iter = listIter;
break;
}
}
//如果无法从现有的式子中找到该字母,就跳过本次循环
if(iter == lFracs.end())
continue;
Frac paramFrac = (*iter).paramSep(remove); //分离该字母
lFracs.erase(iter); //从lFracs中移除该iter
#ifdef DEBUG_FRAC_SOLVINGRQUATIONS
qDebug().noquote() << "\n\033[35m将\033[0m " + remove + " = " + paramFrac.format(true, true) + " \033[35m代入得:\033[0m";
#endif
for(Frac &otherFrac : lFracs) { //将分离的结果代入到其他式子中
#ifdef DEBUG_FRAC_SOLVINGRQUATIONS
qDebug().noquote() << otherFrac.substitute(remove, paramFrac).format(true, true) << "= 0";
#else
otherFrac.substitute(remove, paramFrac);
#endif
}
}
QList<Frac> lRes;
for(const QString &unkNumber : lUnkNumbers) {
auto iter = lFracs.end();
//查找第一个该未知数出现的式子
for(auto listIter = lFracs.begin(); listIter != lFracs.end(); ++listIter) {
if((*listIter).mMapPoly.contains(unkNumber)) {
iter = listIter;
break;
}
}
//如果无法从现有的式子中找到该未知数,就结束该函数
if(iter == lFracs.end()) {
SET_PTR(err, SolvingError::Insufficient);
return QList<Frac>();
}
Frac paramFrac = (*iter).paramSep(unkNumber); //分离该未知数
lRes << paramFrac; //将其加入到结果list中
lFracs.erase(iter); //从lFracs中移除该iter
#ifdef DEBUG_FRAC_SOLVINGRQUATIONS
qDebug().noquote() << "\n\033[35m将\033[0m " + unkNumber + " = " + paramFrac.format(true, true) + " \033[35m代入得:\033[0m";
#endif
for(Frac &otherFrac : lFracs) { //将分离的结果代入到其他式子中
#ifdef DEBUG_FRAC_SOLVINGRQUATIONS
qDebug().noquote() << otherFrac.substitute(unkNumber, paramFrac).format(true, true) << "= 0";
#else
otherFrac.substitute(unkNumber, paramFrac);
#endif
}
}
#ifdef DEBUG_FRAC_SOLVINGRQUATIONS
qDebug() << "\n\033[35m多余项:\033[0m";
for(Frac &frac : lFracs) {
qDebug().noquote() << frac.format(true, true) << "= 0";
}
#endif
for(Frac &frac : lFracs) { //判断是否无解(若有任何一个多余项不为0,就无解)
if(!frac.mMapPoly.isEmpty()) {
SET_PTR(err, SolvingError::Unsolvable);
return QList<Frac>();
}
}
{//往回代入,求出所有的未知数
auto iterUnkNum = lUnkNumbers.rbegin();
for(auto iterGet = lRes.rbegin(); iterGet != lRes.rend(); ++iterGet) {
Frac &cur = *iterGet;
for(auto iterSet = iterGet + 1; iterSet != lRes.rend(); ++iterSet)
(*iterSet).substitute(*iterUnkNum, cur);
++iterUnkNum;
}
}
for(Frac &frac : lRes) //将分母的负号移至分子
frac.moveNegativeToTop();
#ifdef DEBUG_FRAC_SOLVINGRQUATIONS
qDebug().noquote() << "\n\033[35m结果:\033[0m";
{
auto iterUnkNum = lUnkNumbers.begin();
for(auto iter = lRes.begin(); iter != lRes.end(); ++iter) {
qDebug().noquote() << *iterUnkNum << "=" << (*iter).format(true, true);
++iterUnkNum;
}
}
#endif
SET_PTR(err, SolvingError::NoError);
return lRes;
}
void Frac::reduct() {
// checkZero();
if(mMapPoly.isEmpty()) {
b = 1;
return;
}
while(true) {
QVector<int> vValues { b };
for(int value : mMapPoly)
vValues << value;
int divNum = j::Gcd(vValues);
if(divNum == 1 || divNum == 0)
break;
for(int &value : mMapPoly)
value /= divNum;
b /= divNum;
}
}
Frac& Frac::changeMono(const QString &before, const QString &now) {
if(before != now) {
auto iter = mMapPoly.find(before);
if(iter != mMapPoly.end()) {
mMapPoly[now] += *iter;
mMapPoly.erase(iter);
}
}
return *this;
}
Frac Frac::paramSep(const QString ¶m, bool *ok) {
// checkZero();
if(!mMapPoly.contains(param)) {
SET_PTR(ok, false);
return Frac();
}
Frac res = *this;
res.b = 1;
res.mMapPoly.remove(param);
res.div(-mMapPoly[param]);
res.reduct();
SET_PTR(ok, true);
return res;
}
Frac& Frac::substitute(const QString ¶m, int digit) {
auto iter = mMapPoly.find(param);
if(iter != mMapPoly.end()) {
int value = iter.value();
if((mMapPoly[""] += value * digit) == 0)
mMapPoly.remove("");
mMapPoly.erase(iter);
reduct();
}
return *this;
}
Frac& Frac::substitute(const QString ¶m, const Frac &other) {
auto iter = mMapPoly.find(param);
if(iter != mMapPoly.end()) {
int valueMul = iter.value();
mMapPoly.erase(iter);
b *= other.b;
for(int &mono : mMapPoly)
mono *= other.b;
for(auto otherIter = other.mMapPoly.begin(); otherIter != other.mMapPoly.end(); ++otherIter)
if((mMapPoly[otherIter.key()] += otherIter.value() * valueMul) == 0)
mMapPoly.remove(otherIter.key());
reduct();
}
return *this;
}
Frac& Frac::moveNegativeToTop() {
if(b < 0) {
b = -b;
for(int &mono : mMapPoly)
mono = -mono;
}
return *this;
}
QString Frac::format(bool autoSpace, bool useColor) const {
QString res;
int size = mMapPoly.size();
if(size == 0) {
res = "0";
return res;
}
if(b != 1 && size != 1)
res += "(";
bool hasPrev = false;
for(auto iter = mMapPoly.begin(); iter != mMapPoly.end(); ++iter) {
const QString& key = iter.key();
int value = iter.value();
if(hasPrev) {
res += value < 0 ? (autoSpace ? " - " : "-") : (autoSpace ? " + " : "+");
} else {
if(value < 0) res += '-';
hasPrev = true;
}
if(qAbs(value) != 1 || key.isEmpty())
res += QString::number(qAbs(value));
if(!key.isEmpty())
res += (useColor ? "\033[33m[\033[0m" : "[") + key + (useColor ? "\033[33m]\033[0m" : "]");
}
if(b != 1) {
if(size != 1)
res += ")";
res += autoSpace ? " / " : "/";
res += QString::number(b);
}
return res;
}
Frac& Frac::sum(int digit, const QString &key) {
if(digit != 0) {
digit *= b;
if((mMapPoly[key] += digit ) == 0)
mMapPoly.remove(key);
reduct();
}
return *this;
}
Frac& Frac::sub(int digit, const QString &key) {
if(digit != 0) {
digit *= b;
if((mMapPoly[key] -= digit) == 0)
mMapPoly.remove(key);
reduct();
}
return *this;
}
Frac& Frac::mul(int digit) {
if(digit == 0) {
mMapPoly.clear();
b = 1;
} else {
for(int &mono : mMapPoly)
mono *= digit;
reduct();
}
return *this;
}
Frac& Frac::div(int digit) {
if(digit == 0)
throw FracError("cannot div 0");
if(!mMapPoly.isEmpty()) {
b *= digit;
reduct();
}
return *this;
}
Frac& Frac::sum(const Frac &other) {
if(!other.mMapPoly.isEmpty()) {
int commonMulti = j::Lcm(b, other.b);
int selfMulti = commonMulti / b;
int otherMulti = commonMulti / other.b;
b = commonMulti;
for(int &mono : mMapPoly)
mono *= selfMulti;
for(QMap<QString, int>::const_iterator iter = other.mMapPoly.begin(); iter != other.mMapPoly.end(); ++iter)
if((mMapPoly[iter.key()] += iter.value() * otherMulti) == 0)
mMapPoly.remove(iter.key());
reduct();
}
return *this;
}
Frac& Frac::sub(const Frac &other) {
if(!other.mMapPoly.isEmpty()) {
int CommonMulti = j::Lcm(b, other.b);
int selfMulti = CommonMulti / b;
int otherMulti = CommonMulti / other.b;
b = CommonMulti;
for(int &mono : mMapPoly)
mono *= selfMulti;
for(QMap<QString, int>::const_iterator iter = other.mMapPoly.begin(); iter != other.mMapPoly.end(); ++iter)
if((mMapPoly[iter.key()] -= iter.value() * otherMulti) == 0)
mMapPoly.remove(iter.key());
reduct();
}
return *this;
}
Frac& Frac::mul(const PlainFrac &other) {
if(other.a == 0) {
mMapPoly.clear();
b = 1;
} else {
for(int &mono : mMapPoly)
mono *= other.a;
b *= other.b;
reduct();
}
return *this;
}
Frac& Frac::div(const PlainFrac &other) {
if(other.a == 0)
throw FracError("cannot div 0");
for(int &mono : mMapPoly)
mono *= other.b;
b *= other.a;
reduct();
return *this;
}