This repository was archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.base.h
More file actions
726 lines (581 loc) · 24.6 KB
/
engine.base.h
File metadata and controls
726 lines (581 loc) · 24.6 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#ifndef DERIVATIVE_H
#define DERIVATIVE_H
#include <cmath>
/* The method splits expression into arrays of string term. */
array<string> splitTerm(string expr);
/* The methood derivatives the expression. */
string diffExpr(array<string> terms, const char &var);
/* The methood derivatives a term. */
string diff(const string &term, const char &var);
/* The method finds tangent of expression */
string tangent(string expr, double posX, const char &var);
/* The method shows graph of the expression */
void showGraph(const string &expr, const double &scale, const char &var);
/* The method evaluates the expression. */
double evalExpr(array<string> terms, const double &value, const char &var);
/* The method evaluates a term. */
double eval(string term, const double &value, const char &var);
/* The method implicitly derivatives the expression. */
string implExprDiff(array<string> rightTerms, array<string> leftTerms, const char &var);
void mulMinusN(string &expr);
const double PI = 3.14159265358979323846;
const double e = 2.71828;
struct factor {
unsigned type = 0; // 0 = u, 1 = trig, 2 = log, 3 = arc, 4 = var, 5 = n of var, 6 abs
string func = ""; // log10, asin, tan
string n = "1";
string u = "";
/* The method subtracts power of n by -1 of any form, and returns the subtracted number. */
double subtractN(string &receiver, const char &var='x') {
if (n == "1") return 1;
int divIndex = n.search("/");
if (divIndex != -1) {
double upper = parseNum(n.slice(0, divIndex));
double lower = parseNum(n.slice(divIndex+1));
receiver = "^(" + toString(upper-lower) + "/" + toString(lower) + ")";
return upper / lower;
}
else {
if (parseNum(n)-1 != 1) {
if (n.includes("-") || n.includes("+")) receiver = "^(" + toString(parseNum(n)-1) + ")";
else receiver = "^" + toString(parseNum(n)-1);
}
return parseNum(n);
}
}
/* The method compresses the whole factor into a string factors. */
string compress() {
string resN;
if (n.includes("/") || n.includes("-")) resN = "^(" + n + ")";
else if (n != "1") resN = "^" + n;
if (func.length && n != "1") return "(" + func + "(" + u + "))" + resN;
else if (func.length) return func + "(" + u + ")";
else if (splitTerm(u).length > 1 || (type == 0 && n != "1")) return "(" + u + ")" + resN;
else return u + resN;
}
};
class TermComponents {
public:
string src;
array<factor> factors;
double a = 1;
string otherVar = "";
TermComponents(string term, char var);
/* The method compresses all factors into one string */
string compressAll();
private:
/* the start position of 'i' should be the position of '(' + 1 */
string collectParEl(unsigned short &i);
/* checkingPos should be the index position of '^' */
bool collectN(unsigned short &i, string &receiver);
};
TermComponents::TermComponents(string term, char var) {
src = term;
bool trackDiv = false;
if (term[0] == '-') a = -1;
for (unsigned short i = a == -1; i < term.length; i++) {
// find (type): c number.
if (isNum(term[i])) {
if (term[i] == '0' && term[i+1] != '.') error("lead 0 in context");
unsigned startpos = i;
while (isNum(term[i+1]) && i < term.length) i++;
if (term[i+1] == '^') {
factor item;
item.type = 5;
item.u = term.slice(startpos, i+1);
collectN(++i, item.n);
factors.push(item);
}
else {
a *= trackDiv ? 1/parseNum(term.slice(startpos, i+1)) : parseNum(term.slice(startpos, i+1));
trackDiv = false;
}
}
// find (type): e number.
else if (term[i] == 'e') {
factor item;
if (collectN(++i, item.n)) {
item.type = 5;
item.u = "e";
factors.push(item);
}
else {
a *= trackDiv ? 1/2.71828182845904523 : 2.71828182845904523;
trackDiv = false;
}
}
// find (type): trigonometric function.
else if ((term[i] == 's' || term[i] == 'c' || term[i] == 't') && i+5 < term.length) {
string tfunc = term.slice(i, i + 3);
if (tfunc == "sin" || tfunc == "cos" || tfunc == "tan" || tfunc == "csc" || tfunc == "sec" || tfunc == "cot") {
factor item;
i += 3; // skip 'sin...'
if (collectN(i, item.n)) { // find: a*sin^n(u) d
while (term[i++] != '(');
item.u = tfunc + "(" + collectParEl(i) + ")";
}
else { // find: a*sin(u) or a*sin^1(u)
item.type = 1;
item.func = tfunc;
item.u = collectParEl(i+=2);
}
factors.push(item);
}
else error("none standard arithmatic expression presented");
}
// find (type): logarithm function
else if (term[i] == 'l' && i + 4 < term.length) {
factor item;
unsigned startlog = i;
while (term[i] != '^' && term[i] != '(') i++;
string lfunc = term.slice(startlog, i);
if (!lfunc.includes("log") && !lfunc.includes("ln")) error("none standard arithmatic expression presented");
if (term[i] == '^' && collectN(i, item.n)) { // logb^n
while (term[i++] != '(');
item.u = lfunc + "(" + collectParEl(i) + ")";
}
else {
item.type = 2;
item.func = lfunc;
item.u = collectParEl(++i);
}
factors.push(item);
}
// find (type): inverse trigon function
else if (term[i] == 'a') {
string tfunc = term.slice(i, i+4);
if (tfunc == "asin" || tfunc == "acos" || tfunc == "atan" || tfunc == "acsc" || tfunc == "asec" || tfunc == "acot") {
factor item;
i += 4; // skip 'asin...'
if (collectN(i, item.n)) { // find: a*sin^n(u)
while (term[i++] != '(');
item.u = tfunc + "(" + collectParEl(i) + ")";
}
else { // find: a*sin(u) or a*sin^1(u)
item.type = 3;
item.func = tfunc;
item.u = collectParEl(i+=2);
}
factors.push(item);
}
else error("none standard arithmatic expression presented");
}
// find (type): var
else if (term[i] == var) {
factor item;
item.type = 4;
item.u = term[i];
if(!collectN(++i, item.n));
else if (item.n.includes(var) && item.u.includes(var)) error("there's no support for calculation of u^u", 2);
factors.push(item);
}
else if (term[i] >= 97 && term[i] <= 122) {
otherVar += string(term[i]);
string nPlaceHolder = "";
if (collectN(++i, nPlaceHolder))
otherVar += "^" + nPlaceHolder;
}
// find (type): function inside '(...)'
else if (term[i] == '(') {
factor item;
item.u = collectParEl(++i);
if(!collectN(++i, item.n) && splitTerm(item.u).length <= 1 && !trackDiv && term[i+1] != '/') {
TermComponents preTc(item.u, var);
a *= preTc.a;
for (unsigned short j = 0; j < preTc.factors.length; j++) {
factors.push(preTc.factors[j]);
}
}
else factors.push(item);
}
// find (type): abs
else if (term[i] == '|') {
factor item;
unsigned startpos = ++i;
while (term[i] != '|') i++;
item.type = 6;
item.u = term.slice(startpos, i);
factors.push(item);
}
// find (spliter): division
else if (term[i] == '/') {
trackDiv = true;
continue;
}
else error("error in expression checking");
// finalize end round
if (trackDiv && factors.length) {
mulMinusN(factors[factors.length - 1].n);
trackDiv = false;
}
}
}
string TermComponents::collectParEl(unsigned short &i) {
unsigned short l = 1, r = 0, start = i;
while (i < src.length) {
if (src[i] == '(') l++;
else if (src[i] == ')' && l == ++r) {
if (i - start < 1) error("no element inside parentheses '(...)'");
return src.slice(start, i);
}
i++;
}
return "";
}
bool TermComponents::collectN(unsigned short &i, string &receiver) {
if (src[i] == '^') {
if (src[++i] == '(') receiver = collectParEl(++i);
else {
if (src[i] == '0') error("using power of 0");
if (src[i] == '/') error("no power of n after '^...'");
unsigned startpos = i;
if (isNum(src[i])) {
while (i < src.length && isNum(src[i])) i++;
receiver = src.slice(startpos, i--);
}
else { // only single var
receiver = src[i];
}
}
return true;
}
i--;
return false;
}
string TermComponents::compressAll() {
string result = "";
for (unsigned short i = 0; i < factors.length; i++) {
result += factors[i].compress();
}
return result;
}
/* ################################ */
/* ################################ */
array<string> splitTerm(string expr) {
array<string> terms;
unsigned leftPar = 0, rightPar = 0, abs = 0;
// reading equation process
unsigned splitIndex = 0;
for (unsigned i = (expr[0] == '+' || expr[0] == '-'); i < expr.length; i++) {
if (expr[i] == '(') leftPar++;
else if (expr[i] == ')') rightPar++;
else if (expr[i] == '|') abs++;
if ((expr[i] == '+' || expr[i] == '-') && expr[i - 1] != '^' && leftPar == rightPar && abs % 2 == 0) {
terms.push(expr.slice(splitIndex, i));
splitIndex = i + (expr[i] == '+' ? 1 : 0);
}
}
// collect last term
terms.push(expr.slice(splitIndex));
// check for errors
if (leftPar != rightPar) error("no complete pair of parentheses '()'");
return terms;
}
string diffExpr(array<string> terms, const char &var) {
string result = "";
for (unsigned i = 0; i < terms.length; i++) {
string preResult = diff(terms[i], var);
if (preResult.includes("#"));
else {
if (i > 0 && preResult[0] != '-' && result.length) result += "+";
result += preResult;
}
}
return (!result.length ? "0" : result);
}
string diff(const string &term, const char &var) {
TermComponents tc(term, var);
if (!tc.factors.length) return "#"; // only c
string result = "";
if (tc.factors.length > 1) {
for (unsigned i = 0; i < tc.factors.length; i++) {
string termRes = "";
double diffA = 1;
bool skipZero = false;
for (unsigned j = 0; j < tc.factors.length; j++) {
string preRes = i == j ? diff(tc.factors[j].compress(), var) : tc.factors[j].compress();
if (preRes == "0" || preRes.includes("#")) {
skipZero = true;
break;
}
if (splitTerm(preRes).length <= 1) {
TermComponents preTc(preRes, var);
diffA *= preTc.a;
preRes = preTc.compressAll();
}
else preRes = "(" + preRes + ")";
termRes += preRes;
}
// finalize terms
if (!skipZero) {
if (i > 0 && tc.a * diffA > 0 && result.length) result += "+";
result += toCalStr(tc.a * diffA) + termRes;
}
}
}
else if (tc.factors.length == 1) {
// for format result
string preRes = "";
string chainDiff = "";
if (tc.factors[0].type != 4 && tc.factors[0].type != 5) {
chainDiff = diffExpr(splitTerm(tc.factors[0].u), var);
if (chainDiff == "0" || chainDiff.includes("#")) return "#";
if (splitTerm(chainDiff).length <= 1) {
TermComponents preTc(chainDiff, var);
tc.a *= preTc.a;
chainDiff = preTc.compressAll();
}
else chainDiff = "(" + chainDiff + ")";
}
// redering result by cases
switch (tc.factors[0].type) {
case 0: {
string subN = "";
// format result
tc.a *= tc.factors[0].subtractN(subN);
if (splitTerm(tc.factors[0].u).length <= 1 && !subN.length) {
TermComponents preTc(tc.factors[0].u, var);
tc.a *= preTc.a;
preRes += preTc.compressAll();
}
else preRes += "(" + tc.factors[0].u + ")" + subN;
result = tc.factors[0].n == "1"
? toCalStr(tc.a) + chainDiff
: toCalStr(tc.a) + preRes + chainDiff;
} break;
case 1: {
string diffTrigon1, diffTrigon2;
if (tc.factors[0].func == "cos" || tc.factors[0].func == "cot" || tc.factors[0].func == "csc") tc.a *= -1;
if (tc.factors[0].func == "sin" || tc.factors[0].func == "cos" || tc.factors[0].func == "tan" || tc.factors[0].func == "cot") {
if (tc.factors[0].func == "sin") diffTrigon1 = "cos";
else if (tc.factors[0].func == "cos") diffTrigon1 = "sin";
else if (tc.factors[0].func == "tan") diffTrigon1 = "sec^2";
else if (tc.factors[0].func == "cot") diffTrigon1 = "csc^2";
result = toCalStr(tc.a) + diffTrigon1 + "(" + tc.factors[0].u + ")" + chainDiff;
}
else {
if (tc.factors[0].func == "csc") {
diffTrigon1 = "csc";
diffTrigon2 = "cot";
}
else if (tc.factors[0].func == "sec") {
diffTrigon1 = "sec";
diffTrigon2 = "tan";
}
result = toCalStr(tc.a) + diffTrigon1 + "(" + tc.factors[0].u + ")" + diffTrigon2 + "(" + tc.factors[0].u + ")" + chainDiff;
}
} break;
case 2: {
if (tc.factors[0].func.length > 2) { // log...
string logbase = tc.factors[0].func.length == 3 ? "10" : tc.factors[0].func.slice(3);
if (splitTerm(tc.factors[0].u).length <= 1) {
TermComponents preTc(tc.factors[0].u, var);
tc.a /= preTc.a;
preRes = preTc.compressAll();
}
else preRes = "(" + tc.factors[0].u + ")";
preRes = "(" + preRes + "ln(" + logbase + "))";
}
else { // ln...
preRes = tc.factors[0].u.length > 1
? "(" + tc.factors[0].u + ")"
: tc.factors[0].u;
}
result = !chainDiff.length && tc.a == 1
? "1/" + preRes
: toCalStr(tc.a) + chainDiff + "/" + preRes;
} break;
case 3: {
if (tc.factors[0].func == "acos" || tc.factors[0].func == "acot" || tc.factors[0].func == "acsc") tc.a *= -1;
result = !chainDiff.length && tc.a == 1
? toString(tc.a) + "/"
: toCalStr(tc.a) + chainDiff + "/";
if (tc.factors[0].u.length == 1 || isAllNum(tc.factors[0].u)) preRes = tc.factors[0].u;
else preRes = "(" + tc.factors[0].u + ")";
if (tc.factors[0].func == "asin" || tc.factors[0].func == "acos") result += "((1-" + preRes + "^2)^(1/2))";
else if (tc.factors[0].func == "atan" || tc.factors[0].func == "acot") result += "(1+" + preRes + "^2)";
else if (tc.factors[0].func == "asec" || tc.factors[0].func == "acsc") result += "(|" + tc.factors[0].u + "|(" + preRes + "^2-1)^(1/2))";
} break;
case 4: { // CASE: ax^n or ax^(n)
string subN = "";
double numSubN = tc.factors[0].subtractN(subN);
result = numSubN == 1
? toString(tc.a)
: toCalStr(tc.a * numSubN) + string(var) + subN;
} break;
case 5: { // CASE a^(u)
string chainDiff = diffExpr(splitTerm(tc.factors[0].n), var);
if (chainDiff == "0" || chainDiff.includes("#")) return "#";
string preN = "^" + (tc.factors[0].n.length > 1 || tc.factors[0].n.includes("-") || tc.factors[0].n.includes("/")
? "(" + tc.factors[0].n + ")"
: tc.factors[0].n);
bool hasNone1N = false;
if (splitTerm(chainDiff).length <= 1) {
TermComponents preTc(chainDiff, var);
tc.a *= preTc.a;
chainDiff = preTc.compressAll();
hasNone1N = preTc.factors.length && preTc.factors[0].n != "1";
}
else chainDiff = "(" + chainDiff + ")";
preRes = hasNone1N ? "(" + tc.factors[0].u + preN + ")" : tc.factors[0].u + preN;
result += toCalStr(tc.a) + (tc.factors[0].u == "e"
? chainDiff + preRes
: chainDiff + "ln(" + tc.factors[0].u + ")" + preRes);
} break;
case 6: {
if (splitTerm(tc.factors[0].u).length <= 1) {
TermComponents preTc(tc.factors[0].u, var);
tc.a /= preTc.a;
preRes = preTc.compressAll();
}
else preRes = "(" + tc.factors[0].u + ")";
result = chainDiff + preRes + "/|" + tc.factors[0].u + "|";
} break;
default: error("fault at 'diff' function", 5);
}
}
// finalize
if (result == "1" && tc.otherVar.length) return tc.otherVar;
else return (!result.length ? "#" : result) + tc.otherVar;
}
string tangent(string expr, double posX, const char &var) {
string slopeFunc = diffExpr(splitTerm(expr), var);
double slope = evalExpr(splitTerm(slopeFunc), posX, var);
// tangent = m(x1)(x-x1) + y1;
double c = posX * slope + evalExpr(splitTerm(expr), posX, var);
string cStr = "";
if (c != 0 && c >= 0) cStr = "+" + toString(c);
if (slope == 0 && !cStr.length) return "0";
else if (slope == 0 && cStr.length) return cStr.slice(1);
else return toString(slope) + string(var) + cStr;
}
// 1 > zoom in, 1 < zoom out;
void showGraph(const string &expr, const double &scale, const char &var) {
const unsigned short h = 65, w = 65, mid = 32;
array<string> read = splitTerm(expr);
char graph[h][w] = {};
for (unsigned short i = 0; i < w; i++) {
int y = evalExpr(read, (i-mid)/scale, var) + mid;
if (y >= 0 && y <= h) graph[y][i] = 'o';
}
std::cout << "\t\t";
for (unsigned short i = 0; i < 67; i++ ) std::cout << '#';
std::cout << "\n";
for (unsigned short row = h-1; row >= 1; row--) { // loop y
std::cout << "\t\t#";
for (unsigned short col = 0; col < w; col++) { // loop x
if (graph[row][col] == 'o') {
std::cout << 'o';
}
else {
if (row == mid && col == mid) std::cout << '+';
else if (col == mid) std::cout << '-';
else if (row == mid) std::cout << '-';
else std::cout << ' ';
}
}
if (row == mid) {
std::cout << "# " << var << "\n";
continue;
}
std::cout << "#\n";
}
std::cout << "\t\t";
for (unsigned short i = 0; i < 67; i++ ) std::cout << '#';
std::cout << "\n";
}
double evalExpr(array<string> terms, const double &value, const char &var) {
double result = 0;
for (unsigned short i = 0; i < terms.length; i++) result += eval(terms[i], value, var);
return result;
}
double eval(string term, const double &value, const char &var) {
TermComponents tc(term, var);
if (!tc.factors.length) return tc.a;
double result = tc.a;
for (unsigned short i = 0; i < tc.factors.length; i++) {
switch (tc.factors[i].type) {
case 0: {
double chainEval = evalExpr(splitTerm(tc.factors[i].u), value, var);
double n = evalExpr(splitTerm(tc.factors[i].n), value, var);
result *= n < 0 ? 1/pow(chainEval, -n) : pow(chainEval, n);
} break;
case 1: {
double chainEval = evalExpr(splitTerm(tc.factors[i].u), value, var);
if (tc.factors[i].func == "sin")
result *= sin(chainEval * PI / 180);
else if (tc.factors[i].func == "cos")
result *= cos(chainEval * PI / 180);
else if (tc.factors[i].func == "tan")
result *= tan(chainEval * PI / 180);
else if (tc.factors[i].func == "csc")
result *= 1/sin(chainEval * PI / 180);
else if (tc.factors[i].func == "sec")
result *= 1/cos(chainEval * PI / 180);
else if (tc.factors[i].func == "cot")
result *= 1/tan(chainEval * PI / 180);
} break;
case 2: {
double chainEval = evalExpr(splitTerm(tc.factors[i].u), value, var);
double logbase = tc.factors[i].func.length == 3 ? 10 : parseNum(tc.factors[i].func.slice(3));
result *= tc.factors[i].func.length > 2 ? log(chainEval)/log(logbase) : log(chainEval);
} break;
case 3: {
double chainEval = evalExpr(splitTerm(tc.factors[i].u), value, var);
if (tc.factors[i].func == "asin")
result *= asin(chainEval);
else if (tc.factors[i].func == "acos")
result *= acos(chainEval);
else if (tc.factors[i].func == "atan")
result *= atan(chainEval);
else if (tc.factors[i].func == "acsc")
result *= 1/asin(chainEval);
else if (tc.factors[i].func == "asec")
result *= 1/acos(chainEval);
else if (tc.factors[i].func == "acot")
result *= 1/atan(chainEval);
} break;
case 4: {
double n = evalExpr(splitTerm(tc.factors[i].n), value, var);
result *= n < 0 ? 1/pow(value, -n) : pow(value, n);
} break;
case 5: {
double n = evalExpr(splitTerm(tc.factors[i].n), value, var);
result *= n < 0 ? 1/pow(parseNum(tc.factors[i].u), -n) : pow(parseNum(tc.factors[i].u), n);
} break;
case 6: {
double n = evalExpr(splitTerm(tc.factors[i].n), value, var);
double chainEval = evalExpr(splitTerm(tc.factors[i].u), value, var);
result *= n < 0 ? 1/pow(abs(chainEval), -n) : pow(abs(chainEval), n);
} break;
default: error("fault at 'eval' function");
}
}
return result;
}
string implExprDiff(array<string> leftTerms, array<string> rightTerms, const char &var) {
string result = "";
array<string> forms = leftTerms;
// construct new form left = 0
for (unsigned short i = 0; i < rightTerms.length; i++) {
if (rightTerms[i][0] == '-') forms.push(rightTerms[i].slice(1));
else forms.push("-" + rightTerms[i]);
}
string dUpper, dLower;
dUpper = diffExpr(forms, var);
dLower = diffExpr(forms, 'y');
result = "-(" + dUpper + ")/(" + dLower + ")";
return result;
}
void mulMinusN(string &expr) {
array<string> terms = splitTerm(expr);
string res = "";
for (unsigned short i = 0; i < terms.length; i++) {
if (terms[i][0] == '-' && i == 0) res += terms[i];
else if (terms[i][0] != '-' && i == 0) res += "-" + terms[i];
else if (terms[i][0] == '-') res += "+" + terms[i];
else res += "-" + terms[i];
}
expr = res;
}
#endif