码迷,mamicode.com
首页 > 编程语言 > 详细

java中运算符的解析和计算

时间:2015-03-12 14:59:15      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

package com.LBH;
 
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
 
import com.singularsys.jep.Jep;
import com.singularsys.jep.bigdecimal.BigDecComponents;
 
public class JieXi {
 
    public static boolean getExpressionValue(String expression) {
        Jep jep = new Jep(new BigDecComponents());
        jep.addStandardConstants();
        Object result = null;
        try {
            jep.parse(expression);
            result = jep.evaluate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (Boolean) result;
 
    }
 
    public static Object getMathValue(String str) throws ScriptException{
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        engine.put("a", true);
        engine.put("b", false);
        engine.put("c", true);
        Object result = engine.eval(str);
        return result;
    }
     
    public static void main(String[] args) throws Exception {
 
        String str = "(a||b)&&c";
        System.out.println(str);
        Object result = getMathValue(str);
        System.out.println("关系运算符计算结果,类型:" + result.getClass().getName()
                + ",结果:" + result);
        System.out.println("******************\n数学运算符"
                + getExpressionValue("(1+3)&&(0+10)"));
    }
}

运算结果:

(a||b)&&c

关系运算符计算结果,类型:java.lang.Boolean,结果:true

******************

数学运算符true

 

 

参考:http://blog.sina.com.cn/s/blog_acdc06250101p5dh.html

java中运算符的解析和计算

标签:

原文地址:http://www.cnblogs.com/lbhqq/p/4332255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!