国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java中的四則運算

2019-11-15 01:15:25
字體:
來源:轉載
供稿:網友
java中的四則運算

代碼的思路是通過正則判斷計算每個最小的計算單元。以下是代碼:

  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,我稍微修稿了一哈添加了注釋。在這里做個筆記,方便以后用


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石台县| 泸州市| 铜梁县| 温宿县| 阿勒泰市| 陆丰市| 岢岚县| 鲁甸县| 盘锦市| 阿勒泰市| 金平| 三门峡市| 永和县| 泗水县| 汤阴县| 德清县| 诸暨市| 同仁县| 南雄市| 乐陵市| 珲春市| 东光县| 田阳县| 常州市| 遂宁市| 桃园市| 崇明县| 石棉县| 合阳县| 兰溪市| 分宜县| 兴国县| 南丰县| 宁晋县| 葵青区| 开平市| 扬中市| 宣威市| 满城县| 微博| 东山县|