Commit d4e8ae9f authored by yanzg's avatar yanzg

修改实体位置

parent f596f934
...@@ -20,6 +20,7 @@ public class CalcHelper { ...@@ -20,6 +20,7 @@ public class CalcHelper {
private static final String REGEX_CALC_MULTIPLY_MOD = "(^.*?)([*/])(.*?$)"; private static final String REGEX_CALC_MULTIPLY_MOD = "(^.*?)([*/])(.*?$)";
private static final String REGEX_CALC_TAG = "[+\\-*/()]+"; private static final String REGEX_CALC_TAG = "[+\\-*/()]+";
private static final String EMPTY_CHAR = " "; private static final String EMPTY_CHAR = " ";
public static String TEMP_VAR_NAME = "@temp";
private static CalcHelper calcInstance = new CalcHelper(); private static CalcHelper calcInstance = new CalcHelper();
...@@ -36,6 +37,18 @@ public class CalcHelper { ...@@ -36,6 +37,18 @@ public class CalcHelper {
double getValue(String parameterName); double getValue(String parameterName);
} }
/**
* 计算公式
*
* @param formula 公式内容,支持括号、空格、数字、+、-、*、/、变量名,如: A * ( B + C )
* @return 运算后的结果
*/
public static double calc(String formula) {
return calcInstance.calRun(formula, StringHelper.EMPTY, null);
}
/** /**
* 计算公式 * 计算公式
* *
...@@ -44,7 +57,7 @@ public class CalcHelper { ...@@ -44,7 +57,7 @@ public class CalcHelper {
* @return 运算后的结果 * @return 运算后的结果
*/ */
public static double calc(String formula, CalcParameter calcParameter) { public static double calc(String formula, CalcParameter calcParameter) {
return calcInstance.calRun(formula, calcParameter); return calcInstance.calRun(formula, StringHelper.EMPTY, calcParameter);
} }
...@@ -52,13 +65,27 @@ public class CalcHelper { ...@@ -52,13 +65,27 @@ public class CalcHelper {
* 计算公式 * 计算公式
* *
* @param formula 公式内容,支持括号、空格、数字、+、-、*、/、变量名,如: A * ( B + C ) * @param formula 公式内容,支持括号、空格、数字、+、-、*、/、变量名,如: A * ( B + C )
* @param tempName 临时变量名称
* @param calcParameter 获取变量值
* @return 运算后的结果
*/
public static double calc(String formula, String tempName, CalcParameter calcParameter) {
return calcInstance.calRun(formula, tempName, calcParameter);
}
/**
* 计算公式
*
* @param formula 公式内容,支持括号、空格、数字、+、-、*、/、变量名,如: A * ( B + C )
* @param tempName 临时变量名称
* @param calcParameter 获取变量值 * @param calcParameter 获取变量值
* @return 运算后的结果 * @return 运算后的结果
*/ */
private double calRun(String formula, CalcParameter calcParameter) { private double calRun(String formula, String tempName, CalcParameter calcParameter) {
if (StringHelper.isEmpty(formula)) { if (StringHelper.isEmpty(formula)) {
return 0; return 0;
} }
tempName = StringHelper.getFirst(tempName, TEMP_VAR_NAME);
// 去掉公式空格 // 去掉公式空格
formula = formula.replaceAll(EMPTY_CHAR, ""); formula = formula.replaceAll(EMPTY_CHAR, "");
// 获取所有的变量名称 // 获取所有的变量名称
...@@ -66,7 +93,7 @@ public class CalcHelper { ...@@ -66,7 +93,7 @@ public class CalcHelper {
// 获取所有变量值 // 获取所有变量值
Map<String, Double> varValues = getVarValues(varNames, calcParameter); Map<String, Double> varValues = getVarValues(varNames, calcParameter);
// 返回计算结果 // 返回计算结果
return calcProc(formula, varValues); return calcProc(formula, tempName, varValues);
} }
/** /**
...@@ -98,9 +125,9 @@ public class CalcHelper { ...@@ -98,9 +125,9 @@ public class CalcHelper {
*/ */
private Map<String, Double> getVarValues(List<String> varNames, CalcParameter calcParameter) { private Map<String, Double> getVarValues(List<String> varNames, CalcParameter calcParameter) {
// 获取所有的变量值 // 获取所有的变量值
Map<String, Double> varValues = new HashMap<>(); Map<String, Double> varValues = new HashMap<>(varNames.size());
for (String name : varNames) { for (String name : varNames) {
if (varValues.containsKey(name)) { if (varValues.containsKey(name) || calcParameter == null) {
continue; continue;
} }
double value = calcParameter.getValue(name); double value = calcParameter.getValue(name);
...@@ -113,37 +140,38 @@ public class CalcHelper { ...@@ -113,37 +140,38 @@ public class CalcHelper {
* 最终计算结果 * 最终计算结果
* *
* @param formula 公式 * @param formula 公式
* @param tempName 临时变量名称
* @param varValues 变量值 * @param varValues 变量值
* @return * @return
*/ */
private double calcProc(String formula, Map<String, Double> varValues) { private double calcProc(String formula, String tempName, Map<String, Double> varValues) {
double ret = 0; double ret = 0;
if (formula.matches(REGEX_QUOT)) { if (formula.matches(REGEX_QUOT)) {
// 获取第一个括号和最后一个括号 // 获取第一个括号和最后一个括号
Matcher matcher = getMatcher(REGEX_QUOT, formula); Matcher matcher = getMatcher(REGEX_QUOT, formula);
double quotResult = calcProc(matcher.group(2), varValues); double quotResult = calcProc(matcher.group(2), tempName, varValues);
String mutualKey = "@temp" + varValues.size(); String mutualKey = TEMP_VAR_NAME + varValues.size();
varValues.put(mutualKey, quotResult); varValues.put(mutualKey, quotResult);
String formulaTo = matcher.group(1) + mutualKey + matcher.group(3); String formulaTo = matcher.group(1) + mutualKey + matcher.group(3);
ret = calcProc(formulaTo, varValues); ret = calcProc(formulaTo, tempName, varValues);
} else if (formula.matches(REGEX_CALC_ADD_PLUS)) { } else if (formula.matches(REGEX_CALC_ADD_PLUS)) {
// 判断是否包含+-运算符号 // 判断是否包含+-运算符号
Matcher matcher = getMatcher(REGEX_CALC_ADD_PLUS, formula); Matcher matcher = getMatcher(REGEX_CALC_ADD_PLUS, formula);
double leftResult = calcProc(matcher.group(1), varValues); double leftResult = calcProc(matcher.group(1), tempName, varValues);
double rightResult = calcProc(matcher.group(3), varValues); double rightResult = calcProc(matcher.group(3), tempName, varValues);
ret = calcItem(matcher.group(2), leftResult, rightResult); ret = calcItem(matcher.group(2), leftResult, rightResult);
} else if (formula.matches(REGEX_CALC_MULTIPLY_MOD)) { } else if (formula.matches(REGEX_CALC_MULTIPLY_MOD)) {
// 判断是否包含*/运算符号 // 判断是否包含*/运算符号
Matcher matcher = getMatcher(REGEX_CALC_MULTIPLY_MOD, formula); Matcher matcher = getMatcher(REGEX_CALC_MULTIPLY_MOD, formula);
double leftResult = calcProc(matcher.group(1), varValues); double leftResult = calcProc(matcher.group(1), tempName, varValues);
double rightResult = calcProc(matcher.group(3), varValues); double rightResult = calcProc(matcher.group(3), tempName, varValues);
ret = calcItem(matcher.group(2), leftResult, rightResult); 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 {
ret = varValues.get(formula); ret = StringHelper.toDouble(varValues.get(formula));
} }
// System.out.println("公式: " + formula + " 值" + ret); System.out.println("公式: " + formula + " 值" + ret);
return ret; return ret;
} }
......
...@@ -22,8 +22,12 @@ public class TestCalcHelper { ...@@ -22,8 +22,12 @@ public class TestCalcHelper {
} }
} }
})); }));
}
@Test
public void test1() {
System.out.println(CalcHelper.calc(" a * 120 + 2 * ( a + b * d )", new CalcHelper.CalcParameter() { System.out.println(CalcHelper.calc("= a * 120 + 2 * ( a + b * d )", new CalcHelper.CalcParameter() {
@Override @Override
public double getValue(String parameterName) { public double getValue(String parameterName) {
switch (parameterName){ switch (parameterName){
......
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