public class Parser {
public static double h = 1E-8;
public static double a = -3;
private final double Pi = 3.14592653489;
private final double e = 2.718281828459;
private double x1;
/*
* Vor dem parsen werden die Leerzeichen entfernt
*/
public double StrToDbl(String s, double x) {
x1 = x;
if (s.equals("")) {
return 0;
}
s.trim();
if (s.charAt(0) == '-') {
s = "0" + s;
}
s = vereinfache(s);
return parse(s);
}
private String vereinfache(String s) {
for (int k = 0; k <= s.length() - 1; ++k) {
if (s.length() - k > 3) {
switch (s.charAt(k)) {
case 's': {
if (s.charAt(k + 1) == 'i') {
s = s.substring(0, k) + "s"
+ s.substring(k + 3, s.length());
break;
} else {
s = s.substring(0, k) + "w"
+ s.substring(k + 4, s.length());
break;
}
}
case 'c': {
s = s.substring(0, k) + "c"
+ s.substring(k + 3, s.length());
break;
}
case 't': {
s = s.substring(0, k) + "t"
+ s.substring(k + 3, s.length());
break;
}
case 'l': {
if (s.charAt(k + 1) == 'o') {
s = s.substring(0, k) + "l"
+ s.substring(k + 3, s.length());
break;
} else {
s = s.substring(0, k) + "n"
+ s.substring(k + 1, s.length());
break;
}
}
case 'p': {
s = s.substring(0, k) + "p"
+ s.substring(k + 2, s.length());
break;
}
}
}
}
return s;
}
private int pos0(char c, String s) {
int k, z;
z = 0;
for (k = s.length() - 1; k >= 0; --k) {
if (s.charAt(k) == '(') {
++z;
}
if (s.charAt(k) == ')') {
--z;
}
if ((s.charAt(k) == c) && (z == 0)) {
return k;
}
}
return -1;
}
private double parse(String s) {
if (s.equals("")) {
return 0;
}
s = s.trim();
if (pos0('+', s) > -1) {
return parse(s.substring(0, pos0('+', s)))
+ parse(s.substring(pos0('+', s) + 1, s.length()));
} else if (pos0('-', s) > -1) {
return parse(s.substring(0, pos0('-', s)))
- parse(s.substring(pos0('-', s) + 1, s.length()));
} else if (pos0('*', s) > -1) {
return parse(s.substring(0, pos0('*', s)))
* parse(s.substring(pos0('*', s) + 1, s.length()));
} else if (pos0('/', s) > -1) {
return parse(s.substring(0, pos0('/', s)))
/ parse(s.substring(pos0('/', s) + 1, s.length()));
} else if (pos0('^', s) > -1) {
return Math.pow(parse(s.substring(0, pos0('^', s))), parse(s
.substring(pos0('^', s) + 1, s.length())));
} else if (pos0('w', s) > -1) {
return Math.sqrt(parse(s.substring(pos0('w', s) + 1, s.length())));
} else if (pos0('s', s) > -1) {
return Math.sin(parse(s.substring(pos0('s', s) + 1, s.length())));
} else if (pos0('t', s) > -1) {
return Math.tan(parse(s.substring(pos0('t', s) + 1, s.length())));
} else if (pos0('c', s) > -1) {
return Math.cos(parse(s.substring(pos0('c', s) + 1, s.length())));
} else if (pos0('l', s) > -1) {
return Math.log(parse(s.substring(pos0('l', s) + 1, s.length())));
} else if (pos0('n', s) > -1) {
return Math.log(parse(s.substring(pos0('n', s) + 1, s.length())))
/ Math.log(2);
} else if ((s.equals("") == false) && (s.charAt(0) == '(')) {
s = s.substring(1, s.length() - 1);
return parse(s);
} else if (s.equals("x")) {
return x1;
} else if (s.equals("a")) {
return a;
} else if (s.equals("h")) {
return h;
} else if (s.equals("p")) {
return Pi;
} else if (s.equals("e")) {
return e;
} else {
double x;
try {
x = Double.parseDouble(s);
return x;
} catch (Exception ex) {
}
}
return 0;
}
}