//[]()(())([<(]))
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int march(char str[])
{
stack<char> kh;
char ch,temp;
int i,len;
len=strlen(str);
kh.empty();
for(i=0;i<len;i++)
{
ch=str[i];
switch(ch)
{
case '(':
kh.push(ch);
break;
case')':
if(kh.empty()||kh.top()!='(')
{
cout<<"Not Match!"<<endl;
break;
}
else
{
temp=kh.top();
kh.pop();
break;
}
case '[':
kh.push(ch);
break;
case']':
if(kh.empty()||kh.top()!='[')
{
cout<<"Not Match!"<<endl;
break;
}
else
{
temp=kh.top();
kh.pop();
break;
}
case '{':
kh.push(ch);
break;
case'}':
if(kh.empty()||kh.top()!='{')
{
cout<<"Not Match!"<<endl;
break;
}
else
{
temp=kh.top();
kh.pop();
break;
}
}
}
if(kh.empty())
{
cout<<"Match!"<<endl;
}
else
{
cout<<"Not Match!"<<endl;
}
return 0;
}
int main()
{
char temp[20];
cin>>temp;
march(temp);
system("pause");
return 0;
}
栈实现括号匹配
最新推荐文章于 2022-05-10 18:16:32 发布