首先要感谢daniel(mstlq)对于我的完整程序的排查。非常非常感谢! #include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h> #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 //存储空间分配增量 #define OK 1 #define ERROR 0 #define OVERFLOW -2 typedef int Status; typedef char SElemType; typedef struct { SElemType *base; //在栈构造之前和销毁之后,base的值为NULL SElemType *top; //栈顶指针 int stacksize; //当前已分配的空间,以元素为单位 }SqStack; Status InitStack (SqStack &S) { //构造一个空栈 S.base = (SElemType *) malloc(STACK_INIT_SIZE * sizeof(SElemType)); if(!S.base) exit(OVERFLOW); //存储分配失败 S.top = S.base; S.stacksize = STACK_INIT_SIZE; return OK; } //InitStack Status Push(SqStack &S, SElemType e) { if(S.top - S.base >= S.stacksize) { //栈满,追加存储空间 S.base = (SElemType *) realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType)); if(!S.base) exit(OVERFLOW); S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; } //插入元素为e的新的栈顶元素 *S.top = e; S.top++; return OK; } //Push Status Pop (SqStack &S, SElemType &e) { //若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR if(S.top == S.base) return ERROR; S.top--; e = *S.top; return OK; } //Pop //堆栈是否为空 Status StackEmpty(SqStack S){ if(S.top != S.base) return ERROR; return OK; } Status bracket(SqStack &S, char *s) { char *p; p = s; int flag = 0, flag2; int i = 0; SElemType e; while( *p != '/0') { switch(*p) { case '(' : Push(S,'('); break; case '[' : Push(S,'['); break; case ')' : { Pop(S,e); printf("/n%s",p); if(e != '(') flag = 1; printf("测试:%d/n",flag); printf("test:%c/n",e); break; } case ']': { Pop(S,e); if(e != '[') flag = 1; printf("输出: %c/n",e); break; } default: break; } if(flag) break; p++; } flag2 = StackEmpty(S); if(!flag && flag2) printf("括号匹配/n"); else printf("括号不匹配/n"); return OK; } void main () { int flag = 1; char temp; while(flag == 1) { char str[200]; SqStack S; printf("请输入字符串:"); scanf("%s",str); scanf("%c",&temp); InitStack(S); bracket(S,str); printf("继续请输入1,停止请输入0:"); scanf("%d",&flag); printf("/n"); } }