#include<iostream>
#include<stdlib.h>
using namespace std;
#define MaxSize 6
typedef struct {
char data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack& S)
{
S.top = -1;
}
int StackEmpty(SqStack S)
{
if (S.top == -1) return 1;
else return 0;
}
int Push(SqStack &S, char x)
{
if (S.top == MaxSize - 1)
return 0;
S.top = S.top + 1;
S.data[S.top] = x;
return 1;
}
int Pop(SqStack &S, char &x)
{
if (S.top == -1)
return 0;
x = S.data[S.top];
S.top--;
return 1;
}
int GetTop(SqStack &S,char &x)
{
if(S.top==-1)
return 0;
x=S.data[S.top];
return 1;
}
int BracketCheck(char str[], int length)
{
SqStack S;
InitStack(S);
for (int i = 0; i < length; i++)
{
if (str[i] == '(' || str[i] == '[' || str[i] == '{') {
Push(S, str[i]);
}
else
{
if (StackEmpty(S))
return 0;
char topElem;
Pop(S, topElem);
if (str[i] == ')' && topElem != '(')
return 0;
if (str[i] == ']' && topElem != '[')
return 0;
if (str[i] == '}' && topElem != '{')
return 0;
}
}
return StackEmpty(S);
}
int main()
{
cout << "请输入数据(且以'1'为结束点):" << endl;
int i = 0;
char str1[10];
while (1)
{
cout << "请输入第" << i+1 << "个字符:" << endl;
char c;
cin >> c;
if (c == '1')
break;
str1[i] = c;
cout << str1[i] << endl;
i++;
}
int len = 0;
for (int i = 0; i < MaxSize; i++)
{
if (str1[i] == '(' || str1[i] == '[' || str1[i] == '{' ||
str1[i] == ')' || str1[i] == ']' || str1[i] == '}')
len++;
else break;
}
if (BracketCheck(str1, len))
cout << "满足" << endl;
else cout << "不满足" << endl;
return 0;
}
04-08
1641
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-28