• java中运算符的解析和计算


    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("******************
    数学运算符"
                    + getExpressionValue("(1+3)&&(0+10)"));
        }
    }

    运算结果:

    (a||b)&&c

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

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

    数学运算符true

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

  • 相关阅读:
    Linux文件和目录的权限
    nginx的默认web目录
    SSH-keygen rsa 密钥对根据私钥生成公钥
    对ajax的理解
    开关式复选框的操作
    关于弹框和原页面的切换问题
    单选框和复选框中value值得获取
    php中明明写了类函数,却报致命错误找不到类
    python函数
    python关键字
  • 原文地址:https://www.cnblogs.com/lbhqq/p/4332255.html
Copyright © 2020-2023  润新知