代碼的思路是通過正則判斷計算每個最小的計算單元。以下是代碼:
package cn.com.lawchat.forpublicmvc.util; import java.math.BigDecimal; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 計算器工具類 * @author shuqi * @date 2015-7-23 * @version since 1.0 */ public class CalculatorUtil { public static BigDecimal arithmetic(String exp){ if(!exp.matches(numberString)){ String result = parseExp(exp).replaceAll("[\\[\\]]", ""); return new BigDecimal(result); }else{ return new BigDecimal(exp); } } /** * 最小計數單位 * */ private static String minExp="^((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\+\\-\\*\\/]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))$"; /** * 不帶括號的運算 */ private static String noParentheses="^[^\\(\\)]+$"; /** * 匹配乘法或者除法 */ private static String priorOperatorExp="(((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\*\\/]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\])))"; /** * 匹配加法和減法 */ private static String operatorExp="(((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\+\\-]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\])))"; /** * 匹配只帶一個括號的 */ private static String minParentheses="\\([^\\(\\)]+\\)"; /** * 匹配只帶一個只有半邊括號的 */ private static String baseParentheses="^\\(+[^\\)]+$|^[^\\(]+\\)+$"; /** * 匹配存數字 */ private static String numberString = "\\d+\\.\\d+|\\d+"; /** * 解析計算四則運算表達式,例:2+((3+4)*2-22)/2*3 * @param expression * @return */ private static String parseExp(String expression){ //半邊括號 直接跑異常 if(expression.matches(baseParentheses)){ throw new RuntimeException("計算公式存在錯誤,自由半邊括號!"); } //直接是數字不計算 if(expression.matches(numberString)){ return expression; } //方法進入 先替換空格,在去除運算兩邊的()號 expression=expression.replaceAll("\\s+", "").replaceAll("^\\(([^\\(\\)]+)\\)$", "$1"); //最小表達式計算 if(expression.matches(minExp)){ String result=calculate(expression); return Double.parseDouble(result)>=0?result:"["+result+"]"; } //計算不帶括號的四則運算 if(expression.matches(noParentheses)){ Pattern patt=Pattern.compile(priorOperatorExp); Matcher mat=patt.matcher(expression); if(mat.find()){ String tempMinExp=mat.group(); expression=expression.replaceFirst(priorOperatorExp, parseExp(tempMinExp)); }else{ patt=Pattern.compile(operatorExp); mat=patt.matcher(expression); if(mat.find()){ String tempMinExp=mat.group(); expression=expression.replaceFirst(operatorExp, parseExp(tempMinExp)); } } return parseExp(expression); } //計算帶括號的四則運算 Pattern patt=Pattern.compile(minParentheses); Matcher mat=patt.matcher(expression); if(mat.find()){ String tempMinExp=mat.group(); expression=expression.replaceFirst(minParentheses, parseExp(tempMinExp)); } return parseExp(expression); } /** * 計算最小單位四則運算表達式(兩個數字) * @param exp * @return */ private static String calculate(String exp){ exp=exp.replaceAll("[\\[\\]]", ""); String number[]=exp.replaceFirst("(\\d)[\\+\\-\\*\\/]", "$1,").split(","); BigDecimal number1=new BigDecimal(number[0]); BigDecimal number2=new BigDecimal(number[1]); BigDecimal result=null; String operator=exp.replaceFirst("^.*\\d([\\+\\-\\*\\/]).+$", "$1"); if("+".equals(operator)){ result=number1.add(number2); }else if("-".equals(operator)){ result=number1.subtract(number2); }else if("*".equals(operator)){ result=number1.multiply(number2); }else if("/".equals(operator)){ //第二個參數為精度,第三個為四色五入的模式 result=number1.divide(number2,5,BigDecimal.ROUND_CEILING); } return result!=null?result.toString():null; } }
