_012_java_正则表达式实现的加减乘除四则运算的计算器

来自:https://blog.csdn.net/flying_fish_wj/article/details/52097903,感谢作者的无私分享。

其中Pattern.compile("\\(([^()]+)\\)")类型的编译模式中的matcher.start()指向的是“(”,而mathcer.end()指向的是“ )”的下一位。

[html]  view plain  copy
  1. import java.util.regex.Matcher;  
  2. import java.util.regex.Pattern;  
  3.   
  4.   
  5. public class Calculator {  
  6.     public static void main(String[] args) {  
  7.         String src = "(3 + (5 - 2) * 10 / 2 * 3 + 15) * (8 - 4)";  
  8.         System.out.println(cal(src));  
  9.     }  
  10.     public static String cal(String src) {  
  11.         StringBuilder builder = new StringBuilder();  
  12.         if (src.contains("(")) {  
  13.             Pattern pattern = Pattern.compile("\\(([^()]+)\\)");  
  14.             Matcher matcher = pattern.matcher(src);  
  15.             int lastEnd = 0;  
  16.             while (matcher.find()) {  
  17.                   
  18.                 builder.append(src.substring(lastEnd, matcher.start()));  
  19.                 System.out.println(builder.toString());  
  20.                 builder.append(cal(matcher.group(1)));  
  21.                 lastEnd = matcher.end();  
  22.             }  
  23.             builder.append(src.substring(lastEnd));  
  24.         } else {  
  25.             Pattern pattern = Pattern.compile("([\\d.]+)\\s*([*/])\\s*([\\d.]+)");  
  26.             builder.append(src);  
  27.             Matcher matcher = pattern.matcher(builder.toString());  
  28.             while (matcher.find()){  
  29.                 float f1 = Float.parseFloat(matcher.group(1));  
  30.                 float f2 = Float.parseFloat(matcher.group(3));  
  31.                 float result = 0;  
  32.                 switch (matcher.group(2)){  
  33.                     case "*":  
  34.                         result = f1 * f2;  
  35.                         break;  
  36.                     case "/":  
  37.                         result = f1 / f2;  
  38.                         break;  
  39.                 }  
  40.                 builder.replace(matcher.start(), matcher.end(),  
  41.                         String.valueOf(result));  
  42.                 matcher.reset(builder.toString());  
  43.             }  
  44.             pattern = Pattern.compile("([\\d.]+)\\s*([+-])\\s*([\\d.]+)");  
  45.             matcher = pattern.matcher(builder.toString());  
  46.             while (matcher.find()){  
  47.                 float f1 = Float.parseFloat(matcher.group(1));  
  48.                 float f2 = Float.parseFloat(matcher.group(3));  
  49.                 float result = 0;  
  50.                 switch (matcher.group(2)){  
  51.                     case "+":  
  52.                         result = f1 + f2;  
  53.                         break;  
  54.                     case "-":  
  55.                         result = f1 - f2;  
  56.                         break;  
  57.                 }  
  58.                 builder.replace(matcher.start(), matcher.end(),  
  59.                         String.valueOf(result));  
  60.                 matcher.reset(builder.toString());  
  61.             }  
  62.             return builder.toString();  
  63.         }  
  64.         System.out.println(builder);  
  65.         return cal(builder.toString());  
  66.     }  
  67. }  

猜你喜欢

转载自blog.csdn.net/poiuyppp/article/details/80739588