数学表达式解析

本文最后更新于:2023年8月15日 下午

数学表达式解析

要做一个解析字符串表达式的东西,解析之后给前端或者后端函数计算用;

要求能解析基本的加减乘除数学表达式,并且支持自定义函数;

  1. 先看java的:

    1、exp4j

    官网:https://www.objecthunter.net/exp4j/#Custom_functions
    最后一版是2017-01-30,属实挺老的
    参考链接,下面有内置函数列表,竟然没有sum:
    https://ibit.tech/archives/exp4j-introduction
    这里有个坑爹的地方,这个Function必须是exp4j的格式,它要求必须定义参数个数,
    那么我想实现一个可变参数的sum就不行了
    除了这个问题,其他都是满足要求的

    2、JEP

    最新版本是2021-04-06
    官网:https://www.singularsys.com/jep/doc/html/usage.html#gettingstarted

    看了下例子,语法有点难受,不用

    3、ibit-exp4j

    地址:https://github.com/ibit-tech/ibit-exp4j

    他是在exp4j基础上封装了一版,简化参数,我一看,比不封装还难用呢

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    //矮子里面拔高个,选exp4j
    public class Main {
    static Function sumFn = new Funct、ion("sum",3) {
    @Override
    public double apply(double... args) {
    double sumResult = 0.0;
    for (double number : args) {
    sumResult += number;
    }
    return sumResult;
    }
    };

    public static void main(String[] args) {
    Expression expression = new ExpressionBuilder("2 * sum(2,sum(4,5,1),2) - x")
    .function(sumFn)
    .variables("x")
    .build()
    .setVariable("x", 2);

    double result = expression.evaluate();
    System.out.println("Result: " + result); //26
    }
  2. python的

    python有个内置的函数eval可以解析表达式

    global_vars和local_vars是定义全局和局部变量的参数,视具体情况而定,一般情况下不写这两个参数就是默认使用全局的自定义函数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    # 定义两个函数
    def add(x, y):
    return x + y
    def sub(x, y):
    return x - y
    # 全局命名空间,
    global_vars = {'add': add, 'sub': sub}
    # 局部命名空间
    local_vars = {'x': 3, 'y': 4}
    # 要执行的表达式
    expression = "sub(5, 4) + add(x, y)*2"
    # 执行表达式,返回结果
    result = eval(expression, global_vars, local_vars)
    print(result) # 输出 15
  3. javascript

    搜了一会看到一个js的表达式解析,牛逼的math.js:

    官网:https://mathjs.org/docs/expressions/parsing.html

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    // create a parser
    const parser = math.parser()

    // evaluate expressions
    parser.evaluate('sqrt(3^2 + 4^2)') // 5
    parser.evaluate('sqrt(-4)') // 2i
    parser.evaluate('2 inch to cm') // 5.08 cm
    parser.evaluate('cos(45 deg)') // 0.7071067811865476

    // define variables and functions
    parser.evaluate('x = 7 / 2') // 3.5
    parser.evaluate('x + 3') // 6.5
    parser.evaluate('f(x, y) = x^y') // f(x, y)
    parser.evaluate('f(2, 3)') // 8

    // get and set variables and functions
    const x = parser.get('x') // x = 3.5
    const f = parser.get('f') // function
    const g = f(3, 3) // g = 27
    parser.set('h', 500)
    parser.evaluate('h / 2') // 250
    parser.set('hello', function (name) {
    return 'hello, ' + name + '!'
    })
    parser.evaluate('hello("user")') // "hello, user!"

    // clear defined functions and variables
    parser.clear()

数学表达式解析
http://bestkele.cn/2023/07/26/investigation/expression/
作者
kele
发布于
2023年7月26日
许可协议