-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseMath.java
More file actions
219 lines (208 loc) · 10.2 KB
/
Copy pathParseMath.java
File metadata and controls
219 lines (208 loc) · 10.2 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
import java.util.ArrayList;
public class ParseMath {
public static final String VALID_NUMBERS = "1234567890.";
public static final String VALID_OPS = "^*/+-%";
public static final String[] VALID_FUNCTIONS = {"sin", "sin\u207b¹", "cos", "cos\u207b¹", "tan", "tan\u207b¹", "abs", "sqrt", "ln", "log\u2081\u2080", "fac"};
public static final String[] PEMDAS = {"^", "*/%", "+-"};
// finds the close parenthesis and returns the whole block
private static String getBlockAt(int index, String text) {
int closeIndex = index + 1;
int counter = 1;
while (counter != 0 && closeIndex < text.length()) {
if (("" + text.charAt(closeIndex)).equals("(")) {
counter += 1;
} else if (("" + text.charAt(closeIndex)).equals(")")) {
counter -= 1;
}
closeIndex += 1;
}
return text.substring(index, closeIndex);
}
public static String formatFormula(String formula) throws Exception {
String newFormula = "";
for (int i = 0; i < formula.length(); i++) {
String character = "" + formula.charAt(i);
// -sin(2+1) -> -1*sin(2+1)
if ((character.equals("(") || isFunction(i, formula)) && newFormula.length() > 0 && ("" + newFormula.charAt(newFormula.length() - 1)).equals("-")) {
newFormula += "1*";
} if ((character.equals("(") || isFunction(i, formula)) && newFormula.length() > 0 && VALID_NUMBERS.contains("" + newFormula.charAt(newFormula.length() - 1))) {
newFormula += "*";
} if (VALID_NUMBERS.contains(character) || VALID_OPS.contains(character) || character.equals("(") || character.equals(")")) {
newFormula += character;
} else if (isFunction(i, formula)) {
String function = getFunction(i, formula);
newFormula += function;
i += function.length() - 1;
} else if (character.equals("π")) {
// can't have 3pi, for example
if (VALID_NUMBERS.contains("" + newFormula.charAt(newFormula.length() - 1))) {
throw new Exception();
}
newFormula += "" + Math.PI;
} else if (character.equals("e")) {
if (VALID_NUMBERS.contains("" + newFormula.charAt(newFormula.length() - 1))) {
throw new Exception();
}
newFormula += "" + Math.E;
}
}
return newFormula;
}
public static boolean isFunction(int index, String context) {
String sub = context.substring(index);
for (String function : VALID_FUNCTIONS) {
if (sub.startsWith(function)) {
return true;
}
}
return false;
}
public static String getFunction(int index, String context) {
String sub = context.substring(index);
String longestMatchingFunction = "";
for (String function : VALID_FUNCTIONS) {
if (function.length() > longestMatchingFunction.length() && sub.startsWith(function)) {
longestMatchingFunction = function;
}
}
return longestMatchingFunction;
}
// recursive
public static ArrayList<Object> parseFormula(String formula, boolean isRadians) throws Exception {
formula = formatFormula(formula);
ArrayList<Object> parsedFormula = new ArrayList<Object>();
// to do negatives, assume the formula goes back and forth between objects and operations
// that way, "-" can be both a valid character and a vaild 'number'
boolean isOp = false;
int index = 0;
while (index < formula.length()) {
String character = "" + formula.charAt(index);
// recurse on parentheses
if (character.equals("(")) {
String block = getBlockAt(index, formula);
index += block.length();
parsedFormula.add(parseFormula(block.substring(1, block.length() - 1), isRadians));
isOp = true;
} else {
if (!isOp) {
if (isFunction(index, formula)) {
// reduce whole function to a constant
String functionInput = getBlockAt(formula.indexOf("(", index + 1), formula);
double simplifiedInput = solve(parseFormula(functionInput.substring(1, functionInput.length() - 1), isRadians));
String function = getFunction(index, formula);
double output = 0.0;
if (function.equals("sin") || function.equals("cos") || function.equals("tan")) {
// use the right mode
if (!isRadians) {
simplifiedInput = Math.toRadians(simplifiedInput);
}
if (function.equals("sin")) {
output = Math.sin(simplifiedInput);
} else if (function.equals("cos")) {
output = Math.cos(simplifiedInput);
} else if (function.equals("tan")) {
output = Math.tan(simplifiedInput);
}
} else if (function.equals("sin\u207b¹") || function.equals("cos\u207b¹") || function.equals("tan\u207b¹")) {
if (function.equals("sin\u207b¹")) {
output = Math.asin(simplifiedInput);
} else if (function.equals("cos\u207b¹")) {
output = Math.acos(simplifiedInput);
} else if (function.equals("tan\u207b¹")) {
output = Math.atan(simplifiedInput);
}
if (!isRadians) {
output = Math.toDegrees(output);
}
} else if (function.equals("sqrt")) {
output = Math.sqrt(simplifiedInput);
} else if (function.equals("abs")) {
output = Math.abs(simplifiedInput);
} else if (function.equals("ln")) {
output = Math.log(simplifiedInput);
} else if (function.equals("log\u2081\u2080")) {
output = Math.log10(simplifiedInput);
} else if (function.equals("fac")) {
// factorials
int input = (int) simplifiedInput;
double total = 1;
for (int x = input; input > 0; input--) {
total *= input;
}
output = total;
}
parsedFormula.add(output);
index += functionInput.length() + function.length();
}
else {
// constants
String thing = "";
while ((VALID_NUMBERS.contains(character) || (character.equals("-") && thing.length() == 0)) && index < formula.length()) {
thing += character;
index += 1;
try {
character = "" + formula.charAt(index);
} catch (Exception e) {
character = "";
}
}
parsedFormula.add(Double.parseDouble(thing));
}
isOp = true;
} else {
// operations
parsedFormula.add(character);
index += 1;
isOp = false;
}
}
}
return parsedFormula;
}
// also recursive
public static double solve(ArrayList<Object> formula) throws Exception {
for (int index = 0; index < formula.size(); index++) {
Object object = formula.get(index);
if (object instanceof ArrayList) {
// recurse on parentheses
formula.set(index, solve((ArrayList<Object>) object));
}
}
for (String currentOp : PEMDAS) {
// skip over operations and get constants
for (int index = 0; index < formula.size() - 2; index += 2) {
String op = "" + formula.get(index + 1);
if (currentOp.contains(op)) {
// reduce 3 + 2 to 5 in the formula
double first = (double) formula.get(index);
double second = (double) formula.get(index + 2);
double result = 0.0;
if (op.equals("+")) {
result = first + second;
} else if (op.equals("-")) {
result = first - second;
} else if (op.equals("*")) {
result = first * second;
} else if (op.equals("/")) {
result = first / second;
} else if (op.equals("%")) {
result = first % second;
} else if (op.equals("^")) {
result = Math.pow(first, second);
}
ArrayList<Object> newFormula = new ArrayList<Object>();
for (int preIndex = 0; preIndex < index; preIndex++) {
newFormula.add(formula.get(preIndex));
}
newFormula.add(result);
for (int postIndex = index + 3; postIndex < formula.size(); postIndex++) {
newFormula.add(formula.get(postIndex));
}
formula = newFormula;
index -= 2;
}
}
}
return (double) formula.get(0);
}
}