Commit 4959c2c5 authored by yanzg's avatar yanzg

公式处理

parent 7e8d62a0
...@@ -106,34 +106,24 @@ public class CalcHelper { ...@@ -106,34 +106,24 @@ public class CalcHelper {
double ret = 0; double ret = 0;
if (formula.matches(REGEX_QUOT)) { if (formula.matches(REGEX_QUOT)) {
// 获取第一个括号和最后一个括号 // 获取第一个括号和最后一个括号
Pattern p = Pattern.compile(REGEX_QUOT); Matcher matcher = getMatcher(REGEX_QUOT, formula);
Matcher matcher = p.matcher(formula);
if(!matcher.find()){
throw new RuntimeException("正则表达式错误");
}
double quotResult = calcProc(matcher.group(2), varValues); double quotResult = calcProc(matcher.group(2), varValues);
String formulaTo = matcher.group(1) + quotResult + matcher.group(3); String mutualKey = "@temp" + varValues.size();
ret = calcProc(formulaTo,varValues); 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)) { } else if (formula.matches(REGEX_CALC_ADD_PLUS)) {
// 判断是否包含+-运算符号 // 判断是否包含+-运算符号
Pattern p = Pattern.compile(REGEX_CALC_ADD_PLUS); Matcher matcher = getMatcher(REGEX_CALC_ADD_PLUS, formula);
Matcher matcher = p.matcher(formula); double leftResult = calcProc(matcher.group(1), varValues);
if(!matcher.find()){ double rightResult = calcProc(matcher.group(3), varValues);
throw new RuntimeException("正则表达式错误"); ret = calcItem(matcher.group(2), leftResult, rightResult);
}
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)) { } else if (formula.matches(REGEX_CALC_MULTIPLY_MOD)) {
// 判断是否包含*/运算符号 // 判断是否包含*/运算符号
Pattern p = Pattern.compile(REGEX_CALC_MULTIPLY_MOD); Matcher matcher = getMatcher(REGEX_CALC_MULTIPLY_MOD, formula);
Matcher matcher = p.matcher(formula); double leftResult = calcProc(matcher.group(1), varValues);
if(!matcher.find()){ double rightResult = calcProc(matcher.group(3), varValues);
throw new RuntimeException("正则表达式错误"); ret = calcItem(matcher.group(2), leftResult, rightResult);
}
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)) { } else if (formula.matches(REGEX_DOUBLE)) {
ret = StringHelper.toDouble(formula); ret = StringHelper.toDouble(formula);
} else { } else {
...@@ -143,6 +133,22 @@ public class CalcHelper { ...@@ -143,6 +133,22 @@ public class CalcHelper {
return ret; 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) { private static double calcItem(String flag, double a, double b) {
switch (flag) { switch (flag) {
case "+": case "+":
......
...@@ -22,5 +22,21 @@ public class TestCalcHelper { ...@@ -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