-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslator.java
More file actions
90 lines (83 loc) · 4.08 KB
/
Translator.java
File metadata and controls
90 lines (83 loc) · 4.08 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
public class Translator {
//BinaryTree tree;
static char[] orderOfExecution = {'+', '^', '.', '¬'}; //Go through and find the +s first. This makes the AND operations be done first.
public static String removeOuterBrackets(String eq){
if(eq.length() > 0){
if(eq.charAt(0) == '(' && eq.charAt(eq.length()-1) == ')'){
//Need to make sure there's no more brackets inbetween these two. For example (A.B)+(C.D) will just go to A.B)+(C.D. This is bad.
boolean bracketsBetween = false;
for(int i=1;i<eq.length()-1;i++){
if(eq.charAt(i) == '(' || eq.charAt(i) == ')'){
bracketsBetween = true;
break;
}
}
if(bracketsBetween == false){
eq = eq.substring(1, eq.length()-1);
}
}
}
return eq;
}
public static BinaryTree translate(String eq){
BinaryTree tree = null;
eq = Translator.removeOuterBrackets(eq);
int insideBrackets = 0;
//First go through for +s, then go through for .s ...
//Put break outOfLoop; to get here.
outOfLoop:
for(int i=0; i<orderOfExecution.length;i++){
for(int j=0; j<eq.length();j++){
char character = eq.charAt(j);
if(character == '('){
insideBrackets += 1;
}else if(character == ')'){
insideBrackets -= 1;
}else if(character == '+' && insideBrackets == 0 && orderOfExecution[i] == '+'){
OrNode or = new OrNode();
tree = new BinaryTree(or, translate(eq.substring(0, j)), translate(eq.substring(j+1, eq.length())));
break outOfLoop;
}else if(character == '^' && insideBrackets == 0 && orderOfExecution[i] == '^'){
XorNode xor = new XorNode();
tree = new BinaryTree(xor, translate(eq.substring(0, j)), translate(eq.substring(j+1, eq.length())));
break outOfLoop;
}else if(character == '.' && insideBrackets == 0 && orderOfExecution[i] == '.'){
AndNode and = new AndNode();
System.out.println(eq.substring(0, j));
System.out.println(eq.substring(j+1, eq.length()));
tree = new BinaryTree(and, translate(eq.substring(0, j)), translate(eq.substring(j+1, eq.length())));
BinaryTree.printTree(tree);
break outOfLoop;
}else if(character == '¬' && insideBrackets == 0 && orderOfExecution[i] == '¬'){
NotNode not = new NotNode();
if(eq.charAt(j+1) == '('){
//Add the entire part in brackets to the NOT node.
//If there's more brackets inside the not brackets, you don't just want to check for the next closing bracket.
int insideNotBrackets = 0;
int index = j+2;
while(insideNotBrackets >=0){
char notCharacter = eq.charAt(index);
if(notCharacter == '('){
insideNotBrackets += 1;
}else if(notCharacter == ')'){
insideNotBrackets -= 1;
}
index += 1;
}
tree = new BinaryTree(not, translate(eq.substring(j+1, index)), null);
}else{
tree = new BinaryTree(not, translate(eq.substring(j+1,j+2)), null);
}
break outOfLoop;
}else{
//If it's just A,B etc
LetterNode node = new LetterNode(character);
tree = new BinaryTree(node, null, null);
}
}
}
return tree;
}
public Translator(){
}
}