java 使用JEP解析字符串计算公式 求值(附带自定义函数编写)

https://blog.csdn.net/weixin_40461281/article/details/105584570

2.X 存在精确度问题 推荐使用3.X 参考文章: java 使用 jep3.5 解析公式自动计算(包含BigDecimal模式 浮点数精准计算)

1.增加jep依赖

  1.  
    <dependency>
  2.  
    <groupId>org.scijava</groupId>
  3.  
    <artifactId>jep</artifactId>
  4.  
    <version>2.4.2</version>
  5.  
    </dependency>

2.计算公式  

JEP本身支持的函数 如果没有需要的函数  下文有自定义函数实现

计算 M12*3.14/4*pow(O5,2)*(K11+273-G11)/(G12*sqrt(3.14*M11*P11)) 的值  够复杂吧

  1.  
    public static void main(String[] args) {
  2.  
    JEP jep = new JEP();
  3.  
    // 添加常用函数
  4.  
    jep.addStandardFunctions();
  5.  
    // 添加常用常量
  6.  
    jep.addStandardConstants();
  7.  
     
  8.  
    String exp = "M12*3.14/4*pow(O5,2)*(K11+273-G11)/(G12*sqrt(3.14*M11*P11))"; //给变量赋值
  9.  
    jep.addVariable("M12", 1.1);
  10.  
    jep.addVariable("O5", 11.28665296);
  11.  
    jep.addVariable("K11", 25);
  12.  
    jep.addVariable("G11", 200);
  13.  
    jep.addVariable("G12", 100000);
  14.  
    jep.addVariable("M11", 0.000000129);
  15.  
    jep.addVariable("P11", 10);
  16.  
     
  17.  
    try { //执行
  18.  
    jep.parseExpression(exp);
  19.  
    double result = jep.getValue();
  20.  
    System.out.println("计算结果: " + result);
  21.  
    } catch (Throwable e) {
  22.  
    System.out.println("An error occured: " + e.getMessage());
  23.  
    }
  24.  
    }

完全不是问题

3.实现自定义函数 min max

min函数 两数取最小值

  1.  
    public class Min extends PostfixMathCommand {
  2.  
    public Min() {
  3.  
    super();
  4.  
    // 使用参数的数量
  5.  
    numberOfParameters = 2;
  6.  
    }
  7.  
     
  8.  
    @Override
  9.  
    public void run(Stack inStack) throws ParseException {
  10.  
    //检查栈
  11.  
    checkStack(inStack);
  12.  
    Object param2 = inStack.pop();
  13.  
    Object param1 = inStack.pop();
  14.  
     
  15.  
    if ((param1 instanceof Number) && (param2 instanceof Number)) {
  16.  
    double p1 = ((Number) param2).doubleValue();
  17.  
    double p2 = ((Number) param1).doubleValue();
  18.  
     
  19.  
    double result = Math.min(p1, p2);
  20.  
     
  21.  
    inStack.push(new Double(result));
  22.  
    } else {
  23.  
    throw new ParseException("Invalid parameter type");
  24.  
    }
  25.  
    return;
  26.  
    }
  27.  
     
  28.  
    }

max函数 两数取最大值

  1.  
    static class Max extends PostfixMathCommand {
  2.  
    public Max() {
  3.  
    super();
  4.  
    // 使用参数的数量
  5.  
    numberOfParameters = 2;
  6.  
    }
  7.  
     
  8.  
    @Override
  9.  
    public void run(Stack inStack) throws ParseException {
  10.  
    //检查栈
  11.  
    checkStack(inStack);
  12.  
    Object param2 = inStack.pop();
  13.  
    Object param1 = inStack.pop();
  14.  
     
  15.  
    if ((param1 instanceof Number) && (param2 instanceof Number)) {
  16.  
    double p1 = ((Number) param2).doubleValue();
  17.  
    double p2 = ((Number) param1).doubleValue();
  18.  
     
  19.  
    double result = Math.max(p1, p2);
  20.  
     
  21.  
    inStack.push(new Double(result));
  22.  
    } else {
  23.  
    throw new ParseException("Invalid parameter type");
  24.  
    }
  25.  
    return;
  26.  
    }
  27.  
     
  28.  
    }

验证是否好用

  1.  
    public static void main(String[] args) {
  2.  
    JEP jep = new JEP();
  3.  
    // 添加常用函数
  4.  
    jep.addStandardFunctions();
  5.  
    // 添加常用常量
  6.  
    jep.addStandardConstants();
  7.  
    // 添加自定义函数
  8.  
    jep.addFunction("min", new Min());
  9.  
    jep.addFunction("max", new Max());
  10.  
     
  11.  
    String min = "min(A1,A2)";
  12.  
    String max = "max(A1,A2)";
  13.  
    jep.addVariable("A1", 1.1);
  14.  
    jep.addVariable("A2", 2.3);
  15.  
     
  16.  
    try { //执行
  17.  
    jep.parseExpression(min);
  18.  
    double minResult = jep.getValue();
  19.  
    jep.parseExpression(max);
  20.  
    double maxResult = jep.getValue();
  21.  
    System.out.println("最小值为: " + minResult);
  22.  
    System.out.println("最大值为: " + maxResult);
  23.  
    } catch (Throwable e) {
  24.  
    System.out.println("An error occured: " + e.getMessage());
  25.  
    }
  26.  
    }

完全没问题

 
原文地址:https://www.cnblogs.com/xiondun/p/14758496.html