如何计算以字符串形式给出的数学表达式?
我正在尝试编写一个Java例程来计算来自以下值的数学表达式:String
"5+3"
"10-40"
"(1+10)*3"
我想避免很多 if-then-else 语句。我该怎么做?
我正在尝试编写一个Java例程来计算来自以下值的数学表达式:String
"5+3"
"10-40"
"(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));
}
}
我为算术表达式编写了这种方法来回答这个问题。它执行加法,减法,乘法,除法,幂(使用符号)以及一些基本函数,例如.它支持使用 ...进行分组,并正确获取运算符优先级和关联性规则。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”函数,请更改所有返回 s 的方法,以便它们返回该接口的实例。Java 8的lambda语法对此非常有效。更改的方法之一的示例:double
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
此答案中的所有代码都发布到公共领域。玩得愉快!