Java数学表达式计算(Expression Evaluator)

常见的表达式计算lib有: 



(1)parsii 
Java代码   收藏代码
  1. String exp = "2 + (7-5) * 3.14159 * x + sin(0)";  
  2.   
  3. // compile  
  4. Scope scope = Scope.create();  
  5. Expression parsiiExpr = Parser.parse(exp);  
  6. Variable var = scope.getVariable("x");  
  7. var.setValue(X_VALUE);  
  8.   
  9. // evaluate  
  10. double result = parsiiExpr.evaluate();  
  11.   
  12. System.out.println(result);//-> 2.0  


(2)JEval 
Java代码   收藏代码
  1. String exp = "2 + (7-5) * 3.14159 * #{x} + sin(0)";  
  2.   
  3. // compile  
  4. Evaluator jevalEvaluator = new Evaluator();  
  5. jevalEvaluator.setVariables(Collections.singletonMap("x", Double.toString(X_VALUE)));  
  6.   
  7. // evaluate  
  8. double result = Double.parseDouble(jevalEvaluator.evaluate(exp));  
  9.   
  10. System.out.println(result);//-> 2.0  


(3)JEPLite 
Java代码   收藏代码
  1. String exp = "2 + (7-5) * 3.14159 * x + sin(0)";  
  2.   
  3. // compile  
  4. JEP jep = new JEP();  
  5. jep.addVariable("x", X_VALUE);  
  6. jep.parseExpression(exp);  
  7. DoubleStack jepStack = new DoubleStack();  
  8.   
  9. // evaluate  
  10. double result = jep.getValue(jepStack);  
  11.   
  12. System.out.println(result);//-> 2.0  


http://andreas.haufler.info/2013/12/how-to-write-one-of-fastest-expression.html 
http://www.transylvania-jug.org/archives/5777