第一步 增加依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/QLExpress -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>QLExpress</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.dromara.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>6.0.0-M11</version>
</dependency>方案一
简单demo
语法
如果 (语文+数学+英语>270) 则 '及格' 否则 '不及格'
//返回 及格package com.study.Script.QLExpress.简单demo;
import com.guanwei.study.tools.WTools;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
public class QLExpressDemo {
public static void main(String[] args) throws Exception {
ExpressRunner runner = new ExpressRunner();
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
context.put("a",1);
context.put("b",2);
context.put("c",3);
String express = "(a+b*c-5)x50.11%";
express = WTools.mathUtil.transitionFormula(express);
System.out.println(express);
Object r = runner.execute(express, context, null, true, false);
System.out.println(r);
runner.addOperatorWithAlias("如果", "if",null);
runner.addOperatorWithAlias("则", "then",null);
runner.addOperatorWithAlias("否则", "else",null);
runner.addOperatorWithAlias("返回", "return",null);
String exp = "如果 (语文+数学+英语>270) 则 '及格' 否则 '不及格'";
context = new DefaultContext<String, Object>();
context.put("语文",30);
context.put("数学",2);
context.put("英语",3);
Object execute = runner.execute(exp, context, null, false, false);
System.out.println(execute);
}
}工具
package com.guanwei.study.tools;
import com.guanwei.study.math.MathUtil;
public class WTools {
public static MathUtil mathUtil = new MathUtil();
}工具 解析特殊符号 转换为 QLExpress 能看懂的
package com.guanwei.study.math;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ReUtil;
import com.guanwei.study.string.CharUtil;
import org.dromara.hutool.core.text.StrUtil;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class MathUtil {
/**
* 转换符号 公式等
* % + - * /
* @param exp
* @return
*/
public String transitionFormula(String exp){
// exp = "+";
//全角转半角
exp = CharUtil.toDbc(exp);
//所有字符转为小写
exp = exp.toLowerCase();
exp = exp
.replaceAll("﹢", "+")
.replaceAll("十", "+")
.replaceAll("┼", "+")
.replaceAll("╋", "+")
.replaceAll("╬", "+")
.replaceAll("﹣", "-")
.replaceAll("一", "-")
.replaceAll("x", "*")
.replaceAll("×", "*")
.replaceAll("╳", "*")
.replaceAll("х", "*")
.replaceAll("·", "*")
.replaceAll("÷", "/")
.replaceAll("\\{", "(")
.replaceAll("}", ")")
.replaceAll("\\[", "(")
.replaceAll("]", ")")
;
//包含百分号
if(exp.contains("%")){
//获取包含百分号的数字
List<String> resultFindAll = ReUtil.findAll("([\\d])*%|(\\d{1,10}).([\\d])*%", exp, 0, new ArrayList<String>());
// List<String> resultFindAll2 = ReUtil.findAll("([\\d+])*%|([\\d+{1,10}]).([\\d+])*%", exp, 0, new ArrayList<String>());
if(CollUtil.isNotEmpty(resultFindAll)){
for (String s : resultFindAll) {
s = s.trim();
String replace = s.replace("%", "");
if(StrUtil.isBlank(replace)){
exp = exp.replaceAll(s, "0");
}else{
BigDecimal bigDecimal = new BigDecimal(replace);
bigDecimal = bigDecimal.divide(new BigDecimal(100));
exp = exp.replaceAll(s, bigDecimal.toString());
}
}
}
System.out.println(resultFindAll);
}
return exp;
}
}
















