java数学表达式模板解析工具

2020-04-21  本文已影响0人  程序员大春

引用 https://www.yunliantaida.com

需求


调研

MathParser.org-mXparser

示例

<dependency>
           <groupId>org.mariuszgromada.math</groupId>
           <artifactId>MathParser.org-mXparser</artifactId>
           <version>4.2.0</version>
       </dependency>
 public static void main(String[] args) throws InterruptedException {
        Argument days = new Argument("days = 30");
        Expression min = new Expression("min(days/10,1)", days);
        mXparser.consolePrint(min.getExpressionString() + "----" + min.calculate());
        Expression e = new Expression("2-(32-4)/(23+4/5)-(2-4)*(4+6-98.2)+4");
        mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
        Expression abs = new Expression("abs(-1)");
        mXparser.consolePrintln("Res: " + abs.getExpressionString() + " = " + abs.calculate());
        Expression sqrt = new Expression("sqrt(2)");
        mXparser.consolePrintln("Res: " + sqrt.getExpressionString() + " = " + sqrt.calculate());
     
    }

ScriptEngine

示例

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
 
public class MainTest {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager manager=new ScriptEngineManager();
        ScriptEngine engine=manager.getEngineByName("javascript");
        System.out.println(engine.eval("(3*4)%2+1"));
    }
    
}
上一篇下一篇

猜你喜欢

热点阅读