expression4J 数学公式计算

这篇博客介绍了在编程中处理数学公式计算的两种方式:Expression4J和SpringEL。作者通过代码示例展示了如何使用这两个库进行表达式求值,并指出在实际工作中SpringEL因其简洁性而更常用。最后,作者建议深入研究Expression4J的源码以学习其实现原理。
摘要由CSDN通过智能技术生成

 expression4J 数学公式计算

 

在学习解释器模式的时候,其用使用场景很少。对于数字的公式计算,可以用 expression4J来替代。

 

Expression4J

Expression4J下载

Expression4J 引用的jar包下载

 

下载下来后,进行解压,需要引用里面的lib下的jar包:

 

 

因为maven库没有该内容,所以使用的时候,需要手动引入。

 

代码

import fr.expression4j.basic.MathematicalElement;
import fr.expression4j.core.Expression;
import fr.expression4j.core.Parameters;
import fr.expression4j.core.exception.EvalException;
import fr.expression4j.core.exception.ParsingException;
import fr.expression4j.factory.ExpressionFactory;
import fr.expression4j.factory.NumberFactory;


public class Expression4jTest {
    public static void main (String[] args) throws ParsingException, EvalException {
        Expression expression = ExpressionFactory.createExpression("f(a,b,c)=a*b*c");
        System.out.println("Expression name: " + expression.getName());
        System.out.println("Expression parameters: " + expression.getParameters());
        MathematicalElement element_a = NumberFactory.createReal(3);
        MathematicalElement element_b = NumberFactory.createReal(4);
        MathematicalElement element_c = NumberFactory.createReal(5);
        Parameters parameters = ExpressionFactory.createParameters();
        parameters.addParameter("a", element_a);
        parameters.addParameter("b", element_b);
        parameters.addParameter("c", element_c);
        MathematicalElement evaluate = expression.evaluate(parameters);
        System.out.println("Expression result: " + evaluate.getValue());
    }
}

 

结果:

Expression name: f

Expression parameters: [a, b, c]

Expression result: 60.0

 在工作中用expression4J就显得繁琐,实际中更多用SpringEL来处理字符串中数值的计算:

SpringEL

 

Gradle 引用springEL: spring-expression

compile 'org.springframework:spring-expression:5.2.6.RELEASE'

 

代码:

import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

public class CalcuteExpl {
    public static void main(String[] args) {
        testCalcuteOperators();
    }
    public static void testCalcuteOperators() {
        ExpressionParser parser = new SpelExpressionParser();
        //算术运算
        int two = parser.parseExpression("(1 + 1)*13").getValue(Integer.class);
        System.out.println("1 + 1: " + two);
        Object three = parser.parseExpression("(125 - 3 + 1)/13").getValue();
        System.out.println("1 + 1: " + three);
        int four = parser.parseExpression("1 - (0-3)").getValue(Integer.class);
        System.out.println("1 - -3: " + four);
    }
}

 

结果:

1 + 1: 26

1 + 1: 9

1 - -3: 4

总结:

   有空好好看看expression4J的源码,学里面的思想。至于工作中更多用SpringEL,简洁方便。

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天狼1222

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值