代碼的思路是通過正則判斷計算每個最小的計算單元。以下是代碼:
1 import java.math.BigDecimal; 2 import java.util.regex.Matcher; 3 import java.util.regex.Pattern; 4 5 /** 6 * 計算器工具類 7 * @author shuqi 8 * @date 2015-7-23 9 * @version since 1.0 10 */ 11 public class CalculatorUtil { 12 13 public static BigDecimal arithmetic(String exp){ 14 if(!exp.matches("http://d+")){ 15 String result = parseExp(exp).replaceAll("[//[//]]", ""); 16 return new BigDecimal(result); 17 }else{ 18 return new BigDecimal(exp); 19 } 20 } 21 /** 22 * 最小計數單位 23 * 24 */ 25 PRivate static String minExp="^((//d+(//.//d+)?)|(//[//-//d+(//.//d+)?//]))[//+//-//*///]((//d+(//.//d+)?)|(//[//-//d+(//.//d+)?//]))$"; 26 /** 27 * 不帶括號的運算 28 */ 29 private static String noParentheses="^[^//(//)]+$"; 30 /** 31 * 匹配乘法或者除法 32 */ 33 private static String priorOperatorExp="(((//d+(//.//d+)?)|(//[//-//d+(//.//d+)?//]))[//*///]((//d+(//.//d+)?)|(//[//-//d+(//.//d+)?//])))"; 34 /** 35 * 匹配加法和減法 36 */ 37 private static String operatorExp="(((//d+(//.//d+)?)|(//[//-//d+(//.//d+)?//]))[//+//-]((//d+(//.//d+)?)|(//[//-//d+(//.//d+)?//])))"; 38 /** 39 * 匹配只帶一個括號的 40 */ 41 private static String minParentheses="http://([^//(//)]+//)"; 42 43 /** 44 * 解析計算四則運算表達式,例:2+((3+4)*2-22)/2*3 45 * @param expression 46 * @return 47 */ 48 private static String parseExp(String expression){ 49 //方法進入 先替換空格,在去除運算兩邊的()號 50 expression=expression.replaceAll("http://s+", "").replaceAll("^//(([^//(//)]+)//)$", "$1"); 51 52 //最小表達式計算 53 if(expression.matches(minExp)){ 54 String result=calculate(expression); 55 return Double.parseDouble(result)>=0?result:"["+result+"]"; 56 } 57 //計算不帶括號的四則運算 58 if(expression.matches(noParentheses)){ 59 Pattern patt=Pattern.compile(priorOperatorExp); 60 Matcher mat=patt.matcher(expression); 61 if(mat.find()){ 62 String tempMinExp=mat.group(); 63 expression=expression.replaceFirst(priorOperatorExp, parseExp(tempMinExp)); 64 }else{ 65 patt=Pattern.compile(operatorExp); 66 mat=patt.matcher(expression); 67 68 if(mat.find()){ 69 String tempMinExp=mat.group(); 70 expression=expression.replaceFirst(operatorExp, parseExp(tempMinExp)); 71 } 72 } 73 return parseExp(expression); 74 } 75 76 //計算帶括號的四則運算 77 Pattern patt=Pattern.compile(minParentheses); 78 Matcher mat=patt.matcher(expression); 79 if(mat.find()){ 80 String tempMinExp=mat.group(); 81 expression=expression.replaceFirst(minParentheses, parseExp(tempMinExp)); 82 } 83 return parseExp(expression); 84 } 85 /** 86 * 計算最小單位四則運算表達式(兩個數字) 87 * @param exp 88 * @return 89 */ 90 private static String calculate(String exp){ 91 exp=exp.replaceAll("[//[//]]", ""); 92 String number[]=exp.replaceFirst("(//d)[//+//-//*///]", "$1,").split(","); 93 BigDecimal number1=new BigDecimal(number[0]); 94 BigDecimal number2=new BigDecimal(number[1]); 95 BigDecimal result=null; 96 97 String operator=exp.replaceFirst("^.*//d([//+//-//*///]).+$", "$1"); 98 if("+".equals(operator)){ 99 result=number1.add(number2);100 }else if("-".equals(operator)){101 result=number1.subtract(number2);102 }else if("*".equals(operator)){103 result=number1.multiply(number2);104 }else if("/".equals(operator)){105 //第二個參數為精度,第三個為四色五入的模式106 result=number1.divide(number2,5,BigDecimal.ROUND_CEILING);107 }108 109 return result!=null?result.toString():null;110 }111 112 }
代碼原本是一個博客,但記不得阿紫哪里了,原來代碼沒有注釋而且存在BUG,我稍微修稿了一哈添加了注釋。在這里做個筆記,方便以后用
新聞熱點
疑難解答