#include<iostream>
#define MaxSize 50
using namespace std;
typedef struct {
char data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack& S)
{
S.top = -1;
}
bool Push(SqStack& S,char x)
{
if (S.top == MaxSize - 1)
return false;
S.data[++S.top] = x;
return true;
}
bool Pop(SqStack& S,char &x)
{
if (S.top == -1)
return false;
x = S.data[S.top--];
return true;
}
bool isEmpty(SqStack& S)
{
if (S.top == -1)
return true;
else
return false;
}
void ShowStack(SqStack& S)
{
while(S.top != -1)
{
cout << S.data[S.top] << ' ';
S.top--;
}
//cout << endl;
}
bool Judge(SqStack& S, char str[],int n)
{
char m;
for (int i = 0; i < n; i++)
{
if (str[i] == '(' || str[i] == '[' || str[i] == '{')
{
Push(S, str[i]);
//ShowStack(S);
}
else
{
if (isEmpty(S))
return false;
Pop(S, m);
//cout << m << ' ';
if (str[i] == ')' && m != '(')
return false;
if (str[i] == ']' && m != '[')
return false;
if (str[i] == '}' && m != '{')
return false;
}
//ShowStack(S);
}
//ShowStack(S);
return isEmpty(S);
}
int main()
{
SqStack S;
InitStack(S);
char str[MaxSize];
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> str[i];
if (Judge(S, str, n))
cout << "匹配成功!" << endl;
else
cout << "匹配失败!" << endl;
return 0;
}
09-29
1749
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-17
2万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交