0
点赞
收藏
分享

微信扫一扫

括号的匹配

松鼠树屋 2022-01-31 阅读 58

#include<bits/stdc++.h>

using namespace std;

stack<char> s1;

bool check(char *s,int n)
{
	for(int i = 0; i < n; i++)
	{
		char a = s[i];
		switch(a)
		{
			case '(':
			case '[':
				s1.push(a);break;//入栈
			case ')':
				if(s1.top()!='(') return false;	
				s1.pop();break;
			case ']':
				if(s1.top()!='[') return false;	
				s1.pop();break;	
		}		
	}
	if(s1.empty()) return true;
	return false;
}
int main()
{
	char a[1000];
	gets(a);
	int n = strlen(a); 
	if(check(a,n))  printf("合法");
	else printf("非法");
	return 0;
}
举报

相关推荐

0 条评论