我正在尝试编写一个 Java 例程来评估来自 String
值的数学表达式,例如:
"5+3"
"10-40"
"(1+10)*3"
我想避免很多 if-then-else 语句。我怎样才能做到这一点?
原文由 Shah 发布,翻译遵循 CC BY-SA 4.0 许可协议
我写了这个 eval
算术表达式的方法来回答这个问题。它执行加法、减法、乘法、除法、取幂(使用 ^
符号)和一些基本函数,如 sqrt
。它支持使用 (
… )
进行分组,并使运算符 优先级 和 关联性 规则正确。
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 `(` expression `)` | 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();
if (!eat(')')) throw new RuntimeException("Missing ')'");
} 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);
if (eat('(')) {
x = parseExpression();
if (!eat(')')) throw new RuntimeException("Missing ')' after argument to " + func);
} else {
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 throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char)ch);
}
if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation
return x;
}
}.parse();
}
例子:
System.out.println(eval("((4 - 2^3 + 1) * -sqrt(3*3+4*4)) / 2"));
输出:7.5 (正确)
该解析器是一个 递归下降解析器,因此在内部对其语法中的每个运算符优先级级别使用单独的解析方法。我故意保持 简短,但这里有一些您可能想扩展它的想法:
通过查找传递给 eval
方法的变量表中的名称,例如 Map<String,Double> variables
,可以轻松更改解析器读取函数名称的位以处理自定义变量 ---
。
如果在添加了对变量的支持后,您想使用已更改的变量对同一个表达式求值数百万次,而不是每次都解析它,该怎么办?这是可能的。首先定义一个用于评估预编译表达式的接口:
@FunctionalInterface
interface Expression {
double eval();
}
现在将原来的“eval”函数改造成“parse”函数,更改所有返回 double
s 的方法,因此它们返回该接口的一个实例。 Java 8 的 lambda 语法很适合这个。其中一种更改方法的示例:
Expression parseExpression() {
Expression x = parseTerm();
for (;;) {
if (eat('+')) { // addition
Expression a = x, b = parseTerm();
x = (() -> a.eval() + b.eval());
} else if (eat('-')) { // subtraction
Expression a = x, b = parseTerm();
x = (() -> a.eval() - b.eval());
} else {
return x;
}
}
}
这构建了一个递归树 Expression
代表编译表达式的对象( 抽象语法树)。然后你可以编译它一次并用不同的值重复评估它:
public static void main(String[] args) {
Map<String,Double> variables = new HashMap<>();
Expression exp = parse("x^2 - x + 2", variables);
for (double x = -20; x <= +20; x++) {
variables.put("x", x);
System.out.println(x + " => " + exp.eval());
}
}
代替 double
,您可以更改评估器以使用更强大的东西,例如 BigDecimal
,或实现复数或有理数(分数)的类。您甚至可以使用 Object
,允许在表达式中混合使用一些数据类型,就像真正的编程语言一样。 :)
此答案中的所有代码均已发布 到公共领域。玩得开心!
原文由 Boann 发布,翻译遵循 CC BY-SA 4.0 许可协议
15 回答8.4k 阅读
8 回答6.3k 阅读
1 回答4.1k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
2 回答3.1k 阅读
2 回答3.8k 阅读
1 回答2.1k 阅读✓ 已解决
使用JDK1.6,可以使用内置的Javascript引擎。