计算字符串四则运算表达式


  4 
  5  import  java.util.regex.Matcher;
  6  import  java.util.regex.Pattern;
  7 
  8 
  9  public   class  ComputeExpression {
 10       /**   
 11       * 计算字符串四则运算表达式
 12        */   
 13       public   static  String computeString(String string){   
 14          String regexCheck  = " [\\(\\)\\d\\+\\-\\*/\\.]* " ; // 是否是合法的表达式   
 15             
 16           if ( ! Pattern.matches(regexCheck, string))   
 17               return  string;   
 18             
 19          Matcher matcher  =   null ;   
 20          String temp  =   "" ;   
 21           int  index  =   - 1 ;   
 22          String regex  =   " \\([\\d\\.\\+\\-\\*/]+\\) " ; // 提取括号表达式   
 23          string  =  string.replaceAll( " \\s " "" ); // 去除空格   
 24           try {   
 25              Pattern pattern  =  Pattern.compile(regex);   
 26               // 循环计算所有括号里的表达式   
 27               while (pattern.matcher(string).find()){   
 28                  matcher  =  pattern.matcher(string);   
 29                   while (matcher.find()){   
 30                      temp  =  matcher.group();   
 31                      index  =  string.indexOf(temp);   
 32                      string  =  string.substring( 0 , index) + computeStirngNoBracket(temp) + string.substring(index + temp.length());   
 33                  }   
 34              }   
 35               // 最后计算总的表达式结果   
 36              string  =  computeStirngNoBracket(string);   
 37          } catch  (NumberFormatException e) {   
 38               return  e.getMessage();   
 39          }   
 40           return  string;   
 41      }   
 42         
 43       /**   
 44       * 计算不包含括号的表达式  
 45        */   
 46       private   static  String computeStirngNoBracket(String string){   
 47          string  =  string.replaceAll( " (^\\()|(\\)$) " "" );   
 48          String regexMultiAndDivision  =   " [\\d\\.]+(\\*|\\/)[\\d\\.]+ " ;   
 49          String regexAdditionAndSubtraction  =   " [\\d\\.]+(\\+|\\-)[\\d\\.]+ " ;   
 50             
 51          String temp  =   "" ;   
 52           int  index  =   - 1 ;   
 53             
 54           // 解析乘除法   
 55          Pattern pattern  =  Pattern.compile(regexMultiAndDivision);   
 56          Matcher matcher  =   null ;   
 57           while (pattern.matcher(string).find()){   
 58              matcher  =  pattern.matcher(string);   
 59               if (matcher.find()){   
 60                  temp  =  matcher.group();   
 61                  index  =  string.indexOf(temp);   
 62                  string  =  string.substring( 0 , index) + doMultiAndDivision(temp) + string.substring(index + temp.length());   
 63              }   
 64          }   
 65             
 66           // 解析加减法   
 67          pattern  =  Pattern.compile(regexAdditionAndSubtraction);   
 68           while (pattern.matcher(string).find()){   
 69              matcher  =  pattern.matcher(string);   
 70               if (matcher.find()){
 71                  temp  =  matcher.group();   
 72                  index  =  string.indexOf(temp);   
 73                  string  =  string.substring( 0 , index) + doAdditionAndSubtraction(temp) + string.substring(index + temp.length());   
 74              }   
 75          }   
 76             
 77           return  string;   
 78      }   
 79         
 80       /**   
 81       * 执行乘除法 
 82        */   
 83       private   static  String doMultiAndDivision(String string){   
 84          String value  =   "" ;   
 85           double  d1  =   0 ;   
 86           double  d2  =   0 ;   
 87          String[] temp  =   null ;   
 88           if (string.contains( " * " )){   
 89              temp  =  string.split( " \\* " );   
 90          } else {   
 91              temp  =  string.split( " / " );   
 92          }   
 93             
 94           if (temp.length < 2 )   
 95               return  string;   
 96             
 97          d1  =  Double.valueOf(temp[ 0 ]);   
 98          d2  =  Double.valueOf(temp[ 1 ]);   
 99           if (string.contains( " * " )){   
100              value = StringUtil.formatDouble(d1 * d2);
101          } else {   
102              value = StringUtil.formatDouble(d1 / d2);   
103          }   
104             
105           return  value;   
106      }   
107         
108       /**   
109       * 执行加减法   
110        */   
111       private   static  String doAdditionAndSubtraction(String string){   
112           double  d1  =   0 ;   
113           double  d2  =   0 ;   
114          String[] temp  =   null ;   
115          String value  =   "" ;   
116           if (string.contains( " + " )){   
117              temp  =  string.split( " \\+ " );   
118          } else {   
119              temp  =  string.split( " \\- " );   
120          }   
121             
122           if (temp.length < 2 )   
123               return  string;   
124             
125          d1  =  Double.valueOf(temp[ 0 ]);   
126          d2  =  Double.valueOf(temp[ 1 ]);   
127           if (string.contains( " + " )){   
128              value =  StringUtil.formatDouble(d1 + d2);
129          } else {   
130              value = StringUtil.formatDouble(d1 - d2);   
131          }   
132             
133           return  value;   
134      }
135       public   static   void  main(String[] args) {
136          System.out.println(ComputeExpression.computeString( " (7)*-/+ " ));
137      }
138  }
139 

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值