-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmathFrom.java
More file actions
179 lines (165 loc) · 5.3 KB
/
mathFrom.java
File metadata and controls
179 lines (165 loc) · 5.3 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
class mathFrom {
public static boolean isNumeric(String str) {
if (str == null) {return false;}
try {
float f = Float.parseFloat(str);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
public static String removeParens(String str) {
if (str.charAt(0) != '(' || str.charAt(str.length()-1) != ')') {
return str;
}
int parenCount = 0;
int minPC = str.length();
for (int i = 0; i < str.length(); i++) {
if (minPC > parenCount & i != 0) {
minPC = parenCount;
}
if (str.charAt(i) == '(') {parenCount++;}
if (str.charAt(i) == ')') {parenCount--;}
}
if (minPC != 0) {
str = str.substring(1, str.length()-1);
return removeParens(str);
} else {
return str;
}
}
public static MathBlock string(String input) {
boolean diagnostic = false;
if (diagnostic) {System.out.println("converting: "+input);}
input = removeParens(input);
String supportedOps = "-+/*^";
if (diagnostic) {System.out.println("parens: "+input);}
if (isNumeric(input) || input.length() == 1) {
if (diagnostic) {System.out.println("is a number/variable! Result: "+input);}
MathBlock returnBlock = new MathBlock();
returnBlock.numOfInputs = 0;
if (isNumeric(input)) {
returnBlock.input = Float.parseFloat(input);
}else{
returnBlock.variable = input;
}
return returnBlock;
} else {
if (diagnostic) {System.out.println("is a unary/binary op...");}
MathBlock returnBlock = new MathBlock();
returnBlock.numOfInputs = 2;
int splitPoint = 100;
int[] operatorPrecedence = new int[100];
String[] parseByOp = new String[100];
for (int i = 0; i < parseByOp.length; i++) {
parseByOp[i]="";
operatorPrecedence[i] = 100;
}
int i = 0;
int parenCount = 0;
for (int ii = 0; ii < input.length(); ii++) {
if (input.charAt(ii) == '(') {parenCount++;}
if (input.charAt(ii) == ')') {parenCount--;}
if (parenCount == 0 & supportedOps.contains(Character.toString(input.charAt(ii))) & ii!=0) {
if (!supportedOps.contains(Character.toString(input.charAt(ii-1))) & !supportedOps.contains(Character.toString(input.charAt(ii+1)))) {
i++;
parseByOp[i] = ""+input.charAt(ii);
operatorPrecedence[i]=supportedOps.indexOf(parseByOp[i]);
if (min(operatorPrecedence)==supportedOps.indexOf(parseByOp[i])) {
splitPoint = i;
}
i++;
}
} else {
parseByOp[i] = parseByOp[i]+input.charAt(ii);
}
}
if (parseByOp[1] == "") {
if (diagnostic) {System.out.println("Okay, it's unary");};
returnBlock.numOfInputs = 1;
String blockString = "";
if (input.substring(0,2).equals("ln")) {
blockString = input.substring(3, input.length()-1);
} else {
blockString = input.substring(4, input.length()-1);
}
returnBlock.operator1 = op1From(input.substring(0,3));
returnBlock.blockA = mathFrom.string(blockString);
return returnBlock;
}
if (diagnostic) {System.out.println("Binary! I split this as: ");};
if (diagnostic) {arrayPrint(parseByOp);}
//split separated array into two halves
String partOne = "";
String partTwo = "";
for (i=0;i<input.length();i++) {
if (i<splitPoint) {
partOne+=parseByOp[i];
} else if (i==splitPoint) {
} else {
partTwo+=parseByOp[i];
}
}
if (diagnostic) {
System.out.println("partOne: "+partOne);
System.out.println("partTwo: "+partTwo);
System.out.println("Operator: "+parseByOp[splitPoint]);
}
returnBlock.blockA = mathFrom.string(partOne);
returnBlock.blockB = mathFrom.string(partTwo);
returnBlock.operator2 = op2from(parseByOp[splitPoint]);
return returnBlock;
}
}
public static OpInterface1 op1From(String str) {
switch(str) {
case "ln(":
return (a) -> (float) Math.log(a);
case "sin":
return (a) -> (float) Math.sin(a);
case "cos":
return (a) -> (float) Math.cos(a);
case "tan":
return (a) -> (float) Math.tan(a);
case "sec":
return (a) -> (float) 1/((float) Math.cos(a));
case "csc":
return (a) -> (float) 1/((float) Math.sin(a));
case "cot":
return (a) -> (float) 1/((float) Math.tan(a));
default:
return (a) -> -1;
}
}
public static OpInterface2 op2from(String str) {
switch(str) {
case "+":
return (a,b) -> a+b;
case "-":
return (a,b) -> a-b;
case "*":
return (a,b) -> a*b;
case "/":
return (a,b) -> a/b;
case "^":
return (a,b) -> (float)Math.pow(a,b);
default:
return (a,b) -> (a+b)/0;
}
}
public static int min(int[] arr) {
int minint = arr[0];
for (int i = 0; i<arr.length; i++) {
if (minint > arr[i]) {minint = arr[i];}
}
return minint;
}
public static void arrayPrint(String[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (arr[i]=="") {break;}
System.out.print("\""+arr[i]+"\", ");
}
System.out.println("]");
}
};