隨筆 - 303  文章 - 883  trackbacks - 0
          <2007年8月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          歡迎光臨! 
          閑聊 QQ:1074961813

          隨筆分類(357)

          我管理的群

          公共blog

          • n維空間
          • Email : java3d@126.com 群 : 12999758

          參與管理的論壇

          好友的blog

          我的其他blog

          朋友的網站

          搜索

          •  

          最新評論

          吃完飯花半個小時寫的 代碼比較簡單的  不做解釋了,呵呵

          a(b(cd)e                 少右括號
          aa(nn)dd)ff           少左括號
          ade(ed))d(            括號不匹配(左右括號個數相等,但不成對)
          (dd(r)e)                  括號匹配

          import java.io.*;

          public class MainText {
              
              
          public static void main(String[] args) {
                 
                  String str
          =null;
              
                     BufferedReader buf 
          = new BufferedReader(new InputStreamReader(System.in)); 
              
                     System.out.print(
          "請輸入字符串:");
              
                     
          try {
                         str 
          = buf.readLine();
              
                     } 
          catch (IOException e1) {
                  
                         e1.printStackTrace();
              
                     }
                   CheckStr checkstr
          =new CheckStr();
                   checkstr.Check(str);

                }         
              
             }
          class CheckStr{

              
          private boolean bool=true;
              
              
          public void Check(String str){
                  
          char[] ch=str.toCharArray();
                   
          int i=0;
                   
          int chi=0;
                   
          int chj=0;
                   
                   
          while(i<ch.length){
                       
          if(ch[i]=='('){
                           chj
          ++;
                       }
                       
          if(ch[i]==')'){
                           chj
          --;
                       }
                     i
          ++;     
                   }
                   i
          =0;
                   
                   
          while(i<ch.length && bool==true){
                                
                       
          while(i<ch.length){
                        
          if(ch[i]=='(' && ch[i]!=')'){
                               chi
          ++;
                           }
          else if(ch[i]==')'){
                               
          break;
                           }     
                           i
          ++;
                       }
                       
                       
          for( ;i<ch.length;i++){
                           
          if(ch[i]==')' && ch[i]!='('){
                               chi
          --;
                           }
          else if(ch[i]=='('){
                               
          break;
                           }
                       }
                       
                       
          if(chi!=0){
                           bool
          =false;
                       }
                       
          if(chi>0&&chj!=0){
                           System.out.println(
          "缺少右括號");
                           
          try {
                               
          throw new Exception("缺少右括號");
                            } 
          catch (Exception e) {
                               e.printStackTrace();
                            }
                       }
                       
          if(chi<0&&chj!=0){
                           System.out.println(
          "缺少左括號");
                           
          try {
                               
          throw new Exception("缺少左括號");
                            } 
          catch (Exception e) {
                               e.printStackTrace();
                            }
                       }
                  }
                    
                   
          if(bool==true){
                       System.out.println(
          "括號是匹配的");
                       
          try {
                           
          throw new Exception("括號是匹配的");
                        } 
          catch (Exception e) {
                           e.printStackTrace();
                        }
                   }
                   
          else if(chj==0){
                       System.out.println(
          "括號是不匹配的");
                       
          try {
                           
          throw new Exception("括號是不匹配的");
                       } 
          catch (Exception e) {
                           e.printStackTrace();
                       }
                  }
            } 
          }



          地震讓大伙知道:居安思危,才是生存之道。
          posted on 2007-08-21 20:05 小尋 閱讀(1619) 評論(3)  編輯  收藏 所屬分類: j2se/j2ee/j2me

          FeedBack:
          # re: [代碼] 判斷字符串中的括號是否對稱 (更改完成) 2007-08-22 20:32 應曉峰
          寫得不錯,頂一下,呵  回復  更多評論
            
          # re: [代碼] 判斷字符串中的括號是否對稱 (更改完成) 2007-08-22 20:38 幻想~@@~
          謝謝  回復  更多評論
            
          # re: [代碼] 判斷字符串中的括號是否對稱 (更改完成) 2007-08-23 14:59 幻想~@@~
          /*
          *加強版
          */

          import java.io.*;

          public class MainText {
              
              
          public static void main(String[] args) {
                 
                  String str
          =null;
              
                     BufferedReader buf 
          = new BufferedReader(new InputStreamReader(System.in)); 
              
                     System.out.print(
          "請輸入字符串:");
              
                     
          try {
                         str 
          = buf.readLine();
              
                     }
           catch (IOException e1) {
                  
                         e1.printStackTrace();
              
                     }

                   CheckStr checkstr
          =new CheckStr();
                   checkstr.Check(str,
          '{','}');
                       
          //在這里你可以使用其他的字符
                }
                   
              
             }

          class CheckStr{

              
          private boolean bool=true;
              
              
          public void Check(String str,char ch1,char ch2){
                  
          char[] ch=str.toCharArray();
                   
          int i=0;
                   
          int chi=0;
                   
          int chj=0;
                   
                   
          while(i<ch.length){
                       
          if(ch[i]==ch1){
                           chj
          ++;
                       }

                       
          if(ch[i]==ch2){
                           chj
          --;
                       }

                     i
          ++;     
                   }

                   i
          =0;
                   
                   
          while(i<ch.length && bool==true){
                                
                       
          while(i<ch.length){
                        
          if(ch[i]==ch1 && ch[i]!=ch2){
                               chi
          ++;
                           }
          else if(ch[i]==ch2){
                               
          break;
                           }
               
                           i
          ++;
                       }

                       
                       
          for( ;i<ch.length;i++){
                           
          if(ch[i]==ch2 && ch[i]!=ch1){
                               chi
          --;
                           }
          else if(ch[i]==ch1){
                               
          break;
                           }

                       }

                       
                       
          if(chi!=0){
                           bool
          =false;
                       }

                       
          if(chi>0&&chj!=0){
                           System.out.println(
          "缺少右掛號");
                           
          try {
                               
          throw new Exception("缺少右掛號");
                            }
           catch (Exception e) {
                               e.printStackTrace();
                            }

                       }

                       
          if(chi<0&&chj!=0){
                           System.out.println(
          "缺少左掛號");
                           
          try {
                               
          throw new Exception("缺少左掛號");
                            }
           catch (Exception e) {
                               e.printStackTrace();
                            }

                       }

                  }

                    
                   
          if(bool==true){
                       System.out.println(
          "掛號是匹配的");
                       
          try {
                           
          throw new Exception("掛號是匹配的");
                        }
           catch (Exception e) {
                           e.printStackTrace();
                        }

                   }

                   
          else if(chj==0){
                       System.out.println(
          "掛號是不匹配的");
                       
          try {
                           
          throw new Exception("掛號是不匹配的");
                       }
           catch (Exception e) {
                           e.printStackTrace();
                       }

                  }

            }
           
          }
            回復  更多評論
            
          主站蜘蛛池模板: 得荣县| 新乐市| 勃利县| 红桥区| 兴文县| 公主岭市| 南丹县| 合阳县| 江城| 海伦市| 丹江口市| 久治县| 政和县| 石渠县| 云浮市| 阿克苏市| 洛川县| 随州市| 大渡口区| 靖安县| 涟水县| 高雄市| 衡阳县| 辽阳县| 盐边县| 普兰店市| 拉萨市| 墨江| 中方县| 扎赉特旗| 兴义市| 房产| 阳谷县| 平安县| 河北区| 柳林县| 河东区| 平陆县| 昔阳县| 东丰县| 巴林左旗|