#include <iostream> //引入头文件
using namespace std;
typedef char Elemtype;
#define Maxsize 10
#define ERROR 0
#define OK 1
typedef struct
{
Elemtype data[Maxsize];
int top;
}SqStack;
SqStack S;
void InitStack(SqStack& S)
{
S.top = -1;
}
bool StackEmpty(SqStack S)
{
if (S.top == -1) //堆空
return OK;
else //不空
return ERROR;
}
bool Push(SqStack& S, Elemtype x)
{
if (S.top == Maxsize - 1)
return ERROR;
S.data[++S.top] = x;
return OK;
}
bool Pop(SqStack& S, Elemtype& x)
{
if (S.top == -1)
return ERROR;
x = S.data[S.top--];
return OK;
}
bool GetTop(SqStack& S, Elemtype& x)
{
if (S.top == -1)
return ERROR;
x = S.data[S.top];
return OK;
}
//-------------------------------------核心代码-----------------------------------------//
bool BracketsCheck(char* str)
{
InitStack(S); //初始化栈
int i = 0;
char e = 0;
while (str[i] != '\0')
{
switch (str[i])
{
case '(': Push(S, '('); break;
case '[': Push(S, '['); break;
case '{': Push(S, '{'); break;
case ')':
{
Pop(S, e);
if (e != '(') return ERROR;
break;
}
case ']':
{
Pop(S, e);
if (e != '[') return ERROR;
break;
}
case '}':
{
Pop(S, e);
if (e != '{') return ERROR;
break;
}
default: break;
}
i++;
}
if (StackEmpty(S) == 1)
{
cout << "括号匹配" << endl;
return ERROR;
}
else
{
cout << "括号不匹配" << endl;
return OK;
}
}
//-------------------------------------核心代码-----------------------------------------//
int main(void)
{
char a[] = "[](){}";
BracketsCheck(a);
return 0;
}