[url]http://www.ne.jp/asahi/hishidama/home/tech/soft/java/eval16.html[/url]
上面的输出结果:
>java Calc "1+2 * (2 - 4) / -1"
计算式 :1+2 * (2 - 4) / -1
結果:5
还可以设置变量哦~~~
import jp.hishidama.eval.*;
public class Calc {
public static void main(String[] args){
String str = args[0];
System.out.println("计算式 :" + str);
Rule rule = ExpRuleFactory.getDefaultRule();
Expression exp = rule.parse(str); //解析
long result = exp.evalLong(); //计算
System.out.println("結果:" + result);
}
}
上面的输出结果:
>java Calc "1+2 * (2 - 4) / -1"
计算式 :1+2 * (2 - 4) / -1
結果:5
还可以设置变量哦~~~