码迷,mamicode.com
首页 > 其他好文 > 详细

D1166:括号匹配

时间:2016-03-03 20:55:22      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:

描述
假设表达式中只包含三种括号:圆括号、方括号和花括号,它们可相互嵌套,如([{}])或({[][()]})等均为正确的格式,而{[]})}或{[()]或([]}均为不正确的格式.

输入一串括号
如果输入的右括号多余,输出:Extra right brackets
如果输入的左括号多余, 输出:Extra left brackets
如果输入的括号不匹配,输出:Brackets not match
如果输入的括号匹配,输出:Brackets match
输入
{{{{)))
输出
Brackets not match
样例输入

{([)]}

样例输出

Brackets not match

 

思路:运用栈结构,左右括号输入时进行判定,并把匹配括号弹出,如果是左括号输入,如果为右括号弹出相应左括号,如果没有匹配括号就输入,计算剩余括号,如果只有右括号数为0,那么左括号剩余,只有左括号为0,那么右括号剩余,左右括号数都不为0,,那么括号不匹配。

 

 

代码:

 

#include
#include
using namespace std;
class Seqstack
{
        private:
                int top;
                char data[20];
        public:
                Seqstack(){top=-1;}
                void Push(char x){data[++top]=x;}
                void Pop(){top--;}
                char Gettop(){if(top!=-1)return data[top];}
                void Match(char a[],int i);
};
void Seqstack::Match(char a[],int i)
{
        int j,m=0,n=0;
        for(j=0;j<</SPAN>i;j++)
        {
                if(top!=-1)//第二个括号开始输入
                {
                        if(a[j]==‘(‘||a[j]==‘[‘||a[j]==‘{‘)
                        {
                                Push(a[j]);//如果为左括号输入
                        }
                        else if(a[j]==‘)‘||a[j]==‘]‘||a[j]==‘}‘)
                        {
                                if(Gettop()==‘(‘&&a[j]==‘)‘)
                                {
                                        Pop();

                            }
                                else if(Gettop()==‘[‘&&a[j]==‘]‘)
                                {
                                        Pop();
                                }
                                else if(Gettop()==‘{‘&&a[j]==‘}‘)
                                {
                                        Pop();
                                }//如果有相应左括号匹配右括号弹出
                                else
                                {
                                        Push(a[j]);
                                }//如果没有匹配的左括号输入
                        }
                }
                else if(top==-1)
                {
                        Push(a[j]);
                }//第一个括号输入
        }
        if(top==-1)
        {
                cout<<"Brackets match"<<endl;
        }//如果栈为空,括号匹配
        else
        {
                while(top!=-1)
                {
                        if(Gettop()==‘(‘||Gettop()==‘[‘||Gettop()==‘{‘)
                        {
                                m++;
                                Pop();
                        }//计算左括号数
                        else
                        {
                                n++;
                                Pop();
                        }//计算右括号数
                }
                if(m==0)
                {
                        cout<<"Extra right brackets"<<endl;
                }//如果左括号数为0,右括号剩余
                else if(n==0)
                {
                        cout<<"Extra left brackets"<<endl;
                }//如果右括号数为0,左括号剩余
                else
                {
                        cout<<"Brackets not match"<<endl;
                }//否则不匹配
        }
}
int main()
{
        Seqstack s;
        char a[20];
        cin>>a;
        int i=strlen(a);
        s.Match(a,i);
        return 0;
}

 

D1166:括号匹配

标签:

原文地址:http://www.cnblogs.com/Joazer/p/5239822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!