括号匹配问题

package 括号匹配;

import org.apache.commons.collections.ArrayStack;
import org.apache.commons.collections.functors.WhileClosure;

public class KuohaopiPei {
    
    
     public static void main(String[] args)  
        {  
        
         print("(a(a(aaaa(a)");
             
        }  
    
    
    public static void print(String exrt)
    {
        ArrayStack s = new ArrayStack();
        int leng = exrt.length();
        for (int i = 0; i < leng; i++) {
            
            if (exrt.charAt(i)=='(') {
                
                s.push(new Integer(i));
            }
            if (exrt.charAt(i)==')') {
                try {
                    
                    
                    System.out.println("match "+s.pop()+" "+i);
                    
                } catch (Exception e) {
                    
                    
                    System.out.println("No match for right ) at"+i);
                    // TODO: handle exception
                }
                
            
            }
            
        }
        
        while (!s.empty()){
        
            System.out.println("no match at left at"+s.pop());
        }
        
        
    
    }
    
    
    
    

}






package 括号匹配;

import java.util.Stack;  

public class ParenthesisMatching  
{  
 
    public boolean check(String str)  
    {  
        Stack<Character> stack = new Stack<Character>();  
        boolean flag = true;  
        for (int i = 0; i < str.length() && flag; i++)  
        {  
            try  
            {  
                switch (str.charAt(i))  
                {  
                case '(':  
                case '[':  
                case '{':  
                    stack.push(str.charAt(i));  
                    break;  
                case ')':  
                    if (stack.pop() != '(')  
                        flag = false;  
                    break;  
                case ']':  
                    if (stack.pop() != '[')  
                        flag = false;  
                    break;  
                case '}':  
                    if (stack.pop() != '{')  
                        flag = false;  
                    break;  
                }  
            }  
            catch (Exception e)  
            {  
                flag = false;  
            }  
        }  
        if (flag && !stack.isEmpty())  
            flag = false;  
 
        return flag;  
    }  
 
    public static void main(String[] args)  
    {  
        ParenthesisMatching pm = new ParenthesisMatching();  
        System.out.println("(): " + pm.check("()"));  
        System.out.println("a(bc[d])e{fd}: " + pm.check("a(bc[d])e{fd}"));  
        System.out.println("a(bc]d: " + pm.check("a(bc]d"));  
        System.out.println("a(b(c)d: " + pm.check("a(b(c)d"));  
        System.out.println("a(b)c)d: " + pm.check("a(b)c)d"));  
    }  
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值