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
posted on 2009-06-17 10:54
周锐 阅读(1399)
评论(0) 编辑 收藏 所属分类:
Java