import com.singularsys.jep.Jep; import com.singularsys.jep.JepException; public class SimpleExample { public static void main(String[] args) { Jep jep = new Jep(); //一个数学表达式 String exp = "((a+b)*(c+b))/(c+a)/b"; //给变量赋值 jep.addVariable("a", 10); jep.addVariable("b", 10); jep.addVariable("c", 10); try { //执行 jep.parse(exp); Object result = jep.evaluate(); System.out.println("计算结果: " + result); } catch (JepException e) { System.out.println("An error occured: " + e.getMessage()); } } }
package com.eric.test;
import org.nfunk.jep.JEP; public class SimpleExample { public static void main(String[] args) { JEP jep = new JEP(); // 一个数学表达式 String exp = "((a+b)*(c+b))/(c+a)/b"; // 给变量赋值 jep.addVariable("a", 10); jep.addVariable("b", 10); jep.addVariable("c", 10); jep.parseExpression(exp); Object result = jep.getValueAsObject(); System.out.println("计算结果: " + result); } }
package com.eric.test;
import org.nfunk.jep.JEP; public class Test { public static void main(String[] args) { JEP myPrase=new JEP(); myPrase.parseExpression("5+1+6"); double a=myPrase.getValue(); System.out.println(a); myPrase.parseExpression("1000<10002&&1001>1000"); //System.out.println((Boolean)myPrase.getValueAsObject()); System.out.println(myPrase.getValue()); } }
package com.eric.test; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class ScriptTest { private ScriptEngine se; public ScriptTest() { ScriptEngineManager sem = new ScriptEngineManager(); se = sem.getEngineByExtension("js"); } public Object cal(String expression) { Object result = null; try { result = se.eval(expression); } catch (Exception e) { e.printStackTrace(); System.out.println("Error occured"); } return result; } public static void main(String[] args) { ScriptTest app = new ScriptTest(); String[] s = { "1+1", "(1-2)*4", "3*(2+4)/27" }; for (int i = 0; i < s.length; i++) { Object r = app.cal(s[i]); System.out.println(s[i] + " = " + r); } } }