-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
331 lines (311 loc) · 7.56 KB
/
Parser.java
File metadata and controls
331 lines (311 loc) · 7.56 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
import java.util.ArrayList;
/**
* A class to take standard notaion functions and turn them into a value that
* the computer can understand.
*/
public final class Parser
{
private static final double E = 1E-10;
private static int pos;
private static int c;
private static String expression;
/**
* Takes a String representation of a function and calculates the value of
* the function for the given x value.
*
* @param ex The function to parse represented as a String.
* @param x The given x value for calculating the value of the function.
* @return The y value of the given function for the given x value.
*/
public static double parse(String ex, double x)
{
//parse variables and special equations first;
ex = replaceVariables(x, ex);
pos = -1;
expression = ex;
eatChar();
double v = parseExpression();
//if its super close to zero, it is zero
if(v>-E && v<E)
{
return 0;
}
return v;
}
private static String replaceVariables(double x, String function)
{
//special character codes to look for and replace.
ArrayList<Character> supportedFunctions = new ArrayList<Character>();
supportedFunctions.add('s'); //sin
supportedFunctions.add('c'); //cos
supportedFunctions.add('t'); //tan
supportedFunctions.add('S'); //arcsin
supportedFunctions.add('C'); //arccos
supportedFunctions.add('T'); //arctan
supportedFunctions.add('L'); //arctan
supportedFunctions.add('l'); //arctan
supportedFunctions.add('n'); //arctan
String s = function;
//replace x
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) == 'x')
{
s = s.substring(0,i) + x + s.substring(i+1);
}
}
//replace special functions with number values
for(int i = 0; i < s.length(); i++) //two loops because variable must
//be replaced first
{
if(supportedFunctions.contains(s.charAt(i)))
{
s = replaceWithFunction(i, s);
}
}
//System.out.println(s);
return s;
}
private static String replaceWithFunction(int i, String s)
{
//figures out what function to preform.
char fchar = s.charAt(i); //trig
if(fchar == 's')
{
s = sin(i,s);
}
else if(fchar == 'c')
{
s = cos(i,s);
}
else if(fchar == 't')
{
s = tan(i,s);
}
else if(fchar == 'S')
{
s = arcsin(i,s);
}
else if(fchar == 'C')
{
s = arccos(i,s);
}
else if(fchar == 'T')
{
s = arctan(i,s);
}
else if(fchar == 'L') //log
{
s = logBaseTwo(i,s);
}
else if(fchar == 'l')
{
s = logBase10(i,s);
}
else if(fchar == 'n')
{
s = naturalLog(i,s);
}
return s;
}
private static void eatChar()
{
c = (++pos < expression.length()) ? expression.charAt(pos) : -1;
}
private static void eatSpace()
{
while(Character.isWhitespace(c)){
eatChar();
}
}
private static double parseExpression()
{
double v = parseTerm();
while(true)
{
eatSpace();
if(c == '+')
{
eatChar();
v+=parseTerm();
}
else if(c == '-')
{
eatChar();
v-=parseTerm();
}
else
{
return v;
}
}
}
private static double parseTerm()
{
double v = parseFactor();
while(true)
{
eatSpace();
if(c == '/')
{
eatChar();
v /= parseFactor();
}
else if(c == '*' || c == '(')
{
if(c == '*')
{
eatChar();
}
v*=parseFactor();
}
else
{
return v;
}
}
}
private static double parseFactor()
{
double v;
boolean negate = false;
eatSpace();
if(c =='+' || c == '-')
{
negate = c == '-';
eatChar();
eatSpace();
}
if(c == '(')
{
eatChar();
v = parseExpression();
if(c == ')')
{
eatChar();
}
}
else
{
int startIndex = pos;
boolean small = false;
while((c >='0' && c <='9') || c =='.' || c == 'E' || c == '-')
{
eatChar();
}
if(pos==startIndex)
{
throw new SyntaxException("Error Char:" + (char)c);
//return "Error Char:" + (char)c;
}
try
{
v = Double.parseDouble(expression.substring(startIndex, pos));
}
catch(NumberFormatException ne)
{
throw new SyntaxException("Error Char:" + (char)c);
//return "Error Char:" + (char)c;
}
}
eatSpace();
if(c == '^')
{
eatChar();
v = Math.pow(v, parseFactor());
}
if(negate)
{
v = -v;
}
return v;
}
private static double findArgVal(int j, String s)
{
String replace = s.substring(j+2);
boolean found = false;
int parendCount = 1;
int index = 0;
for(int i = 0; i < replace.length() || !found; i++)
{
if(parendCount == 0)
{
found = true;
index = i;
break;
}
if(replace.charAt(i) == '(')
{
parendCount++;
}
else if(replace.charAt(i) == ')')
{
parendCount--;
}
}
String ex = s.substring(j+2,j+index+1);
double val =(parse(ex, 0));
return val;
}
private static String replaceFunction(int j, String s, double v)
{
String replace = s.substring(j+2);
boolean found = false;
int parendCount = 1;
int index = 0;
for(int i = 0; i < replace.length() || !found; i++)
{
if(replace.charAt(i) == '(')
{
parendCount++;
}
else if(replace.charAt(i) == ')')
{
parendCount--;
if(parendCount == 0)
{
found = true;
index = i;
break;
}
}
}
return s.substring(0,j) + v + s.substring(j+index+3);
}
private static String sin(int i, String s)
{
return replaceFunction(i,s,Math.sin(findArgVal(i,s)));
}
private static String cos(int i, String s)
{
return replaceFunction(i,s,Math.cos(findArgVal(i,s)));
}
private static String tan(int i, String s)
{
return replaceFunction(i,s,Math.tan(findArgVal(i,s)));
}
private static String arcsin(int i, String s)
{
return replaceFunction(i,s,Math.asin(findArgVal(i,s)));
}
private static String arccos(int i, String s)
{
return replaceFunction(i,s,Math.acos(findArgVal(i,s)));
}
private static String arctan(int i, String s)
{
return replaceFunction(i,s,Math.atan(findArgVal(i,s)));
}
private static String logBaseTwo(int i, String s)
{
return replaceFunction(i,s,Math.log(findArgVal(i,s))/Math.log(2));
}
private static String logBase10(int i, String s)
{
return replaceFunction(i,s,Math.log10(findArgVal(i,s)));
}
private static String naturalLog(int i, String s)
{
return replaceFunction(i,s,Math.log(findArgVal(i,s)));
}
}