Commit 4959c2c5 authored by yanzg's avatar yanzg

公式处理

parent 7e8d62a0
......@@ -106,34 +106,24 @@ public class CalcHelper {
double ret = 0;
if (formula.matches(REGEX_QUOT)) {
// 获取第一个括号和最后一个括号
Pattern p = Pattern.compile(REGEX_QUOT);
Matcher matcher = p.matcher(formula);
if(!matcher.find()){
throw new RuntimeException("正则表达式错误");
}
Matcher matcher = getMatcher(REGEX_QUOT, formula);
double quotResult = calcProc(matcher.group(2), varValues);
String formulaTo = matcher.group(1) + quotResult + matcher.group(3);
ret = calcProc(formulaTo,varValues);
String mutualKey = "@temp" + varValues.size();
varValues.put(mutualKey, quotResult);
String formulaTo = matcher.group(1) + mutualKey + matcher.group(3);
ret = calcProc(formulaTo, varValues);
} else if (formula.matches(REGEX_CALC_ADD_PLUS)) {
// 判断是否包含+-运算符号
Pattern p = Pattern.compile(REGEX_CALC_ADD_PLUS);
Matcher matcher = p.matcher(formula);
if(!matcher.find()){
throw new RuntimeException("正则表达式错误");
}
double leftResult = calcProc(matcher.group(1),varValues);
double rightResult = calcProc(matcher.group(3),varValues);
ret = calcItem(matcher.group(2), leftResult,rightResult);
Matcher matcher = getMatcher(REGEX_CALC_ADD_PLUS, formula);
double leftResult = calcProc(matcher.group(1), varValues);
double rightResult = calcProc(matcher.group(3), varValues);
ret = calcItem(matcher.group(2), leftResult, rightResult);
} else if (formula.matches(REGEX_CALC_MULTIPLY_MOD)) {
// 判断是否包含*/运算符号
Pattern p = Pattern.compile(REGEX_CALC_MULTIPLY_MOD);
Matcher matcher = p.matcher(formula);
if(!matcher.find()){
throw new RuntimeException("正则表达式错误");
}
double leftResult = calcProc(matcher.group(1),varValues);
double rightResult = calcProc(matcher.group(3),varValues);
ret = calcItem(matcher.group(2), leftResult,rightResult);
Matcher matcher = getMatcher(REGEX_CALC_MULTIPLY_MOD, formula);
double leftResult = calcProc(matcher.group(1), varValues);
double rightResult = calcProc(matcher.group(3), varValues);
ret = calcItem(matcher.group(2), leftResult, rightResult);
} else if (formula.matches(REGEX_DOUBLE)) {
ret = StringHelper.toDouble(formula);
} else {
......@@ -143,6 +133,22 @@ public class CalcHelper {
return ret;
}
/**
* 匹配正则表达式和公式
*
* @param regex 正则表达式
* @param formula 公式
* @return 匹配后的结果
*/
private static Matcher getMatcher(String regex, String formula) {
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(formula);
if (!matcher.find()) {
throw new RuntimeException("正则表达式错误");
}
return matcher;
}
private static double calcItem(String flag, double a, double b) {
switch (flag) {
case "+":
......
......@@ -22,5 +22,21 @@ public class TestCalcHelper {
}
}
}));
System.out.println(CalcHelper.calc(" a * 120 + 2 * ( a + b * d )", new CalcHelper.CalcParameter() {
@Override
public double getValue(String parameterName) {
switch (parameterName){
case "a":
return 5;
case "b":
return 3;
case "d":
return 11;
default:
return 0;
}
}
}));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment