您的位置:首页 > 理论基础 > 数据结构算法

SDUT OJ 数据结构实验之栈四:括号匹配

2014-12-04 23:17 260 查看

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char a[51],b[51];
int i,top;
while(gets(a)!=NULL)
{
top=-1;
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='{'||a[i]=='['||a[i]=='(')
{
b[++top]=a[i];
}
else if(a[i]=='}')
{
if(b[top]=='{')
{
top--;
}
else
{
break;
}
}
else if(a[i]==']')
{
if(b[top]=='[')
{
top--;
}
else
{
break;
}
}
else if(a[i]==')')
{
if(b[top]=='(')
{
top--;
}
else
{
break;
}
}
}
if(top==-1 && a[i]=='\0')
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  后缀式