-
Notifications
You must be signed in to change notification settings - Fork 0
/
jCAS.java
1000 lines (896 loc) · 26 KB
/
jCAS.java
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* INPUT FORMAT
* SOLVE:
** solve('x^2=1')
* SIMPLIFY:
** simplify((x+1)*2+3*x-4)
*/
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
/*
* This is the main class which represents the
* entire underlying CAS (Computer Algebra System).
* It is responsible for:
* - creating the AST from infix
* - creating the infix from AST
* - solving, given infix
* - simplifying
*/
public class jCAS {
/*
* Hopefully a debug variable is an acceptable
* use of a global variable, despite their
* general prohibition.
*/
public static boolean debug=false;
private String output="";
public static void main(String[] args) {
jGUI gui = new jGUI();
}
/*
* The constructor for jCAS objects. Acts as essentially a main
* function in that all "main" methods are called from here.
*/
public jCAS(String variables, String infix, boolean display, boolean solve) {
// add spaces between terms
infix = addSpaces(infix);
// build AST from infix
Node root = infixToAST(infix, variables);
String infixVer="";
// continues to simplify AST until fully simplified
while (!infixVer.equals(ASTtoInfix(root, "")))
{
infixVer = ASTtoInfix(root, "");
simplifyAST(root, root);
}
// Differentiates between solve and simplify command-calls
if (solve)
{
List<Double> solution = new ArrayList<Double>();
for (double i = -100; i <= 100; i+=1)
{
double sol = solve(infix, variables, i);
if (solution.indexOf(sol) == -1)
{
solution.add(sol);
}
}
output += variables.charAt(0) + " = [";
for (int i = 0; i < solution.size(); i++)
{
output += solution.get(i);
if (i != solution.size()-1)
output += ", ";
}
output += "] \n";
// safe check for garbage solutions
if (output.length() > 100)
{
output = variables.charAt(0) + " = []";
}
}
else
{
output=infixVer;
}
}
public String getOutput()
{
return output;
}
/*
* Implements Newton's Method to solve the equation a=b
* by finding roots of (a-b), which is solving a-b=0.
*/
public static double solve(String eq, String variables, double guess)
{
/* initial guess */ double x = guess;
/* # of iterations */ int n = 1000;
/* approximately 0 */ double h = 0.0001;
for (int i = 0; i < n; i++)
{
x -= eval(plugIn(eq, x, variables))/deriv(x,h, eq, variables);
}
BigDecimal bd = new BigDecimal(x);
bd = bd.setScale(3, RoundingMode.HALF_UP);
return bd.doubleValue();
}
/*
* The following three methods all are crucial in simplifying
* the expression. They work together to break down the expression.
*
* simplifyAST is the driving force which deals with major simplification
* processes (e.g. distribution, combine like terms, etc.)
*
* canonicalForm makes sure the AST is structured in a predicatable
* manner for ease of simplification. For example, all "x" should be
* "1*x^1" and all "a-b" should be "a + (-1*b)"
*
* constSimp combines constants starting from three locations:
* - const & const
* - const & op
* - op & op
* From these three starting points, the method combines constants
* that are linked across all acceptable ranges (i.e. within same precedence)
*/
public static void simplifyAST(Node node, Node root)
{
if (debug)
{
System.out.println("----------------------------------------");
printAST(root, " ");
System.out.println(ASTtoInfix(root,""));
System.out.println("++++++++++++++++++++++++++++++++++++++++");
printAST(node, " ");
}
canonicalForm(node, root);
// distribution of * over + or -
if (node.dataIs("*"))
{
for (int i = 0; i < node.getChildren().size(); i++)
{
if (node.getChild(i).dataIsOr("+","-"))
{
Node child;
Node dist1 = new Node(new OperatorExpr("*"));
child=new Node();
dist1.addChild(child.setNode(node.getChild((i+1)%2)));
child=new Node();
dist1.addChild(child.setNode(node.getChild(i).getChild(0)));
Node dist2 = new Node(new OperatorExpr("*"));
child=new Node();
dist2.addChild(child.setNode(node.getChild((i+1)%2)));
child=new Node();
dist2.addChild(child.setNode(node.getChild(i).getChild(1)));
Node dist = new Node(new OperatorExpr(node.getChild(i).getData().getData()));
dist.addChild(dist1);
dist.addChild(dist2);
node.setNode(dist);
break;
}
}
}
// combine like terms
// +
// *
// ^
// p
// x
// a
// *
// ^
// p
// x
// b
// (a+b)*x^p
// const simplification (extension of combine like terms for x^0)
if (node.hasChildren())
{
constSimp(node);
}
canonicalForm(node, root);
fillParentNodes(root);
// branch further down and repeat simplification
for (Node child : node.getChildren())
{
simplifyAST(child, root);
}
}
public static void canonicalForm(Node node, Node root)
{
// ensures nonconst*const --> const*nonconst
if (node.dataIs("*"))
{
// constant and variable
if (node.getChild(0).typeIs("const") && !node.getChild(1).typeIs("const"))
{
swapNodes(node.getChild(0), node.getChild(1));
}
}
// convert a-b to a + (-1*b)
else if (node.dataIs("-"))
{
Node child, child2;
Node newSum = new Node(new OperatorExpr("+"));
child=new Node();
newSum.addChild(child.setNode(node.getChild(1)));
child = new Node(new OperatorExpr("*"));
child2=new Node();
child.addChild(child2.setNode(node.getChild(0)));
child2=new Node();
child.addChild(child2.setNode(new Node(new ConstExpr("-1"))));
newSum.addChild(child);
node.setNode(newSum);
}
// x ---> x^1
else if (node.typeIs("variable"))
{
if (!(node.getParent().dataIs("^")))
{
Node child=new Node();
Node newPower = new Node(new OperatorExpr("^"));
newPower.addChild(new Node(new ConstExpr("1")));
newPower.addChild(child.setNode(node));
node.setNode(newPower);
}
}
// ^... ---> 1*^...
else if (node.dataIs("^"))
{
if (node.isRoot() || (!(node.getParent().dataIs("*") && node.getOtherChild().typeIs("const"))))
{
Node child=new Node();
Node newMult = new Node(new OperatorExpr("*"));
newMult.addChild(child.setNode(node));
newMult.addChild(new Node(new ConstExpr("1")));
node.setNode(newMult);
}
}
}
public static void constSimp(Node node)
{
if (!(node.getChild(0).typeIs("variable") && node.getChild(1).typeIs("variable")))
{
// const & const
if (node.getChild(0).typeIs("const") && node.getChild(1).typeIs("const"))
{
double result = operate(node.getData().getData(), node.getChild(1).getData().getData(), node.getChild(0).getData().getData());
Node res = new Node(new ConstExpr(toString(result)));
node.setNode(res);
}
// const & op
else if (node.hasChildOfType("const") && node.hasChildOfType("operator"))
{
if (node.getChildOfType("operator").sameData(node))
{
String const1 = node.getChildOfType("const").getData().getData();
String op = node.getData().getData();
Node tempNode = new Node();
tempNode.setNode(node.getChildOfType("operator"));
ArrayList<Node> checkpointNodes = new ArrayList<Node>();
Node newNode = new Node();
while (tempNode.hasChildOfType("const") || tempNode.hasChildOfType("operator"))
{
if (debug)
{
System.out.println("================================");
printAST(tempNode, " ");
}
if (tempNode.hasChildOfType("const"))
{
Node res = new Node(new OperatorExpr(op));
Node child;
double result = operate(op, const1, tempNode.getChildOfType("const").getData().getData());
child = new Node(new ConstExpr(toString(result)));
res.addChild(child);
Node child2 = new Node();
fillParentNodes(node);
child2.setNode(tempNode.getChildOfType("const").getOtherChild());
fillParentNodes(node);
while (!tempNode.getOtherChild().dataIs(const1))
{
Node tempChild2 = new Node(new OperatorExpr(op));
newNode = new Node();
tempChild2.addChild(newNode.setNode(child2));
newNode = new Node();
tempChild2.addChild(newNode.setNode(tempNode.getOtherChild()));
newNode = new Node();
child2.setNode(newNode.setNode(tempChild2));
newNode = new Node();
tempNode.setNode(newNode.setNode(tempNode.getParent()));
}
newNode = new Node();
res.addChild(newNode.setNode(child2));
newNode = new Node();
node.setNode(newNode.setNode(res));
break;
}
else if (tempNode.hasChildOfType("operator"))
{
if (tempNode.getChild(0).getData().getData().equals(op) && tempNode.getChild(1).getData().getData().equals(op))
{
newNode = new Node();
checkpointNodes.add(newNode.setNode(tempNode.getChild(1)));
newNode = new Node();
tempNode.setNode(newNode.setNode(tempNode.getChild(0)));
}
else
{
if (tempNode.getChild(0).getData().getData().equals(op))
{
newNode = new Node();
tempNode.setNode(newNode.setNode(tempNode.getChild(0)));
}
else if (tempNode.getChild(1).getData().getData().equals(op))
{
newNode = new Node();
tempNode.setNode(newNode.setNode(tempNode.getChild(1)));
}
else
{
if (checkpointNodes.size() > 0)
{
newNode = new Node();
tempNode.setNode(newNode.setNode(checkpointNodes.get(0)));
checkpointNodes.remove(0);
}
else
{
break;
}
}
}
}
}
}
}
// op & op
else if (node.getChild(0).typeIs("operator") && node.getChild(1).typeIs("operator"))
{
if (node.getChild(0).sameData(node) && node.getChild(1).sameData(node))
{
boolean operable = false;
for (int i = 0; i < node.getChildren().size(); i++)
{
String op = node.getData().getData();
Node tempNode = new Node();
tempNode.setNode(node.getChild(i));
ArrayList<Node> checkpointNodes = new ArrayList<Node>();
Node newNode = new Node();
while (tempNode.hasChildOfType("const") || tempNode.hasChildOfType("operator"))
{
if (debug)
{
System.out.println("================================");
printAST(tempNode, " ");
}
if (tempNode.hasChildOfType("const"))
{
Node res = new Node(new OperatorExpr(op));
Node child = new Node();
child.setNode(newNode.setNode(tempNode.getChildOfType("const")));
res.addChild(child);
Node child2 = new Node();
fillParentNodes(node);
child2.setNode(tempNode.getChildOfType("const").getOtherChild());
fillParentNodes(node);
while (!tempNode.getOtherChild().isSame(node.getChild(i).getOtherChild()))
{
Node tempChild2 = new Node(new OperatorExpr(op));
newNode = new Node();
tempChild2.addChild(newNode.setNode(child2));
newNode = new Node();
tempChild2.addChild(newNode.setNode(tempNode.getOtherChild()));
newNode = new Node();
child2.setNode(newNode.setNode(tempChild2));
newNode = new Node();
tempNode.setNode(newNode.setNode(tempNode.getParent()));
}
newNode = new Node();
res.addChild(newNode.setNode(child2));
newNode = new Node();
node.getChild(i).setNode(newNode.setNode(res));
if (i == 0)
{
operable=true;
}
else
{
operable=operable&&true;
}
break;
}
else if (tempNode.hasChildOfType("operator"))
{
if (tempNode.getChild(0).getData().getData().equals(op) && tempNode.getChild(1).getData().getData().equals(op))
{
newNode = new Node();
checkpointNodes.add(newNode.setNode(tempNode.getChild(1)));
newNode = new Node();
tempNode.setNode(newNode.setNode(tempNode.getChild(0)));
}
else
{
if (tempNode.getChild(0).getData().getData().equals(op))
{
newNode = new Node();
tempNode.setNode(newNode.setNode(tempNode.getChild(0)));
}
else if (tempNode.getChild(1).getData().getData().equals(op))
{
newNode = new Node();
tempNode.setNode(newNode.setNode(tempNode.getChild(1)));
}
else
{
if (checkpointNodes.size() > 0)
{
newNode = new Node();
tempNode.setNode(newNode.setNode(checkpointNodes.get(0)));
checkpointNodes.remove(0);
}
else
{
operable = operable && false;
break;
}
}
}
}
}
}
if (operable)
{
double result = operate(node.getData().getData(), node.getChild(0).getChild(0).getData().getData(),
node.getChild(1).getChild(0).getData().getData());
Node res = new Node(new OperatorExpr(node.getData().getData()));
Node newNode;
newNode = new Node();
Node child1 = new Node(new ConstExpr(toString(result)));
res.addChild(newNode.setNode(child1));
Node child = new Node(new OperatorExpr(node.getData().getData()));
newNode = new Node();
child.addChild(newNode.setNode(node.getChild(0).getChild(1)));
newNode = new Node();
child.addChild(newNode.setNode(node.getChild(1).getChild(1)));
newNode = new Node();
res.addChild(newNode.setNode(child));
newNode = new Node();
node.setNode(newNode.setNode(res));
}
}
}
}
}
/*
* These methods are responsible for converting the inputed
* infix to the representative AST. This process is complex
* and involves many steps. A concise summary of the process
* is that it first converts the infix into reverse polish
* notation. Then, then reverse polish expression is converted
* into the AST. An understanding of reverse polish notation
* will explain how this works. Unfortunately, that is beyond
* the scope of this comment.
*/
public static Node infixToAST(String infix, String variables)
{
Queue<String> revPol = infToPost(infix);
Node root = createAST(revPol, variables);
canonicalForm(root, root);
simplifyAST(root, root);
return root;
}
public static Node createAST(Queue<String> revPol, String variables)
{
Stack<Node> nodeStk = new Stack<>();
for (String tkn : revPol)
{
// number (constant or variable)
if (getPrec(tkn) == -1)
{
Node num;
if (variables.indexOf(tkn) >= 0)
num = new Node(new VariableExpr(tkn));
else
num = new Node(new ConstExpr(tkn));
nodeStk.push(num);
}
// operator
else
{
Node op = new Node(new OperatorExpr(tkn));
Node child;
for (int i=0; i<2; i++)
{
child = nodeStk.pop();
op.addChild(child);
child.setParent(op);
}
nodeStk.push(op);
}
}
return nodeStk.peek();
}
public static Queue<String> infToPost(String infix)
{
Stack<String> opStk = new Stack<>();
Queue<String> output = new LinkedList<>();
for(String tkn : infix.split("\\s"))
{
if (tkn.isEmpty())
continue;
int prec = getPrec(tkn);
// operator or (
if (prec > 0)
{
if (opStk.isEmpty())
opStk.push(tkn);
else
{
// if other operators have higher precedence, then add them to stack
// NOTE: Disregard ( as it is not technically an operator
while ((!opStk.isEmpty()) && (!opStk.peek().equals("(")) && ((getPrec(opStk.peek()) > prec) || (getPrec(opStk.peek()) == prec && !tkn.equals("^"))))
{
output.add(opStk.pop());
}
opStk.push(tkn);
}
}
// (
else if (prec == 0)
{
// while top operator is not (
while (!opStk.peek().equals("("))
{
output.add(opStk.pop());
}
// pop and discard the ( then
// do nothing with the )
opStk.pop();
}
// number
else if (prec == -1)
{
output.add(tkn);
}
}
// clear out remaining operators
while (!opStk.isEmpty())
{
output.add(opStk.pop());
}
return output;
}
/*
* The deriv method simply computes the derivative of a given
* expression, eq, at the point x. It is approximated by taking
* h to be very small to simulate lim h ---> 0.
*/
public static double deriv(double x, double h, String eq, String variables)
{
return (eval(plugIn(eq, x+h, variables)) - eval(plugIn(eq, x, variables)))/h;
}
/*
* The eval method is a Recursive Descent Parser. It uses
* the order of operations to solve a mathematical expression.
* eval is used (unsurprisingly) as an evaluation method, akin
* to f(x). It is crucial in computing the derivative
*/
public static double eval(String eq) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < eq.length()) ? eq.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < eq.length()) throw new RuntimeException("Unexpected: " + (char)ch);
return x;
}
// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)`
// | number | functionName factor | factor `^` factor
double parseExpression() {
double x = parseTerm();
for (;;) {
if (eat('+')) x += parseTerm(); // addition
else if (eat('-')) x -= parseTerm(); // subtraction
else return x;
}
}
double parseTerm() {
double x = parseFactor();
for (;;) {
if (eat('*')) x *= parseFactor(); // multiplication
else if (eat('/')) x /= parseFactor(); // division
else return x;
}
}
double parseFactor() {
if (eat('+')) return parseFactor(); // unary plus
if (eat('-')) return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(eq.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z') nextChar();
String func = eq.substring(startPos, this.pos);
x = parseFactor();
} else {
throw new RuntimeException("Unexpected: " + (char)ch);
}
if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation
return x;
}
}.parse();
}
/*
* This method returns a string of the mathematical
* expression, eq, with all the variables replaced with
* the given double value, x.
*
* For example:
* (x+5) * (x-3)
* goes to:
* (1+5) * (1-3)
* when Double x == 1.
*/
public static String plugIn(String eq, Double x, String variables)
{
String newEq="";
for (int i = 0; i < eq.length(); i++)
{
if (eq.charAt(i)==variables.charAt(0))
{
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(8);
newEq+="("+df.format(x)+")";
}
else
{
newEq+=eq.charAt(i);
}
}
return newEq;
}
/*
* In the attempt to avoid problems with pointers
* and unwanted references in java, almost all nodes
* are constructed as new nodes with same information.
* As a result, they are no longer a part of the AST
* and exist instead separately. To put them into the tree
* in their respective positions, the fillParentNodes() method
* must occasionally be called.
*/
// fills all nodes with correct parents starting from "node"
public static void fillParentNodes(Node node)
{
for (Node child : node.getChildren())
{
child.setParent(node);
if (!child.getChildren().isEmpty())
{
fillParentNodes(child);
}
}
}
/*
* These "to..." methods and swapNodes are self-explanatory
*/
public static String toString(Double d)
{
return Double.toString(d);
}
public static double toDouble(String s)
{
BigDecimal bd = new BigDecimal(Double.parseDouble(s));
bd = bd.setScale(3, RoundingMode.HALF_UP);
return bd.doubleValue();
}
public static void swapNodes(Node a, Node b)
{
Node temp = new Node();
temp.setNode(a);
a.setNode(b);
b.setNode(temp);
}
/*
* toInfix converts a given AST to its respective
* infix. This is a complex process due to traditional
* mathematical nomenclature. It contains many arbitrarily
* ugly rules which don't implement nicely into computers,
* hence the excessive if-else statements.
*/
public static String toInfix(String infix, Node node)
{
if (!node.typeIs("operator"))
{
if (node.getParent() != null)
{
if (!(node.getParent().dataIsOr("*", "/") && node.dataIsIn(new String[]{"1", "1.0", "-1", "-1.0"})))
{
if (!(node.getParent().dataIsOr("+","-") && node.dataIsOr("0", "0.0")))
{
if (node.getData().getData().charAt(0)=='-' && infix.length()>=2)
{
if (infix.charAt(infix.length()-2)=='+')
infix = infix.substring(0, infix.length()-2);
}
infix+=node.getData().getData() + " ";
}
else if (node.dataIsOr("0", "0.0"))
{
if (infix.length()>=2)
{
if (infix.charAt(infix.length()-2)=='+' || infix.charAt(infix.length()-2)=='-')
infix = infix.substring(0, infix.length()-2);
}
}
}
else if (node.dataIsOr("-1", "-1.0"))
{
if (infix.length()>=2)
{
if (infix.charAt(infix.length()-2)=='+')
infix = infix.substring(0, infix.length()-2);
}
infix += "- ";
}
}
}
else
{
boolean needParens=true;
if (node.isRoot())
{
needParens=false;
}
else
{
if (node.comparePrec(node.getParent()) > 0)
{
needParens=false;
}
else if (node.comparePrec(node.getParent()) == 0)
{
if (node.getParent().getChild(node.getParent().getChildren().size()-1) == node)
{
needParens=false;
}
if (!node.getParent().dataIs("-"))
{
needParens=false;
}
}
}
if (needParens)
infix += "( ";
for (int i = node.getChildren().size() - 1; i >= 0; i--)
{
infix = toInfix(infix, node.getChild(i));
// not first child
if (i != 0)
{
if (!(node.getChild(i-1).dataIsOr("-1","-1.0") || (node.dataIs("*")&&node.getChild(1).dataIsIn(new String[]{"1", "1.0", "-1", "-1.0"}))))
infix += node.getData().getData() + " ";
// else if (node.getChild(i-1).dataIsOr("-1","-1.0"))
// infix += "- ";
}
}
if (needParens)
infix += ") ";
}
return infix;
}
/*
* getPrec is an attempt at enforcing the rules
* of order of operations whilst reading the infix
*/
public static int getPrec(String op)
{
Map<String, Integer> ops = new HashMap<String, Integer>();
ops.put("(", 100); ops.put(")", 0); ops.put("+", 2); ops.put("-", 2); ops.put("*", 3); ops.put("/", 3); ops.put("^", 4);
if (ops.get(op) != null)
return ops.get(op);
else
return -1;
}
/*
* printAST simply provides a visual representation of the
* AST, which is useful for debugging
*/
public static void printAST(Node node, String appender)
{
// default appender should be " "
System.out.println(appender + node.getData().getData());
for (Node child : node.getChildren())
{
printAST(child, appender+appender);
}
}
public static String ASTtoInfix(Node node, String delim)
{
String s = toInfix("", node);
String[] rev = s.split(" ");
return String.join(delim, rev);
}
/*
* operate is an important part of the constSimp method
* in generating actual results from mathematical operations
* on two doubles, a and b.
*/
public static double operate(String op, String a, String b)
{
// returns result from operation op on operands a and b
if (op.equals("+"))
return toDouble(a) + toDouble(b);
else if (op.equals("-"))
return toDouble(a) - toDouble(b);
else if (op.equals("*"))
return toDouble(a) * toDouble(b);
else if (op.equals("/"))
return toDouble(a) / toDouble(b);
else if (op.equals("^"))
return Math.pow(toDouble(a), toDouble(b));
else
return -1;
}
/*
* This method allows the user to type in:
* a+b-(c+d)
*
* The method will then transform this into:
* a + b - ( c + d )
* for use by the program
*/
public static String addSpaces(String infix)
{
String inf = String.join("", infix.split(" "));
String newInfix="" + inf.charAt(0);
if (inf.charAt(0)=='(')
{
newInfix+=" ";
}
for (int i = 1; i < inf.length(); i++)
{
if (isOperator(inf.charAt(i)))
{
if (!isOperator(inf.charAt(i-1)))
{
newInfix += " ";
}
newInfix += inf.charAt(i) + " ";
}
else
{
newInfix += inf.charAt(i);
}
}
return newInfix;
}
/*
* Helper method to addSpaces
*/
public static boolean isOperator(char c)
{
return (c == '-') || (c == '+') || (c == '*') || (c == '/') || (c == '^') || (c == '(') || (c == ')');
}
/*
* This method is a useful helper method for converting
* the infix to AST, discussed above.
*/
public static String queueToString(Queue<String> q)
{
String s = "";
for (String i : q)
s += i + ", ";
return s;
}
}