首页 > 其他 > 详细

利用栈,判断括号匹配

时间:2020-11-02 16:40:42      阅读:36      评论:0      收藏:0      [点我收藏+]

p65页第4题

思路书上有

参考代码:

import java.util.Stack;

public class c1
{
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        Stack<String> mys=new Stack<String>();
        String a,t;
        a="3*(2+(1/(6+8))-(4+1))";
        for (int i = 0; i < a.length(); i++)
        {
            t=a.substring(i,i+1);
            if(t.equals("("))
            {
                mys.push("x");
            }
            if(t.equals(")"))
            {
                if(mys.empty())
                {
                    System.out.println("sth wrong![( requied]");
                    return;
                }
                mys.pop();
            }
        }
        if(!mys.empty())
        {
            System.out.println("sth wrong![) requied]");
            return;
        }
        System.out.println("ok");
    }
}

自行运行调试

利用栈,判断括号匹配

原文:https://www.cnblogs.com/wanjinliu/p/13895909.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!