forked from SiaXD/comp354-project-Team-G
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorInterface_backup.txt
More file actions
483 lines (442 loc) · 16.1 KB
/
CalculatorInterface_backup.txt
File metadata and controls
483 lines (442 loc) · 16.1 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
package project;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CalculatorInterface {
public static void main(String[] args) {
int mainMenuInput = -1;
Scanner sc = new Scanner(System.in);
while (mainMenuInput != 0) {
System.out.print("Please choose one of the following options "
+ "(0 to exit): "
+ "\n\t1- sin(x)"
+ "\n\t2- 10^x"
+ "\n\t3- ln(x)"
+ "\n\t4- e^x"
+ "\n\t5- MAD"
+ "\n\t6- sinh(x)"
+ "\n\t7- x^y"
+ "\n\t8- math arithmetic\n>>>");
mainMenuInput = sc.nextInt();
switch (mainMenuInput) {
case 1:
boolean keepGoing = true;
while (keepGoing) {
System.out.print("Enter value of x: ");
double x = sc.nextDouble();
// replace with calculator function
System.out.println(Sin.sinforR(x));
System.out.print("\nContinue? (y/n)");
String userInput = sc.next();
if (userInput.equals("n")) {
keepGoing = false;
}
}
break;
case 2:
keepGoing = true;
while (keepGoing) {
System.out.print("Enter value of x: ");
double x = sc.nextDouble();
// replace with calculator function
System.out.println(CalculatorFunctions.XtoN(10, x));
System.out.print("\nContinue? (y/n)");
String userInput = sc.next();
if (userInput.equals("n")) {
keepGoing = false;
}
}
break;
case 3:
keepGoing = true;
while (keepGoing) {
System.out.print("Enter value of x: ");
double x = sc.nextDouble();
System.out.println(Ln.ln(x));
System.out.print("\nContinue? (y/n)");
String userInput = sc.next();
if (userInput.equals("n")) {
keepGoing = false;
}
}
break;
case 4:
keepGoing = true;
while (keepGoing) {
System.out.print("Enter value of x: ");
double x = sc.nextDouble();
// replace with calculator function
System.out.println(Exp.EXP(x, 1));
System.out.print("\nContinue? (y/n)");
String userInput = sc.next();
if (userInput.equals("n")) {
keepGoing = false;
}
}
break;
case 5:
keepGoing = true;
while (keepGoing) {
System.out.print("Enter the values, use \",\" to separate: ");
String x = sc.next();
// replace with calculator function
System.out.println(Mean_absolute_deviation.MAD(x));
System.out.print("\nContinue? (y/n)");
String userInput = sc.next();
if (userInput.equals("n")) {
keepGoing = false;
}
}
break;
case 6:
keepGoing = true;
while (keepGoing) {
System.out.print("Enter value of x: ");
double x = sc.nextDouble();
// replace with calculator function
System.out.println(Sinh.sinh(x, false));
System.out.print("\nContinue? (y/n)");
String userInput = sc.next();
if (userInput.equals("n")) {
keepGoing = false;
}
}
break;
case 7:
keepGoing = true;
while (keepGoing) {
System.out.print("Enter value of x: ");
int x = sc.nextInt();
System.out.print("Enter value of y: ");
int y = sc.nextInt();
// replace with calculator function
System.out.println(CalculatorFunctions.xPowY(x, y));
System.out.print("\nContinue? (y/n)");
String userInput = sc.next();
if (userInput.equals("n")) {
keepGoing = false;
}
}
break;
case 8:
keepGoing=true;
String userinput;
while (keepGoing) { // user interface
System.out.print("Write your math equation\n>>>");
userinput = sc.next();
if (userinput.contains("quit")) {
keepGoing = false;
} else
Shunting_yard_algorithm.shunting_yard_algorithm(userinput);
System.out.print("\nContinue? (y/n)");
String userInput = sc.next();
if (userInput.equals("n")) {
keepGoing = false;
}
}
break;
}
}
System.out.println("Sucessfully exited Eternity Calculator");
sc.close();
}
public static boolean isnumber(String a) {
return a.matches("-?\\d+(\\.\\d+)?");
}
public static void shunting_yard_algorithm(String expression){
String filtered = expression.replace(" ", "");
String head="";
int index=0;
ArrayList<String> number_stack = new ArrayList<String>();
ArrayList<String> operation_stack = new ArrayList<String>();
for(int i =0;i<filtered.length();i++) {
head = filtered.substring(i, i+1);
if(head.equals(".") || isnumber(head)) {
number_stack.add(number_stack.get(i++)+head);
}
else
operation_stack.add(head);
}
}
/**
* method command_parser will filter user's command, then first use
* process_paren to process the items in parentheses finally get the result by
* using parse_command function
*
* @param command
* string
*/
public static void command_parser(String command) {
// remove all spaces in the user's command
String filtered = command.replace(" ", "");
// remove unwanted parentheses to reduce errors
if (filtered.charAt(0) == '(' && filtered.charAt(filtered.length() - 1) == ')')
filtered = filtered.substring(1, filtered.length() - 1);
// process parentheses
String processed = process_paren(filtered);
if (processed.contains("Invalid"))
System.out.println("Invalid command!"); // if process parentheses is unsuccessful
else
System.out.println(parse_command(processed)); // otherwise compute the rest of command
}
/**
* method process_paren will process parentheses. This method use parens_check
* to check user's commands' parentheses structure First it will retrieve items
* in the parentheses and put in split_result array list. Next depends on the
* structure of user's command we will process it differently It will use
* parse_command function to compute items in the parentheses
*
* @param command
* string return the value string after processed
*/
public static String process_paren(String str) {
// parens_check, check if parentheses are in good structures
if (!parens_check(str))
return "Invalid input! Please check your parentheses";
else {
ArrayList<String> split_result = new ArrayList<String>();
for (String a : str.split("\\(")) {
for (String b : a.split("\\)"))
split_result.add(b);
}
for(String c:split_result)
System.out.println(c);
if (split_result.size() == 2) {
if (split_result.get(0).length() > 0)
return split_result.get(0) + parse_command(split_result.get(1));
else
return split_result.get(1);
} else {
if (split_result.size() % 2 == 0) {
split_result.set(split_result.size() / 2 + 1, split_result.get(split_result.size() / 2)
+ Double.toString(parse_command(split_result.get(split_result.size() / 2 + 1))));
split_result.remove(split_result.size() / 2);
return process_paren(split_result);
} else
return process_paren(split_result);
}
}
}
/**
* method parens_check check if the parentheses follow the correct structure
*
* @param text
* string
* @return boolean true if the parenthese structure is correct
*/
public static boolean parens_check(String text) {
Stack<Character> openParens = new Stack<>();
for (Character ch : text.toCharArray()) {
if (ch == '(') {
openParens.push(ch);
} else if (ch == ')') {
if (openParens.empty()) {
return false; // unbalanced
} else {
openParens.pop();
}
}
}
return true;
}
/**
* method process_paren will process the array list that has user's commands
* break down into list. The middle item is the most inner parenthese's item.
* Therefore we first convert that parenthese item into a single double number.
* Then, we proceed to the left side of the equation or right side depends on
* the position of parenthese.
*
* @param command_list
* ArrayList contains arithmetic statment breaks into pieces
* @return middle String return the double value in string
*/
public static String process_paren(ArrayList<String> command_list) {
/*
* Example of an equation converted to command_list
*
* a+(b+c)+d has the following list structure [a+,b+c,+d]. Therefore we start
* with middle, if length of both left and right side is not 0 we compute both
* side.
*
* (b+c)+d will have ["",b+c,+d] where the first item being empty
*
* a+((b+c)+d) will have [a+,"",b+c,+d,""]
*/
String middle = command_list.get(command_list.size() / 2);
middle = Double.toString(parse_command(middle));
for (int i = 1, j = command_list.size() / 2; (j + i) < command_list.size(); i++) {
if (command_list.get(j - i).length() == 0)
middle = Double.toString(parse_command(middle + command_list.get(j + i)));
else if (command_list.get(j + i).length() == 0)
middle = Double.toString(parse_command(command_list.get(j - i) + middle));
else
middle = Double.toString(parse_command(command_list.get(j - i) + middle + command_list.get(j + i)));
}
return middle;
}
/**
* method parse_command using Pattern and Mathcer class to find specific
* substring by using regular expression. The pattern can be split into cases
* where we have exponents,multiplication,division,addition and substraction.
* Notice that parenthese are already removed in the previous step process_paren
* function.
*
* @param user_command
* string contains arithmetic commands
* @return user_command to a double value
*/
public static double parse_command(String user_command) {
/*
* The order of arithmetic Parentheses Exponents Multiplication and Division
* Addition and Subtraction
*/
String[] temp;
Pattern pattern_neg = Pattern.compile("(^\\-)\\d+(\\.\\d+)?");
Pattern pattern_mul_exp = Pattern.compile("(\\*\\*(\\-)?\\d+(\\.\\d+)?(E\\d+)?\\*\\*(\\-)?\\d+(\\.\\d+)?(E\\\\d+)?)");
Pattern pattern_exp = Pattern.compile("((\\-)?\\d+(\\.\\d+)?(E\\d+)?\\*\\*(\\-)?\\d+(\\.\\d+)?(E\\d+)?)");
Pattern pattern_mul = Pattern.compile("((\\-)?\\d+(\\.\\d+)?(E\\d+)?\\*(\\-)?\\d+(\\.\\d+)?(E\\d+)?)");
Pattern pattern_div = Pattern.compile("((\\-)?\\d+(\\.\\d+)?(E\\d+)?\\/(\\-)?\\d+(\\.\\d+)?(E\\d+)?)");
Pattern pattern_add = Pattern.compile("((\\-)?\\d+(\\.\\d+)?(E\\d+)?\\+(\\-)?\\d+(\\.\\d+)?(E\\d+)?)");
Pattern pattern_sub = Pattern.compile("((\\-)?\\d+(\\.\\d+)?(E\\d+)?\\-(\\-)?\\d+(\\.\\d+)?(E\\d+)?)");
// The following part deals with a specific case where the input is
// a**b**c -> a^(b^(c))
// This needs special treatment
Matcher matcher_mul_exp = pattern_mul_exp.matcher(user_command);
while (matcher_mul_exp.find()) {
temp = matcher_mul_exp.group().split("\\*\\*");
user_command = user_command.replace(matcher_mul_exp.group(),
"**" + Double.toString(CalculatorFunctions.XtoN(Double.parseDouble(temp[1]), Double.parseDouble(temp[2]))));
// After replacing the first match to a double value, we have to work on the
// remaining
// This remind true for every condition
matcher_mul_exp = pattern_mul_exp.matcher(user_command);
}
Matcher matcher_exp = pattern_exp.matcher(user_command);
while (matcher_exp.find()) {
temp = matcher_exp.group().split("\\*\\*");
user_command = user_command.replace(matcher_exp.group(),
Double.toString(CalculatorFunctions.XtoN(Double.parseDouble(temp[0]), Double.parseDouble(temp[1]))));
matcher_exp = pattern_exp.matcher(user_command);
}
Matcher matcher_mul = pattern_mul.matcher(user_command);
Matcher matcher_div = pattern_div.matcher(user_command);
/*
* Mul and Div have the same priorities
* So we need to verify both's operation's position to determine which execute first
* */
while (matcher_mul.find()) {
if(matcher_div.find()) {
int mul_index = user_command.indexOf(matcher_mul.group());
int div_index = user_command.indexOf(matcher_div.group());
if(mul_index < div_index){
temp = matcher_mul.group().split("\\*");
user_command = user_command.replace(matcher_mul.group(),
Double.toString(Double.parseDouble(temp[0]) * Double.parseDouble(temp[1])));
matcher_mul = pattern_mul.matcher(user_command);
}
else {
temp = matcher_div.group().split("\\/");
user_command = user_command.replace(matcher_div.group(),
Double.toString(Double.parseDouble(temp[0]) / Double.parseDouble(temp[1])));
matcher_div = pattern_div.matcher(user_command);
temp = matcher_mul.group().split("\\*");
user_command = user_command.replace(matcher_mul.group(),
Double.toString(Double.parseDouble(temp[0]) * Double.parseDouble(temp[1])));
matcher_mul = pattern_mul.matcher(user_command);
}
}
else {
temp = matcher_mul.group().split("\\*");
user_command = user_command.replace(matcher_mul.group(),
Double.toString(Double.parseDouble(temp[0]) * Double.parseDouble(temp[1])));
matcher_mul = pattern_mul.matcher(user_command);
}
}
while (matcher_div.find()) {
temp = matcher_div.group().split("\\/");
user_command = user_command.replace(matcher_div.group(),
Double.toString(Double.parseDouble(temp[0]) / Double.parseDouble(temp[1])));
matcher_div = pattern_div.matcher(user_command);
}
Matcher matcher_add = pattern_add.matcher(user_command);
Matcher matcher_sub = pattern_sub.matcher(user_command);
while (matcher_add.find()) {
if(matcher_sub.find()) {
int add_index = user_command.indexOf(matcher_add.group());
int sub_index = user_command.indexOf(matcher_sub.group());
if(add_index<sub_index) {
temp = matcher_add.group().split("\\+");
user_command = user_command.replace(matcher_add.group(),
Double.toString(Double.parseDouble(temp[0]) + Double.parseDouble(temp[1])));
matcher_add = pattern_add.matcher(user_command);
}
else {
temp = matcher_sub.group().split("\\-");
int first=-1,second=-1;
for(int i =0;i<temp.length;i++)
if(temp[i].length()==0) {
temp[i+1]="-"+temp[i+1];
if(first==-1)
first=i+1;
else
second=i+1;
}
if(first==-1)
user_command = user_command.replace(matcher_sub.group(),
Double.toString(Double.parseDouble(temp[0]) - Double.parseDouble(temp[1])));
else if(second==-1)
if(first==1)
user_command = user_command.replace(matcher_sub.group(),
Double.toString(Double.parseDouble(temp[first]) - Double.parseDouble(temp[first+1])));
else
user_command = user_command.replace(matcher_sub.group(),
Double.toString(Double.parseDouble(temp[0]) - Double.parseDouble(temp[first])));
else
user_command = user_command.replace(matcher_sub.group(),
Double.toString(Double.parseDouble(temp[first]) - Double.parseDouble(temp[second])));
matcher_sub = pattern_sub.matcher(user_command);
temp = matcher_add.group().split("\\+");
user_command = user_command.replace(matcher_add.group(),
Double.toString(Double.parseDouble(temp[0]) + Double.parseDouble(temp[1])));
matcher_add = pattern_add.matcher(user_command);
}
}
else {
temp = matcher_add.group().split("\\+");
user_command = user_command.replace(matcher_add.group(),
Double.toString(Double.parseDouble(temp[0]) + Double.parseDouble(temp[1])));
matcher_add = pattern_add.matcher(user_command);
}
}
while (matcher_sub.find()) {
//System.out.println(matcher_sub.group());
temp = matcher_sub.group().split("\\-");
int first=-1,second=-1;
for(int i =0;i<temp.length;i++)
if(temp[i].length()==0) {
temp[i+1]="-"+temp[i+1];
if(first==-1)
first=i+1;
else
second=i+1;
}
if(first==-1)
user_command = user_command.replace(matcher_sub.group(),
Double.toString(Double.parseDouble(temp[0]) - Double.parseDouble(temp[1])));
else if(second==-1)
if(first==1)
user_command = user_command.replace(matcher_sub.group(),
Double.toString(Double.parseDouble(temp[first]) - Double.parseDouble(temp[first+1])));
else
user_command = user_command.replace(matcher_sub.group(),
Double.toString(Double.parseDouble(temp[0]) - Double.parseDouble(temp[first])));
else
user_command = user_command.replace(matcher_sub.group(),
Double.toString(Double.parseDouble(temp[first]) - Double.parseDouble(temp[second])));
matcher_sub = pattern_sub.matcher(user_command);
}
return Double.parseDouble(user_command);
}
}