您的位置:首页 > 其它

栈的应用:括号匹配检验

2018-03-26 21:17 253 查看
整点简单的哈哈哈#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;

int main()
{
stack<char> s;
char c;
int flag=0;
while((c=getchar())!='\n')
{
if(c=='{'||c=='['||c=='(')
{
s.push(c);
continue;

4000
}
if(c=='}')
{
if(!s.empty()&&s.top()=='{')
{
s.pop();
continue;
}
else
{
flag=1;
break;
}
}
if(c==']')
{
if(!s.empty()&&s.top()=='[')
{
s.pop();
continue;
}
else
{
flag=1;
break;
}
}
if(c==')')
{
if(!s.empty()&&s.top()=='(')
{
s.pop();
continue;
}
else
{
flag=1;
break;
}
}
}
if(s.empty()&&flag==0)
cout<<"yes!"<<endl;
else
cout<<"no!"<<endl;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: