//括号匹配,利用STL中的栈来实现,对(,[,{入栈操作,其余采用出栈操作
#include<iostream>
#include<stack>
using namespace std;
int judge( char a )
{
switch (a)
{
case '[':
case '{':
case '(':
return 1;
default:
return 0;
}
}
char match( char a )
{
switch(a)
{
case '{':
return '}';
case '[':
return ']';
case '(':
return ')';
}
}
int main()
{
char A[10] = {0};
gets(A);
stack<char> S;
int i = 0;
while(A[i]!='\0')
{
if( judge(A[i])==1 )
S.push(A[i]);
else
{
if(match(S.top())==A[i])
{
S.pop();
}
else
{
printf("Wrong\n");
return 0;
}
}
i++;
}
printf("Right\n");
return 0;
}
本文介绍了一种利用C++ STL中的栈数据结构实现括号匹配的方法,包括判断字符、匹配规则及主函数流程。
2408

被折叠的 条评论
为什么被折叠?



