JEP是java數學解析器的一個開源工具,解析并計算與數學運算相關的字符串的一個類庫,是java數學解析器的一個開源工具,提供第三方的jar包只要將jar包放到lib目錄下即可使用
簡單示例:
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());
}
}
}
具體更復雜的操作可以參看下載的zip中\doc\html\index.html
Jep is a Java library for parsing and evaluating mathematical expressions. With this package you can allow your users to enter an arbitrary formula as a string, and instantly evaluate it. Jep supports user defined variables, constants, and functions. A number of common mathematical functions and constants are included.
參考資料:http://www.singularsys.com/jep/doc/html/index.html