FormulaHelper.java 7.92 KB
Newer Older
yanzg's avatar
yanzg committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
package com.yanzuoguang.util.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 公式计算
 *
 * @author 颜佐光
 */
public class FormulaHelper {

    private static final String REGEX_DOUBLE = "^[-+]?[0-9]*\\.?[0-9]+$";
    private static final String REGEX_QUOT = "(^.*?)\\((.+?)\\)(.*?$)";
    private static final String REGEX_CALC_ADD_PLUS = "(^.*?)([+\\-])(.*?$)";
    private static final String REGEX_CALC_MULTIPLY_MOD = "(^.*?)([*/])(.*?$)";
    private static final String REGEX_CALC_TAG = "[+\\-*/()]+";
    private static final String EMPTY_CHAR = " ";
    public static String TEMP_VAR_NAME = "@temp";

    private static FormulaHelper calcInstance = new FormulaHelper();

    /**
     * 公式参数获取
     */
    public interface CalcParameter {
        /**
         * 获取参数值
         *
         * @param parameterName 获取参数值的名称
         * @return
         */
        double getValue(String parameterName);
    }


    /**
     * 获取excel列序号
     *
     * @param columnName
     * @return
     */
    public static final int getExcelIndex(String columnName) {
        columnName = columnName.toLowerCase();
        if (!columnName.matches("^[a-z]+$")) {
            throw new RuntimeException("变量名" + columnName + "不支持");
        }
        // 从名称转换列序号
        int formulaColumnIndex = 0;
        char[] chs = new char[columnName.length()];
yanzg's avatar
yanzg committed
55
        columnName.getChars(0, columnName.length(), chs, 0);
yanzg's avatar
yanzg committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
        for (int i = 0; i < chs.length; i++) {
            formulaColumnIndex = formulaColumnIndex * 26 + (chs[i] - 'a' + 1);
        }
        formulaColumnIndex--;
        return formulaColumnIndex;
    }

    /**
     * 计算公式
     *
     * @param formula 公式内容,支持括号、空格、数字、+、-、*、/、变量名,如: A * ( B + C )
     * @return 运算后的结果
     */
    public static double calc(String formula) {
        return calcInstance.calRun(formula, StringHelper.EMPTY, null);
    }


    /**
     * 计算公式
     *
     * @param formula       公式内容,支持括号、空格、数字、+、-、*、/、变量名,如: A * ( B + C )
     * @param calcParameter 获取变量值
     * @return 运算后的结果
     */
    public static double calc(String formula, CalcParameter calcParameter) {
        return calcInstance.calRun(formula, StringHelper.EMPTY, calcParameter);
    }


    /**
     * 计算公式
     *
     * @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 获取变量值
     * @return 运算后的结果
     */
    private double calRun(String formula, String tempName, CalcParameter calcParameter) {
        if (StringHelper.isEmpty(formula)) {
            return 0;
        }
        tempName = StringHelper.getFirst(tempName, TEMP_VAR_NAME);
        // 去掉公式空格
        formula = formula.replaceAll(EMPTY_CHAR, "");
        // 获取所有的变量名称
        List<String> varNames = getVarNames(formula);
        // 获取所有变量值
        Map<String, Double> varValues = getVarValues(varNames, calcParameter);
        // 返回计算结果
        return calcProc(formula, tempName, varValues);
    }

    /**
     * 获取变量表
     *
     * @param formula 输入等式的右边
     **/
    private List<String> getVarNames(String formula) {
        List<String> list = new ArrayList<>();
        //清理所有运算符,并且包含多个运算符号时,将多个运算符号当成一个运算符号处理
        String formulaTo = formula.replaceAll(REGEX_CALC_TAG, EMPTY_CHAR);
        String[] items = formulaTo.split(EMPTY_CHAR);
        for (String item : items) {
            // 判断是否是空字符串、纯数字,是则不属于变量
            if (StringHelper.isEmpty(item) || item.matches(REGEX_DOUBLE)) {
                continue;
            }
            list.add(item);
        }
        return list;
    }

    /**
     * 获取所有变量值
     *
     * @param varNames      变量名称列表
     * @param calcParameter 获取变量值
     * @return 变量值
     */
    private Map<String, Double> getVarValues(List<String> varNames, CalcParameter calcParameter) {
        // 获取所有的变量值
        Map<String, Double> varValues = new HashMap<>(varNames.size());
        for (String name : varNames) {
            if (varValues.containsKey(name) || calcParameter == null) {
                continue;
            }
            double value = calcParameter.getValue(name);
            varValues.put(name, value);
        }
        return varValues;
    }

    /**
     * 最终计算结果
     *
     * @param formula   公式
     * @param tempName  临时变量名称
     * @param varValues 变量值
     * @return
     */
    private double calcProc(String formula, String tempName, Map<String, Double> varValues) {
        double ret = 0;
        if (formula.matches(REGEX_QUOT)) {
            // 获取第一个括号和最后一个括号
            Matcher matcher = getMatcher(REGEX_QUOT, formula);
            double quotResult = calcProc(matcher.group(2), tempName, varValues);
            String mutualKey = TEMP_VAR_NAME + varValues.size();
            varValues.put(mutualKey, quotResult);
            String formulaTo = matcher.group(1) + mutualKey + matcher.group(3);
            ret = calcProc(formulaTo, tempName, varValues);
        } else if (formula.matches(REGEX_CALC_ADD_PLUS)) {
            // 判断是否包含+-运算符号
            Matcher matcher = getMatcher(REGEX_CALC_ADD_PLUS, formula);
            double leftResult = calcProc(matcher.group(1), tempName, varValues);
            double rightResult = calcProc(matcher.group(3), tempName, varValues);
            ret = calcItem(matcher.group(2), leftResult, rightResult);
        } else if (formula.matches(REGEX_CALC_MULTIPLY_MOD)) {
            // 判断是否包含*/运算符号
            Matcher matcher = getMatcher(REGEX_CALC_MULTIPLY_MOD, formula);
            double leftResult = calcProc(matcher.group(1), tempName, varValues);
            double rightResult = calcProc(matcher.group(3), tempName, varValues);
            ret = calcItem(matcher.group(2), leftResult, rightResult);
        } else if (formula.matches(REGEX_DOUBLE)) {
            ret = StringHelper.toDouble(formula);
        } else {
            ret = StringHelper.toDouble(varValues.get(formula));
        }
        System.out.println("公式: " + formula + " 值" + ret);
        return ret;
    }

    /**
     * 匹配正则表达式和公式
     *
     * @param regex   正则表达式
     * @param formula 公式
     * @return 匹配后的结果
     */
    private 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 double calcItem(String flag, double a, double b) {
        switch (flag) {
            case "+":
yanzg's avatar
yanzg committed
219
                return CalcHelper.add(a, b);
yanzg's avatar
yanzg committed
220
            case "-":
yanzg's avatar
yanzg committed
221
                return CalcHelper.sub(a, b);
yanzg's avatar
yanzg committed
222
            case "*":
yanzg's avatar
yanzg committed
223
                return CalcHelper.mul(a, b);
yanzg's avatar
yanzg committed
224
            case "/":
yanzg's avatar
yanzg committed
225
                return CalcHelper.div(a, b);
yanzg's avatar
yanzg committed
226 227 228 229 230 231 232
            default:
                return 0;
        }
    }


}