I took this code from a previously answered question and I am trying to expand it so that it works with E and PI when the String contains "E" and "PI". I don't really understand how the code works because I am pretty new to Java and the explanation on the original comment was not great (I have since lost the link to the comment unfortunately).
public static double eval(final String str) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
return x;
}
// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)`
// | number | functionName factor | factor `^` factor
double parseExpression() {
double x = parseTerm();
for (;;) {
if (eat('+')) x += parseTerm(); // addition
else if (eat('-')) x -= parseTerm(); // subtraction
else return x;
}
}
double parseTerm() {
double x = parseFactor();
for (;;) {
if (eat('*')) x *= parseFactor(); // multiplication
else if (eat('/')) x /= parseFactor(); // division
else return x;
}
}
double parseFactor() {
if (eat('+')) return parseFactor(); // unary plus
if (eat('-')) return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z') nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sqrt")) x = Math.sqrt(x);
else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
else if (func.equals("csc")) x = 1/Math.sin(Math.toRadians(x));
else if (func.equals("sec")) x = 1/Math.cos(Math.toRadians(x));
else if (func.equals("cot")) x = 1/Math.tan(Math.toRadians(x));
else if (func.equals("log")) x = Math.log(x);
else throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char)ch);
}
if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation
return x;
}
}.parse();
}
The key to the solution is to modify the grammar to support your supported named constants. It is therefore a requirement (as your examples suggest) that named constants be in capital letters: A - Z
(to distinguish between functions).
(The specified grammar is incomplete in that it does not specify the syntax of functions but the code suggests it is lower-case characters in a subset of trig and log functions.)
So, grammar is updated as:
// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)`
// | number | functionName factor | factor `^` factor | namedConstant
// namedConstant = 'PI' | 'E'
And the only modification required is to update parseFactor
and the only change to it is to add the else if
at the bottom.
double parseFactor() {
if (eat('+')) return parseFactor(); // unary plus
if (eat('-')) return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
//..no changes
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
//..no changes
} else if (ch >= 'a' && ch <= 'z') { // functions
//..no changes
} else if (ch >= 'A' && ch <= 'Z') { // named constants
while (ch >= 'A' && ch <= 'Z') nextChar();
String s = str.substring(startPos,this.pos);
if (s.equals("PI")) {
x = Math.PI;
} else if (s.equals("E")) {
x = Math.E;
} else {
throw new RuntimeException("Invalid constant: "+s);
}
} else {
throw new RuntimeException("Unexpected: " + (char)ch);
}
//...continue with your code
So these tests:
double d = eval("2.5");
System.out.println(d);
d = eval("-2.5");
System.out.println(d);
d = eval("2.5 * 10");
System.out.println(d);
d = eval("3 * PI");
System.out.println(d);
d = eval("E ^ 2");
System.out.println(d);
d = eval("7.5 + (2.5 * PI)");
System.out.println(d);
d = eval("sqrt PI");
System.out.println(d);
try {
d = eval("K / 3");
System.out.println(d);
} catch (RuntimeException re) {
System.out.println("--E--");
}
Produces:
2.5
-2.5
25.0
9.42477796076938
7.3890560989306495
15.353981633974483
1.7724538509055159
--E--
Postscript:
Some of your operations (terms and functions) do not protect against invalid input, such as divide-by-zero
and undefined functions such as log 0
.