算法实现
#include
#include
#include
#define MaxSize 10
typedef struct{
char data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack &S){
S.top = -1;
}
bool StackEmpty(SqStack S){
if(S.top==-1) return true;
else return false;
}
bool Push(SqStack &S,char x){
if(S.top==MaxSize-1) return false;
S.top = S.top+1;
S.data[S.top]=x;
return true;
}
char Pop(SqStack &S){
if(S.top==-1) return false;
char x;
x = S.data[S.top];
S.top = S.top-1;
return x;
}
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)){
printf("失败1");
return false;
}
char t;
t=Pop(S);
if(str[i]==')' && t!='('){
printf("失败2");
return false;
}
if(str[i]==']' && t!='['){
printf("失败3");
return false;
}
if(str[i]=='}' && t!='{'){
printf("失败4");
return false;
}
}
}
return StackEmpty(S);
}
int main(){
char ch[10];
for(int i=0;i<10;i++){
scanf("%c",&ch[i]);
}
if(bracketCheck(ch,10)){
printf("括号匹配成功");
}
else{
printf("括号不匹配");
}
return 0;
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83