常見的表達(dá)式計(jì)算lib有:
parsiiJEvalJEPLiteexprJaninoMathEval(1)parsii
Java代碼
String exp = "2 + (7-5) * 3.14159 * x + sin(0)";
// compile
Scope scope = Scope.create();
Expression parsiiExpr = Parser.parse(exp);
Variable var = scope.getVariable("x");
var.setValue(X_VALUE);
// evaluate
double result = parsiiExpr.evaluate();
System.out.println(result);//-> 2.0
(2)JEval
Java代碼
String exp = "2 + (7-5) * 3.14159 * #{x} + sin(0)";
// compile
Evaluator jevalEvaluator = new Evaluator();
jevalEvaluator.setVariables(Collections.singletonMap("x", Double.toString(X_VALUE)));
// evaluate
double result = Double.parseDouble(jevalEvaluator.evaluate(exp));
System.out.println(result);//-> 2.0
(3)JEPLite
Java代碼
String exp = "2 + (7-5) * 3.14159 * x + sin(0)";
// compile
JEP jep = new JEP();
jep.addVariable("x", X_VALUE);
jep.parseExpression(exp);
DoubleStack jepStack = new DoubleStack();
// evaluate
double result = jep.getValue(jepStack);
System.out.println(result);//-> 2.0
http://andreas.haufler.info/2013/12/how-to-write-one-of-fastest-expression.htmlhttp://www.transylvania-jug.org/archives/5777ExprEvalSample.zip (157.5 KB)
下載次數(shù): 6
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。