#include <stdio.h>
#include <stdlib.h>
#define Max 10
typedef struct {
char data[Max];
int top;
}SqStack;
void InitStack(SqStack& S) {
S.top = -1;
}
bool push(SqStack& S, char e) {
if (S.top == Max - 1) {
return false;
}
S.data[++S.top] = e;
return true;
}
bool pop(SqStack& S, char& e) {
if (S.top == -1) {
return false;
}
e = S.data[S.top--];
return true;
}
bool GetTop(SqStack S, char& e) {
if (S.top == -1)
return false;
e = S.data[S.top];
return true;
}
bool StackEmpty(SqStack S) {
if (S.top == -1) {
return true;
}
return false;
}
bool 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 false;
}
else {
char top;
pop(S, top);
if (top == '(' && str[i] != ')')
return false;
if (top == '[' && str[i] != ']')
return false;
if (top == '{' && str[i] != '}')
return false;
}
}
}
return StackEmpty(S);
}
int main() {
char str[] = { '(','(',')',')','{','}'};
if (bracketCheck(str, 6)) {
printf("It is match\n");
}
else {
printf("Mach failed\n");
}
}