随笔 - 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 小寻 阅读(1595) 评论(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();
             }

        }

  }
 
}
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: