#include<stdio.h>
#define MaxSize 256
typedef struct{
char data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack &S){
S.top = -1;
}
bool IsEmpty(SqStack S){
if(S.top == -1)
return true;
else
return false;
}
bool Push(SqStack &S,char c){
if(S.top == MaxSize-1)
return false;
S.data[++S.top] = c;
return true;
}
bool Pop(SqStack &S,char &c){
if(S.top == -1)
return false;
c = S.data[S.top--];
return true;
}
bool BracketsCheck(char *str){
char e;
SqStack S;
InitStack(S);
int i = 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 false;
break;
case ']':Pop(S,e);
if(e != '[') return false;
break;
case '}':Pop(S,e);
if(e != '{') return false;
break;
default:
break;
}
i++;
}
if(!IsEmpty(S)){
printf("Check error!\n");
return false;
}
else{
printf("Check right.\n");
return true;
}
}
int main(){
char str[MaxSize] = {'(','(','{','[',']','}',')',')'};
BracketsCheck(str);
return 0;
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-52566.html