SDUT OJ 數據結構實驗之棧四:括號匹配

#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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章