鱼C论坛

 找回密码
 立即注册
查看: 3376|回复: 3

[已解决]为什么我这个中缀表达式实现输出的时候会出错,明明跟小甲鱼老师的代码一样的

[复制链接]
发表于 2016-3-30 12:58:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>

#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10



typedef char  ElemType;
typedef struct
{
        ElemType *base;
        ElemType *top;
        int stacksize;
} sqStack;

int InitStack(sqStack *s)
{
        s->base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
        if(!s->base)
        {
                exit(0);
         }
        s->top=s->base;
        s->stacksize=STACK_INIT_SIZE;
}       

int Push(sqStack *s,ElemType e)
{
        if(s->top-s->base>=s->stacksize)
        {
                s->base=(ElemType*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(ElemType));
                if(!s->base)
                {
                        exit(0);
                 }
         }
        *(s->top)=e;
        s->top++;
}


int Pop(sqStack *s,ElemType *e)
{
        if(s->top==s->base)
        {
       
         }
         *e=*(--s->top);
}

int StackLen(sqStack *s)
{
        return (s->top-s->base);
}

int main()
{
        sqStack s;
        char c,e;
        InitStack(&s);
        printf("请输入中缀表达式,以#作为结束标志:\n");
        scanf("%c",&c);
        while(c!='#')
        {
                if(c>='0'&&c<='9')
                {
                        printf("%c",c);
                }
                else if(c==')')
                 {
                         printf("%c",c);
                         Pop(&s,&e);
                         printf("%c",e);
                         while('('!=e)
                         {
                                
                                 Pop(&s,&e);
                                printf("%c",e);
                         }
                       
                 }
                 else if('+'==c||'-'==c)
                 {
                         if(!StackLen(&s))
                         {
                                 Push(&s,c);
                         }
                         else
                         {
                                
                                 do
                                 {
                                         Pop(&s,&e);
                                         if('('==e)
                                         {
                                                 Push(&s,e);
                                         }
                                         else
                                         {
                                                
                                                 printf("%c",e);
                                         }
                                         
                                 }while(StackLen(&s)&&'('!=e);
                                 Push(&s,c);
                         }
                 }
                 else if('('==c||'*'==c||'/'==c)
                 {
                         Push(&s,c);
                 }

                 else
                 {
                         printf("error!\n");
                         return -1;
                 }
                scanf("%c",&c);
         }
         getchar();
         
         while(StackLen(&s))
         {
                 Pop(&s,&e);
                 printf("%c",e);
         }
       
}
最佳答案
2023-11-30 08:44:26
有几个问题可能导致输出时出错:

InitStack函数没有返回值。在函数声明中,将函数返回类型改为void即可。

在Pop函数中,当栈为空时,没有进行错误处理。可以在栈为空时添加错误处理的逻辑,例如输出错误信息或返回错误码。

在读取输入时,使用scanf("%c", &c)读取字符,但由于输入缓冲区中可能还有其他字符(如换行符),可能会导致输入不符合预期。可以在读取字符后加上空格符,如scanf(" %c", &c),以消耗掉输入缓冲区中的空白字符。

