-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslator.java
More file actions
259 lines (210 loc) · 9.41 KB
/
Translator.java
File metadata and controls
259 lines (210 loc) · 9.41 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// Translator is responsible for breaking down the expression into its components and using PEMDAS to decide the next sub-expression to evaluate
public class Translator {
private ArrayList<String> _calculations;
private String _currentExpression;
public Translator(String userInput) {
// Assign values to private fields
this._calculations=new ArrayList<String>();
this._currentExpression=userInput.replace("\\s", "");
}
// Getter methods to access private fields
public ArrayList<String> getCalculations() {
// Returns the current list of calculations to eventually be sent to formatter in main Calculator class
return this._calculations;
}
public String getCurrentExpression() {
// Gets current expression in order to access the private field from the outside Calculator object
return this._currentExpression;
}
public Integer countOperators(String expression){
int count = 0;
//expression.charAt(1);
for (int i = 0; i < expression.length(); i++){
if ((expression.charAt(i) + "").equals("+") || (expression.charAt(i) + "").equals("/") ||
(expression.charAt(i) + "").equals("*") || (expression.charAt(i) + "").equals("^")){
count ++;
}
}
return count;
}
public ArrayList<String> parse(String cur_expression){
// parse will convert expression string into list of components
while (true) {
// Check for parentheses
ArrayList<String> parenthesisResult = checkParenthesis(cur_expression);
Boolean hasPara = false;
if (parenthesisResult.get(0).equals("true")) {
cur_expression = (String) parenthesisResult.get(1);
if (countOperators(cur_expression) > 1)
hasPara = false;
else{
hasPara = true;
}
}
// Check for exponentiation
ArrayList<String> exponentResult = checkEMDAS(cur_expression, "^");
if (exponentResult.get(0).equals("true")) {
String resultString = (String) exponentResult.get(1);
int operator_index = resultString.indexOf("^");
ArrayList<String> result = new ArrayList<String>();
String num1 = resultString.substring(0, operator_index);
String num2 = resultString.substring(operator_index + 1, resultString.length());
String operator = resultString.charAt(operator_index) + "";
result.add(operator);
result.add(num1);
result.add(num2);
if (hasPara){
resultString = "(" + resultString + ")";
result.add(resultString);
}
else{
result.add(resultString);
}
return result;
}
// Check for multiplication and division
ArrayList<String> multiplicationResult = checkEMDAS(cur_expression, "*");
ArrayList<String> divisionResult = checkEMDAS(cur_expression, "/");
if (multiplicationResult.get(0).equals("true")) {
String resultString = (String) multiplicationResult.get(1);
int operator_index = resultString.indexOf("*");
ArrayList<String> result = new ArrayList<String>();
String num1 = resultString.substring(0, operator_index);
String num2 = resultString.substring(operator_index + 1, resultString.length());
String operator = resultString.charAt(operator_index) + "";
result.add(operator);
result.add(num1);
result.add(num2);
if (hasPara){
resultString = "(" + resultString + ")";
result.add(resultString);
}
else{
result.add(resultString);
}
return result;
} else if (divisionResult.get(0).equals("true")) {
String resultString = (String) divisionResult.get(1);
int operator_index = resultString.indexOf("/");
ArrayList<String> result = new ArrayList<String>();
String num1 = resultString.substring(0, operator_index);
String num2 = resultString.substring(operator_index + 1, resultString.length());
String operator = resultString.charAt(operator_index) + "";
result.add(operator);
result.add(num1);
result.add(num2);
if (hasPara){
resultString = "(" + resultString + ")";
result.add(resultString);
}
else{
result.add(resultString);
}
return result;
}
// Check for addition and subtraction
ArrayList<String> additionResult = checkEMDAS(cur_expression, "+");
ArrayList<String> subtractionResult = checkEMDAS(cur_expression, "-");
if (additionResult.get(0).equals("true")) {
String resultString = (String) additionResult.get(1);
int operator_index = resultString.indexOf("+");
ArrayList<String> result = new ArrayList<String>();
String num1 = resultString.substring(0, operator_index);
String num2 = resultString.substring(operator_index + 1, resultString.length());
String operator = resultString.charAt(operator_index) + "";
result.add(operator);
result.add(num1);
result.add(num2);
if (hasPara){
resultString = "(" + resultString + ")";
result.add(resultString);
}
else{
result.add(resultString);
}
return result;
}
else if (hasPara){
ArrayList<String> result = new ArrayList<String>();
result.add("+");
result.add((String) parenthesisResult.get(1));
result.add("0");
result.add("(" + (String) parenthesisResult.get(1) + ")");
return result;
}
// No more operations to perform
if (!parenthesisResult.get(0).equals("true") &&
!exponentResult.get(0).equals("true") &&
!multiplicationResult.get(0).equals("true") &&
!divisionResult.get(0).equals("true") &&
!additionResult.get(0).equals("true") &&
!subtractionResult.get(0).equals("true")) {
break;
}
}
// If no operations were found, return an empty string
ArrayList<String> blank = new ArrayList <String>();
blank.add("");
blank.add("");
blank.add("");
blank.add("");
return blank;
}
// PEMDAS Stuff
public ArrayList<String> checkParenthesis(String currentExpresion) {
// checkParanthesis will check for paranthesis in the current expression and return an array with a boolean and string (result)
// If there is a paranthesis, the result will return [True, <the portion of the expression containing the paranthesis>]
// If there isn't, the result will return [False, ""]
ArrayList<String> result = new ArrayList<String>();
result.add("false");
result.add("");
if (!currentExpresion.contains("(")) {
return result;
}
else {
String chunk=currentExpresion;
String ending=chunk.substring(chunk.lastIndexOf("(")+1);
chunk=chunk.substring(chunk.lastIndexOf("(")+1, ending.indexOf(")")+chunk.lastIndexOf("(")+1);
result.set(0, "true");
result.set(1,chunk);
return result;
}
}
public ArrayList<String> checkEMDAS(String currentExpression, String key) {
ArrayList<String> result = new ArrayList<String>();
result.add("false");
result.add("");
if (!currentExpression.contains(key)) {
return result;
}
else {
String chunk=currentExpression;
String[] operations={"+", "*", "/", "^", ")", "("};
List operationsList = Arrays.asList(operations);
int frontIndex=0;
int backIndex=0;
for (int i=chunk.indexOf(key)-1; i>=0; i--) {
if (operationsList.contains(chunk.charAt(i)+"")) {
frontIndex=i+1;
break;
}
}
for (String i:operations) {
if (chunk.substring(chunk.indexOf(key)+1).contains(i)) {
backIndex=chunk.indexOf(i, chunk.indexOf(key)+1);
break;
}
}
if (backIndex==0) {
backIndex=chunk.length();
}
chunk=chunk.substring(frontIndex, backIndex);
result.set(0, "true");
result.set(1,chunk);
return result;
}
}
}