本文正在参加「Java主题月 – Java Debug笔记活动」,详情查看<活动链接>
提问:如何计算以字符串形式给出的数学表达式?
我正在尝试写个Java工具能直接计算字符串格式的数学表达式,例如
1. "5+3"
2. "10-40"
3. "(1+10)*3"
复制代码
我想避免写很多繁琐的if-then-else
语句,我该怎样做呢?
回答一
如果你使用的是JDK1.6,那么你可以使用内置的Javascript引擎来计算
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
public class Test {
public static void main(String[] args) throws ScriptException {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2";
System.out.println(engine.eval(foo));
}
}
复制代码
回答二
我写了下面这个算法来来处理这个问题。他能做加减乘除、指数运算(使用^
符号)和一些基本函数(如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 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 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
方法的变量表中查找变量名称,可以很容易的将读取函数名称的解析器更改为读取自定义变量。
- 大量计算
如果你想对某个表达式进行数百万次的求值,而并不是每次都对其进行解析呢?这是可能的,首先定义用于计算预编译表达式的接口:
@FunctionalInterface
interface Expression {
double eval();
}
复制代码
现在更改所有返回double
的方法,使它们返回该接口的实例。Java8的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;
}
}
}
复制代码
它构建了一个已编译的表达式的递归树。然后可以再编译一次,并使用不同的值重复计算:
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());
}
}
复制代码
你还可以让求值器的功能更加强大,如使用BigDecimal
代替double
,允许在表达式中混合一些数据类型,就像真正的编程语言一样。
文章翻译自Stack Overflow:stackoverflow.com/questions/3…
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END