评估以字符串形式给出的数学表达式
  我正在尝试编写一个Java例程来评估来自String值的简单数学表达式,如: 
"5+3" "10-40" "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 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方法的变量表中查找名称,比如Map<String,Double> variables可以轻松地更改读取函数名称的解析器的位,以便处理自定义Map<String,Double> variables 。 
单独编译和评估:
如果增加了对变量的支持,如果你想用改变的变量对同一个表达式进行数百万次评估,而不是每次都解析它, 这是可能的。 首先定义一个用来评估预编译表达式的接口:
@FunctionalInterface
interface Expression {
    double eval();
}
  现在改变所有返回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());
    }
}
不同的数据类型:
  您可以将评估程序更改为使用更强大的功能,如BigDecimal或实现复数或合理数字(分数)的类,而不是double 。  你甚至可以使用Object ,在表达式中允许混合使用一些数据类型,就像真正的编程语言一样。  :) 
此答案中的所有代码都已发布到公共领域。 玩的开心!
解决这个问题的正确方法是使用词法分析器和解析器。 您可以自己编写这些简单版本,或者这些页面也可以链接到Java词法分析器和分析器。
创建一个递归下降解析器是一个非常好的学习练习。
链接地址: http://www.djcxy.com/p/76161.html