user-0 提问: 新手刚学栈，做了个左右括号匹配，但结果怎么都不对，求解我该怎么该

回复:
user: user-1
content: if (!StackEmpty(S))         return ERROR;     else         return OK;  这段是不是写反了，改成下面这样。 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓     if (!StackEmpty(S))         return OK;     else         return  ERROR;

user: user-1
content: OKOKOKOK

user: user-2
content: #include <stdio.h> #include <stdlib.h> #include <malloc.h>  #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define STACK_INIT_SIZE  100 #define STACKINCREMENT 10  typedef int Status; typedef char SElemType;  typedef struct {     SElemType * base;     SElemType * top;     int stacksize; }SqStack;  //栈的存储结构定义 SqStack stack;//定义全局变量  Status InitStack(SqStack *S)     //初始化栈 {     S->base = (SElemType *)         malloc(STACK_INIT_SIZE * sizeof(SElemType));     if (S->base == NULL)         exit(OVERFLOW);     S->top = S->base;     S->stacksize = STACK_INIT_SIZE;     return OK;  }//InitStack  SElemType GetTop(SqStack *S)    //获取栈顶元素 {     int e;     if (S->top == S->base)         return ERROR;     e = *(S->top - 1);     return e;  }// GetTop Status Push(SqStack *S, SElemType e) //元素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;     }     *S->top++ = e;     return OK;  }// Push( Status Pop(SqStack *S, SElemType *e) //出栈，元素存进e {     if (S->top == S->base)         return ERROR;     *e = *(--S->top);     return OK; }//Pop Status ClearStack(SqStack *S)   //清空栈 {     S->top = S->base;     return OK;  }// ClearStack Status DestroyStack(SqStack *S)  //销毁栈 {      free(S->base);     S->base = NULL;     S->top = NULL;     S->stacksize = 0;     return OK;  }// DestroyStack Status StackEmpty(SqStack *S) {//判断空栈     if (S->top == S->base)   /*这儿应该是判断表达式不应该是赋值=*/         return OK;     else         return ERROR; }  int Judge(SqStack *S, SElemType  exp[])   //判断表达式括号是否匹配，若匹配，返回OK，不匹配，返回ERROR {      SElemType e;     int i=0;     for (i = 0; exp[i] != '\0'; i++)     {         switch (exp[i])         {              case '(':case '[':case '{':                 Push(S, exp[i]);                 i++;                 break;             case ')':                 if (!StackEmpty(S) && GetTop(S) == '(')                 {                     Pop(S, &e);                     i++;                 }                 else { return ERROR; }                 break;             case ']':                 if (!StackEmpty(S) && GetTop(S) == '[')                 {                     Pop(S, &e); i++;                 }                 else { return ERROR; }                 break;             case '}':                 if (!StackEmpty(S) && GetTop(S) == '{')                 {                     Pop(S, &e); i++;                 }                 else {                     return ERROR;                 }                 break;         }     }     if (!StackEmpty(S))         return ERROR;     else         return OK;     //DestroyStack(S);   /*放着没意义，也不会运行到*/ }// Judge int main() {     int i, flag;     SElemType express[81];     InitStack(&stack);  //初始化栈     do     {         printf("请输入表达式：\n");         gets(express);         ClearStack(&stack);  //清空栈         i = Judge(&stack, express);         if (i == OK)             printf("该表达式的括号匹配！\n");         else             printf("该表达式的括号不匹配！\n");         printf("1：继续判断,0：退出程序\n");         scanf("%d", &flag);         getchar();     } while (flag != 0);     DestroyStack(&stack);// 销毁栈     system("pause");     return 0; } 以上是修改你的代码，测试了暂没发现问题，建议对比看一下； 问题比较多，主要体现在两个方面： 1. 形参和实参，形参应该定义成指针，这样才能将修改的栈信息带回； 2. if表达式应该是判断表达式，而不应该是赋值表达式=

user: user-3
content: 仅供参考：#include<stdio.h> #include<malloc.h> #include<string.h> #define STACK_INIT_SIZE 10 #define STACK_GROW_SIZE 5 #define ELEMTYPE char #define OK 1 #define ERROR 0 typedef struct { /*建立一个栈的首结点*/     ELEMTYPE * base;     ELEMTYPE * top;     int stacksize; } SpStack; int InitStack(SpStack *s) { /*建立空的栈并返回首地址*/     s->base=((ELEMTYPE*)malloc(STACK_INIT_SIZE*sizeof(ELEMTYPE)));     if (!s->base) return ERROR;     s->top=s->base;     s->stacksize=STACK_INIT_SIZE;     return OK; } int StackEmpty(SpStack *s) { /*判断栈是否为空*/     if (s->top==s->base) return OK;     else                 return ERROR; } int Push(SpStack *s,ELEMTYPE e) { /*往栈顶插入元素即进栈*/     if (s->top-s->base>=s->stacksize) { /*判断是否栈满*/         s->base=((ELEMTYPE*)realloc(s->base,(s->stacksize+STACK_GROW_SIZE)*sizeof(ELEMTYPE)));         if (!s->base) return ERROR;         s->stacksize+=STACK_GROW_SIZE;         s->top=s->base+s->stacksize;     }     *s->top++=e;     return OK; } int Pop(SpStack *s,ELEMTYPE *e) { /*让栈顶元素依次输出即出栈*/     if (StackEmpty(s)) return ERROR;     *e=*(--s->top);     return OK; } int Comp(ELEMTYPE a,ELEMTYPE b) {     if ((a=='('&&b!=')')       ||(a=='['&&b!=']')       ||(a=='{'&&b!='}')) {         return ERROR;     } else return OK; } int Count(SpStack *s) {     ELEMTYPE e[STACK_INIT_SIZE*2];     ELEMTYPE e1;     int i;      InitStack(s);     fgets(e,STACK_INIT_SIZE*2,stdin);     if ('\n'==e[strlen(e)-1]) e[strlen(e)-1]=0;     printf("%s\n",e);     for (i=0;e[i]!='\0';i++) {         switch (e[i]) {         case '(':         case '[':         case '{':             Push(s,e[i]);             break;         case ')':         case ']':         case '}':             if (StackEmpty(s)) {                 printf("%*s↖右括号多余\n",i+1,"");                 return(ERROR);             } else Pop(s,&e1);             if (!Comp(e1,e[i])) {                 printf("%*s↖左右匹配出错\n",i+1,"");                 return(ERROR);             }         }     }     if (!StackEmpty(s)) {         printf("%*s↖左括号多余\n",i,"");         return(ERROR);     } else {         printf("匹配正确\n");         return(OK);     } } void main() {     SpStack s;     Count(&s);     free(s.base); }