歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java實現四則運算表達式計算

  1. /** 
  2.  * 四則運算表達式計算 
  3.  * @author penli 
  4.  * 
  5.  */  
  6. public class Arithmetic {  
  7.     public static void main(String args[]){  
  8.         System.out.println(arithmetic("2.2+((3+4)*2-22)/2*3.2"));  
  9.     }  
  10.     public static double arithmetic(String exp){  
  11.         String result = parseExp(exp).replaceAll("[\\[\\]]""");  
  12.         return Double.parseDouble(result);  
  13.     }  
  14.     /** 
  15.      * 解析計算四則運算表達式,例:2+((3+4)*2-22)/2*3 
  16.      * @param expression 
  17.      * @return 
  18.      */  
  19.     public static String parseExp(String expression){  
  20.         //String numberReg="^((?!0)\\d+(\\.\\d+(?<!0))?)|(0\\.\\d+(?<!0))$";   
  21.         expression=expression.replaceAll("\\s+""").replaceAll("^\\((.+)\\)$""$1");  
  22.         String checkExp="\\d";  
  23.         String minExp="^((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\+\\-\\*\\/]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))$";  
  24.         //最小表達式計算   
  25.         if(expression.matches(minExp)){  
  26.             String result=calculate(expression);  
  27.               
  28.             return Double.parseDouble(result)>=0?result:"["+result+"]";  
  29.         }  
  30.         //計算不帶括號的四則運算   
  31.         String noParentheses="^[^\\(\\)]+$";  
  32.         String priorOperatorExp="(((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\*\\/]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\])))";  
  33.         String operatorExp="(((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\+\\-]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\])))";  
  34.         if(expression.matches(noParentheses)){  
  35.             Pattern patt=Pattern.compile(priorOperatorExp);  
  36.             Matcher mat=patt.matcher(expression);  
  37.             if(mat.find()){  
  38.                 String tempMinExp=mat.group();  
  39.                 expression=expression.replaceFirst(priorOperatorExp, parseExp(tempMinExp));  
  40.             }else{  
  41.                 patt=Pattern.compile(operatorExp);  
  42.                 mat=patt.matcher(expression);  
  43.                   
  44.                 if(mat.find()){  
  45.                     String tempMinExp=mat.group();  
  46.                     expression=expression.replaceFirst(operatorExp, parseExp(tempMinExp));  
  47.                 }  
  48.             }  
  49.             return parseExp(expression);  
  50.         }  
  51.         //計算帶括號的四則運算   
  52.         String minParentheses="\\([^\\(\\)]+\\)";  
  53.         Pattern patt=Pattern.compile(minParentheses);  
  54.         Matcher mat=patt.matcher(expression);  
  55.         if(mat.find()){  
  56.             String tempMinExp=mat.group();  
  57.             expression=expression.replaceFirst(minParentheses, parseExp(tempMinExp));  
  58.         }  
  59.         return parseExp(expression);  
  60.     }  
  61.     /** 
  62.      * 計算最小單位四則運算表達式(兩個數字) 
  63.      * @param exp 
  64.      * @return 
  65.      */  
  66.     public static String calculate(String exp){  
  67.         exp=exp.replaceAll("[\\[\\]]""");  
  68.         String number[]=exp.replaceFirst("(\\d)[\\+\\-\\*\\/]""$1,").split(",");  
  69.         BigDecimal number1=new BigDecimal(number[0]);  
  70.         BigDecimal number2=new BigDecimal(number[1]);  
  71.         BigDecimal result=null;  
  72.           
  73.         String operator=exp.replaceFirst("^.*\\d([\\+\\-\\*\\/]).+$""$1");  
  74.         if("+".equals(operator)){  
  75.             result=number1.add(number2);  
  76.         }else if("-".equals(operator)){  
  77.             result=number1.subtract(number2);  
  78.         }else if("*".equals(operator)){  
  79.             result=number1.multiply(number2);  
  80.         }else if("/".equals(operator)){  
  81.             result=number1.divide(number2);  
  82.         }  
  83.           
  84.         return result!=null?result.toString():null;  
  85.     }  
  86. }  
Copyright © Linux教程網 All Rights Reserved