-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalc.java
More file actions
63 lines (54 loc) · 2.16 KB
/
Calc.java
File metadata and controls
63 lines (54 loc) · 2.16 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
package sample;
import java.util.ArrayList;
public class Calc {
public double calcSplit(String string, ArrayList<String> splits){
double aNum = 0;
double bNum = 0;
double c = 0;
boolean containsSymbol = false;
for (String symbol : new String[]{"+", "-", "*", "/", "^"}) {
if (string.contains(symbol)) {
containsSymbol = true;
String a = string.substring(0, string.indexOf(symbol));
String b = string.substring(string.indexOf(symbol) + 1);
if (a.contains("#")){
System.out.println(splits.get(Integer.parseInt(a.substring(1, a.length()-1))));
aNum = calcSplit(splits.get(Integer.parseInt(a.substring(1, a.length()-1))), splits);
} else {
aNum = Double.parseDouble(a);
}
if (b.contains("#")){
System.out.println(splits.get(Integer.parseInt(b.substring(1, b.length()-1))));
bNum = calcSplit(splits.get(Integer.parseInt(b.substring(1, b.length()-1))), splits);
} else {
bNum = Double.parseDouble(b);
}
switch (symbol){
case "+":
c = aNum + bNum;
break;
case "-":
c = aNum - bNum;
break;
case "*":
c = aNum * bNum;
break;
case "/":
c = aNum / bNum;
break;
case "^":
c = Math.pow(aNum, bNum);
break;
default:
break;
}
System.out.println(a + " " + symbol + " " + b + " | " + aNum + " " + symbol + " " + bNum + " = " + c);
}
}
if (!containsSymbol){
c = Double.parseDouble(string);
System.out.println(c);
}
return c;
}
}