回 帖 发 新 帖 刷新版面

主题:斑竹帮帮我  个路高手看看晚上写的程序

晚上写的一个题目 改了一段时间 没找出错误 大家看看

#define ERROR 0
#define OK    1
#define TURE  1
#define FALSE 0
#define OVERFLOW -1
#define NULL 0
#define INIT_STACK_SIZE 10
#define STACKINCREMENT  10
typedef  int  SElemType ;
typedef int  status ;
typedef struct {                         /*定义一个栈*/
                 SElemType *base;
                 SElemType *top;
                 int stacksize;

               }SqStack;

 status InitStack(SqStack *S)      /*初始化一个栈*/
   {
      S->base=(SElemType*)malloc(INIT_STACK_SIZE*sizeof(SElemType));
      if(S->base==NULL)
         {
          printf("out of space");
          exit(OVERFLOW);

         }
       S->base=S->top;
       S->stacksize=INIT_STACK_SIZE;
       return OK;

  }

 status PushStack(SqStack *S,SElemType *e) /*进栈操作*/
 {
     if(S->base-S->top>=S->stacksize)
    {
        S->base=(SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
        if(S->base==NULL)
        {
          printf("out of space");
          exit(OVERFLOW);
         }
       S->top=S->base+S->stacksize;
       S->stacksize+=STACKINCREMENT;
    }
    *S->top++=e;
    return OK;
 }

 status PopStack(SqStack *S,SElemType *e)   /*出栈操作*/
 {
    if(S->base==S->top)
       return ERROR;
    *e=*--S->top;
    return OK;

 }
                                         /*判断栈是否为空*/
 status EmptyStack(*S)
 {
     if(S->base==S->top)
      return TURE;
      return FALSE;
 }

  status DestroyStack(*S)   /*销毁一个栈*/
  {
     S->base=S->top;
     return OK;
  }


  main()            /*主函数*/
  {
    int i,M;
    SqStack S;
    char e;
    M=TURE;
    char str[];
    printf("请输入要测试的表达式");      /*输入初始值*/
    scanf("%s",str);

   for(i=0;i<strlen(str)&&M;i++)
      { switch(str[i])
          { case '(':

            case '{':

            case '[':

               PushStack(&S,str[i])
                break;

           case'}':

               if(EmptyStack(&S))
                  {
                   printf("NOT March");
                   M=FALSH;
                   break;
                  }

                 PopStack(&S,&e);
                  if(e=='}')
                   break;

           case']':

               if(EmptyStack(&S))
                  {
                   printf("NOT March");
                   M=FALSH;
                   break;
                  }

                 PopStack(&S,&e);
                 if(e==']')
                   break;

            case')':

               if(EmptyStack(&S))
                  {
                     printf("NOT March");
                     M=FALSH;
                     break;
                  }

               PopStack(&S,&e);
                 if(e==')')
                   break;


       }

      }
        if(EmptyStack(&S))
          { printf(" Match \n");
       printf("Not Match\n");
       DestroyStack(&S)
      system("pause");
      return 0;
      }

    }







回复列表 (共4个回复)

沙发

status InitStack(SqStack *S)      /*初始化一个栈*/
   {
      S->base=(SElemType*)malloc(INIT_STACK_SIZE*sizeof(SElemType));
      if(S->base==NULL)
         {
          printf("out of space");
          exit(OVERFLOW);

         }
       [color=FF0000]S->top=S->base;[/color]
       S->stacksize=INIT_STACK_SIZE;
       return OK;

  }

 status PushStack(SqStack *S,SElemType e) /*进栈操作*/
 {
     if([color=FF0000]S->top-S->base[/color]>=S->stacksize)
    {
        S->base=(SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
        if(S->base==NULL)
        {
          printf("out of space");
          exit(OVERFLOW);
         }
       S->top=S->base+S->stacksize;
       S->stacksize+=STACKINCREMENT;
    }
    *S->top++=e;
    return OK;
 }

 status PopStack(SqStack *S,SElemType e)   /*出栈操作*/
 {
    if(S->base==S->top)
       return ERROR;
    e=*--S->top;
    return OK;

 }
                                         /*判断栈是否为空*/
 status EmptyStack(S)
 {
     if(S->base==S->top)
      return TURE;
      return FALSE;
 }

  status DestroyStack(*S)   /*销毁一个栈*/
  {
     [color=FF0000]return free (S->base);[/color]
  }

板凳

把自己的链表程序做成一个类,以后要用的时候直接拿出来用就行了~~这样也可以减轻调试的负担。。。

3 楼

#include"stdio.h"
#define ERROR 0
#define OK    1
#define TURE  1
#define FALSE 0
#define OVERFLOW -1
#define NULL 0

#define INIT_STACK_SIZE 10
#define STACKINCREMENT  10
typedef  int  SElemType ;
typedef int  status ;
typedef struct {                         /*定义一个栈*/
                 SElemType *base;
                 SElemType *top;
                 int stacksize;

               }SqStack;

 status InitStack(SqStack *S)      /*初始化一个栈*/
   {
      S->base=(SElemType*)malloc(INIT_STACK_SIZE*sizeof(SElemType));
      if(S->base==NULL)
         {
          printf("out of space");
          exit(OVERFLOW);

         }
       S->top=S->base;
       S->stacksize=INIT_STACK_SIZE;
       return OK;

  }

 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==NULL)
        {
          printf("out of space");
          exit(OVERFLOW);
         }
       S->top=S->base+S->stacksize;
       S->stacksize+=STACKINCREMENT;
    }
    *S->top++=e;
    return OK;
 }

 status Pop(SqStack *S,SElemType e)   /*出栈操作*/
 {
    if(S->base==S->top)
       return ERROR;
    e=*--S->top;
    return e;

 }
                                         /*判断栈是否为空*/
 status EmptyStack(SqStack S)
 {
     if(S.base==S.top)
        return TURE;
      return FALSE;
 }

  status DestroyStack(SqStack *S)   /*销毁一个栈*/
  {
     S->base=S->top;
     return OK;
  }


  main()            /*主函数*/
  {
    int i;
    SqStack S;  int Marth;

    char str[20],e;
     Marth=TURE;
    printf("请输入要测试的表达式");      /*输入初始值*/
    scanf("%s",str);

   for(i=0;i<strlen(str)&&Marth;i++)
      { switch(str[i])
          { case '(':

            case '{':

            case '[':

                {Push(&S,str[i]);
                break; }

           case'}':

               if(EmptyStack(S))
                  {

                   Marth=FALSE;
                   break;
                  }

                  e=Pop(&S,e);
                  if(e=='}')
                   break;

           case']':

               if(EmptyStack(S))
                  {

                   Marth=FALSE;
                   break;
                  }

                 Pop(&S,e);
                 if(e==']')
                   break;

            case')':

               if(EmptyStack(S))
                  {

                     Marth=FALSE;
                     break;
                  }

                e=Pop(&S,e);
                 if(e==')')
                   break;
         default: continue;

       }

      }
        if(!EmptyStack(S))
           printf("Not Match \n");
       else
          printf(" Match\n");
       DestroyStack(&S);
      system("pause");
      return 0;
      }

4 楼

改了一下  不过输出结果有点问题 明天在看看 学校要熄灯  要关电脑了  斑竹有空就看看

我来回复

您尚未登录,请登录后再回复。点此登录或注册