参考代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <ctype.h>

  5. #define STACK_INIT_SIZE 20
  6. #define STACKINCREMENT 10

  7. typedef char ElemType;
  8. typedef struct
  9. {
  10.     ElemType *base;
  11.     ElemType *top;
  12.     int stacksize;
  13. } sqStack;

  14. void InitStack(sqStack *s)
  15. {
  16.     s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
  17.     if (!s->base)
  18.     {
  19.         exit(0);
  20.     }
  21.     s->top = s->base;
  22.     s->stacksize = STACK_INIT_SIZE;
  23. }

  24. void Push(sqStack *s, ElemType e)
  25. {
  26.     if (s->top - s->base >= s->stacksize)
  27.     {
  28.         s->base = (ElemType *)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(ElemType));
  29.         if (!s->base)
  30.         {
  31.             exit(0);
  32.         }
  33.     }
  34.     *(s->top) = e;
  35.     s->top++;
  36. }

  37. int Pop(sqStack *s, ElemType *e)
  38. {
  39.     if (s->top == s->base)
  40.     {
  41.         return 0; // 栈为空,返回错误码
  42.     }
  43.     *e = *(--s->top);
  44.     return 1;
  45. }

  46. int StackLen(sqStack *s)
  47. {
  48.     return (s->top - s->base);
  49. }

  50. int main()
  51. {
  52.     sqStack s;
  53.     char c, e;
  54.     InitStack(&s);
  55.     printf("请输入中缀表达式,以#作为结束标志:\n");
  56.     scanf(" %c", &c);
  57.     while (c != '#')
  58.     {
  59.         if (c >= '0' && c <= '9')
  60.         {
  61.             printf("%c", c);
  62.         }
  63.         else if (c == ')')
  64.         {
  65.             printf("%c", c);
  66.             if (Pop(&s, &e))
  67.             {
  68.                 printf("%c", e);
  69.                 while (e != '(' && Pop(&s, &e))
  70.                 {
  71.                     printf("%c", e);
  72.                 }
  73.             }
  74.         }
  75.         else if (c == '+' || c == '-')
  76.         {
  77.             if (!StackLen(&s))
  78.             {
  79.                 Push(&s, c);
  80.             }
  81.             else
  82.             {
  83.                 do
  84.                 {
  85.                     if (Pop(&s, &e))
  86.                     {
  87.                         if (e == '(')
  88.                         {
  89.                             Push(&s, e);
  90.                         }
  91.                         else
  92.                         {
  93.                             printf("%c", e);
  94.                         }
  95.                     }
  96.                     else
  97.                     {
  98.                         break;
  99.                     }
  100.                 } while (StackLen(&s) && e != '(');
  101.                 Push(&s, c);
  102.             }
  103.         }
  104.         else if (c == '(' || c == '*' || c == '/')
  105.         {
  106.             Push(&s, c);
  107.         }
  108.         else
  109.         {
  110.             printf("error!\n");
  111.             return -1;
  112.         }
  113.         scanf(" %c", &c);
  114.     }
  115.     getchar();

  116.     while (StackLen(&s))
  117.     {
  118.         if (Pop(&s, &e))
  119.         {
  120.             printf("%c", e);
  121.         }
  122.         else
  123.         {
  124.             break;
  125.         }
  126.     }

  127.     return 0;
  128. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-4-7 16:31:37 From FishC Mobile | 显示全部楼层
你的错误是什么啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-4-11 22:07:57 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-30 08:44:26 | 显示全部楼层    本楼为最佳答案   
有几个问题可能导致输出时出错:

InitStack函数没有返回值。在函数声明中,将函数返回类型改为void即可。

在Pop函数中,当栈为空时,没有进行错误处理。可以在栈为空时添加错误处理的逻辑,例如输出错误信息或返回错误码。

在读取输入时,使用scanf("%c", &c)读取字符,但由于输入缓冲区中可能还有其他字符(如换行符),可能会导致输入不符合预期。可以在读取字符后加上空格符,如scanf(" %c", &c),以消耗掉输入缓冲区中的空白字符。

参考代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <ctype.h>

  5. #define STACK_INIT_SIZE 20
  6. #define STACKINCREMENT 10

  7. typedef char ElemType;
  8. typedef struct
  9. {
  10.     ElemType *base;
  11.     ElemType *top;
  12.     int stacksize;
  13. } sqStack;

  14. void InitStack(sqStack *s)
  15. {
  16.     s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
  17.     if (!s->base)
  18.     {
  19.         exit(0);
  20.     }
  21.     s->top = s->base;
  22.     s->stacksize = STACK_INIT_SIZE;
  23. }

  24. void Push(sqStack *s, ElemType e)
  25. {
  26.     if (s->top - s->base >= s->stacksize)
  27.     {
  28.         s->base = (ElemType *)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(ElemType));
  29.         if (!s->base)
  30.         {
  31.             exit(0);
  32.         }
  33.     }
  34.     *(s->top) = e;
  35.     s->top++;
  36. }

  37. int Pop(sqStack *s, ElemType *e)
  38. {
  39.     if (s->top == s->base)
  40.     {
  41.         return 0; // 栈为空,返回错误码
  42.     }
  43.     *e = *(--s->top);
  44.     return 1;
  45. }

  46. int StackLen(sqStack *s)
  47. {
  48.     return (s->top - s->base);
  49. }

  50. int main()
  51. {
  52.     sqStack s;
  53.     char c, e;
  54.     InitStack(&s);
  55.     printf("请输入中缀表达式,以#作为结束标志:\n");
  56.     scanf(" %c", &c);
  57.     while (c != '#')
  58.     {
  59.         if (c >= '0' && c <= '9')
  60.         {
  61.             printf("%c", c);
  62.         }
  63.         else if (c == ')')
  64.         {
  65.             printf("%c", c);
  66.             if (Pop(&s, &e))
  67.             {
  68.                 printf("%c", e);
  69.                 while (e != '(' && Pop(&s, &e))
  70.                 {
  71.                     printf("%c", e);
  72.                 }
  73.             }
  74.         }
  75.         else if (c == '+' || c == '-')
  76.         {
  77.             if (!StackLen(&s))
  78.             {
  79.                 Push(&s, c);
  80.             }
  81.             else
  82.             {
  83.                 do
  84.                 {
  85.                     if (Pop(&s, &e))
  86.                     {
  87.                         if (e == '(')
  88.                         {
  89.                             Push(&s, e);
  90.                         }
  91.                         else
  92.                         {
  93.                             printf("%c", e);
  94.                         }
  95.                     }
  96.                     else
  97.                     {
  98.                         break;
  99.                     }
  100.                 } while (StackLen(&s) && e != '(');
  101.                 Push(&s, c);
  102.             }
  103.         }
  104.         else if (c == '(' || c == '*' || c == '/')
  105.         {
  106.             Push(&s, c);
  107.         }
  108.         else
  109.         {
  110.             printf("error!\n");
  111.             return -1;
  112.         }
  113.         scanf(" %c", &c);
  114.     }
  115.     getchar();

  116.     while (StackLen(&s))
  117.     {
  118.         if (Pop(&s, &e))
  119.         {
  120.             printf("%c", e);
  121.         }
  122.         else
  123.         {
  124.             break;
  125.         }
  126.     }

  127.     return 0;
  128. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 07:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表