java 获取数学函数公式中的参数集

import com.hivescm.billing.exception.BillingSystemException;
import com.hivescm.billing.exception.ExceptionKey;

import java.util.*;

public class MathFormulaUtil {

   public static void main(String[] args) {
      //主方法测试
      try {
         getMathFunctionParamsList("max(weight*price(300,weightUnit)*(s+n)*6 , volume*price(400,volumeUnit))");
         getMathFunctionParamsList("min( a ,price * (a + b ),volume*max(400,volumeUnit)*price)");
      }catch (Exception e){

      }
   }

   /**
    * 获取数学函数公式中的参数集
    * @param functionFormulaString 数学函数公式
    * @return Map<String,Object>
    *     "functionName" -- 返回结果的函数名称(max,min,sum); "funParamList" -- 返回结果的函数参数集
    */
   public static Map<String,Object> getMathFunctionParamsList(String functionFormulaString) throws  Exception{
      //functionName -- 返回结果的函数名称(max,min,sum); funParamList -- 返回结果的函数参数集
      String functionName = "";
      List<String> funParamList = new ArrayList<String>();

      //第一步,消除空格
      String functionFormulaStringNoBlank = functionFormulaString.trim().replace(" ","");
      String isFunctionName = functionFormulaStringNoBlank.substring(0,4);
      //第二步,验证是否是所列函数
      List<String> functionFilters = Arrays.asList("max(","min(","sum(");
      if(functionFilters.contains(isFunctionName)){
         functionName = isFunctionName.substring(0,isFunctionName.length() - 1);
         //第三步,获取字符串的每个字符
         String[] expressArgs = functionFormulaStringNoBlank.split("");
         int leftParenthesisCount = 0;//左括号计数器,用于判断表达式左右括号是否完全匹配
         String funParam = "";//返回结果的函数参数
         StringBuffer funParamBuffer = new StringBuffer("");
         //从第一个左括号开始识别函数参数,遇到‘,’且左括号计数器=1 则结束。期间可能再次遇到左括号或者右括号,需要计数。
         for(int i = 0; i < expressArgs.length; i++){
            if("(".equals(expressArgs[i])){
               leftParenthesisCount += 1;//左括号计数
               if(leftParenthesisCount > 1){
                  funParamBuffer.append(expressArgs[i]);
               }
            }else if(")".equals(expressArgs[i])){
               leftParenthesisCount -= 1;//左括号计数
               if(leftParenthesisCount > 0){
                  funParamBuffer.append(expressArgs[i]);
               }else{
                  funParam = funParamBuffer.toString();
                  funParamList.add(funParam);
                  funParam = "";
                  funParamBuffer.delete(0,funParamBuffer.length());
                  continue;
               }
            }else if(",".equals(expressArgs[i])){
               if(leftParenthesisCount > 1){
                  funParamBuffer.append(expressArgs[i]);
               }else {
                  //leftParenthesisCount=1,结束参数的拼写,返回该参数。
                  funParam = funParamBuffer.toString();
                  funParamList.add(funParam);
                  funParam = "";
                  funParamBuffer.delete(0,funParamBuffer.length());
                  continue;
               }
            }else if(leftParenthesisCount > 0){
               funParamBuffer.append(expressArgs[i]);
            }
         }
         //打印输出结果至控制台
//       if(leftParenthesisCount == 0){//左括号和右括号匹配,公式无问题。
//          System.out.println("函数方法名为:" + functionName);
//          System.out.println("参数集为");
//          for(int i = 0; i < funParamList.size(); i++){
//             System.out.println(funParamList.get(i));
//          }
//       }else{//左括号和右括号不匹配,公式有问题。
//          System.out.println( "左括号和右括号不匹配,公式有问题!" );
//          throw new BillingSystemException(ExceptionKey.SERVICE_ITEM_MESSAGE,"左括号和右括号不匹配,公式有问题!");
//       }
      }
      Map<String,Object> result = new HashMap<String,Object>();
      result.put("functionName",functionName);
      result.put("funParamList",funParamList);
      return result;
   }

}

猜你喜欢

转载自blog.csdn.net/big_bigwolf/article/details/79390926
今日推荐