[Java数据结构]使用Stack检查表达式中左右括号是否匹配

Stack是一种先进后出的数据结构后,这个特点决定了它在递归向下的场景中有独到的功效。

以下程序展示了它在检查表达式中括号匹配的有效性:

程序:

复制代码
package com.heyang.util;

import java.util.Collections;
import java.util.Stack;

class Bracket{
    char type;
    int idx;
    
    public Bracket(char type,int idx) {
        this.type=type;
        this.idx=idx;
    }
}

// Used to check if the brackets in a expression is balanced
public class BracketChecker {
    private String errMsg;
    
    public String getErrMsg() {
        return errMsg;
    }
    
    public boolean isBalanced(String expression) {
        boolean retval=false;
        
        try {
            retval=checkBalanced(expression);
        }catch(Exception ex) {
            errMsg=ex.getMessage();
        }
        
        return retval;
    }
    
    private boolean checkBalanced(String expression) throws Exception{
        int length=expression.length();
        Stack<Bracket> stack=new Stack<Bracket>();
        
        for(int i=0;i<length;i++){
            char ch=expression.charAt(i);
            
            if(ch=='{' || ch=='[' || ch=='('){
                stack.push(new Bracket(ch,i));
            }else if(ch==')' || ch==']' || ch=='}'){
                if(stack.isEmpty()) {
                    throw new Exception(buildErrorMsg(expression,ch,i));
                }else {
                    Bracket popedBracket=stack.pop();
                    
                    if( (ch==')' && popedBracket.type !='(') || 
                        (ch==']' && popedBracket.type !='[') || 
                        (ch=='}' && popedBracket.type !='{') ){
                        throw new Exception(buildErrorMsg(expression,popedBracket.type,popedBracket.idx));
                        //throw new Exception(buildErrorMsg(expression,ch,i));
                    }
                }
            }
        }
        
        Bracket popedBracket=null;
        while(stack.isEmpty()==false) {
            popedBracket=stack.pop();
        }
        if(popedBracket!=null) {
            throw new Exception(buildErrorMsg(expression,popedBracket.type,popedBracket.idx));
        }
        
        return true;
    }
    
    // build error message
    private String buildErrorMsg(String expression,char ch,int idx) {
        StringBuilder sb=new StringBuilder();
        sb.append(""+expression+"\n");
        sb.append(createRepeatedStr(" ",idx)+"^");
        sb.append(" This bracket '"+ch+"' has not matched bracket!\n");
        
        return sb.toString();
    }
    
    // Repeat seed with n times
    private static String createRepeatedStr(String seed,int n) {
        return String.join("", Collections.nCopies(n, seed));
    }
    
    // Entry point
    public static void main(String[] args) {
        BracketChecker bbc=new BracketChecker();
        
        String[] arr= {"123)456","5+2*(3+3)","{[(2+4)*8]/6}","[()]}","{[(]}","{[](","((((())))))","((([((())))))","[[[[[]]]]]","{[(((((()))))))]}"};
        
        int index=0;
        for(String expression:arr) {
            index++;
            
            boolean balanced=bbc.isBalanced(expression);
            if(!balanced) {
                System.out.println("#"+index+"\n"+bbc.getErrMsg());
            }
        }
    }
}
复制代码

输出:

复制代码
#1
123)456
   ^ This bracket ')' has not matched bracket!

#4
[()]}
    ^ This bracket '}' has not matched bracket!

#5
{[(]}
  ^ This bracket '(' has not matched bracket!

#6
{[](
^ This bracket '{' has not matched bracket!

#7
((((())))))
          ^ This bracket ')' has not matched bracket!

#8
((([((())))))
   ^ This bracket '[' has not matched bracket!

#10
{[(((((()))))))]}
 ^ This bracket '[' has not matched bracket!
复制代码

--2020年5月23日--

posted @   逆火狂飙  阅读(470)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2018-12-24 【高中数学/导数】函数f(x)=cosx+(x+1)sinx+1在区间[0,2*PI]的最小值,最大值分别是?(全国统考高考真题)
2018-12-24 【高中数学/三角函数】设x,y为实数,若4x^2+y^2+xy=1,求2x+y的最大值?
2017-12-24 证明:一个数的各位数之和能被3整除,则该数能被3整除
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示