鱼C论坛

 找回密码
 立即注册
查看: 3026|回复: 1

求助!中缀转后缀表达式,代码每次遇到符号都会出错。求解决!!!

[复制链接]
发表于 2017-9-18 10:48:11 | 显示全部楼层 |阅读模式

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

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

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

  4. #define STACK_INIT_SIZE 20
  5. #define STACKINFREMENT  10
  6. #define MAXBUFFER       10

  7. typedef double 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 +STACKINFREMENT)*sizeof(ElemType));
  29.         if(!s->base)
  30.         {
  31.             exit(0);
  32.         }
  33.     }

  34.     *(s->top) = e;
  35.     s->top++;
  36. }

  37. void Pop(sqStack *s ,ElemType *e)
  38. {
  39.     if(s->top == s->base)
  40.     {
  41.         return;
  42.     }
  43.     *e= *-- (s->top);
  44. }
  45. int StackLen(sqStack s)
  46. {
  47.     return(s.top - s.base);
  48. }
  49. int main()
  50. {
  51.     sqStack s;
  52.     char c,e;
  53.     InitStack( &s );
  54.     printf("请输入中缀表达式,以#作为结束:");
  55.     scanf("%c",&c);
  56.     while( c != '#')
  57.     {
  58.         if(c >= '0' && c <= '9')
  59.         {
  60.             printf("%c",c);
  61.             scanf("%c",&c);
  62.             if(c < '0' || c > '9')
  63.             {
  64.                 printf(" ");
  65.             }
  66.         }
  67.         else if( ')' == c)
  68.         {
  69.             Pop(&s ,&e);
  70.             while( '(' != e)
  71.             {
  72.                 printf("%c",e);
  73.                 Pop(&s,&e);
  74.             }
  75.         }
  76.         else if('+' == c || '-' == c)
  77.         {
  78.             if(!StackLen(s))
  79.             {
  80.                 Push(&s,c);
  81.             }
  82.             else
  83.             {
  84.                 do
  85.                 {
  86.                     Pop(&s,&e);
  87.                     if('(' == e)
  88.                     {
  89.                         Push(&s,e);
  90.                     }
  91.                     else
  92.                     {
  93.                         printf("%c",e);
  94.                     }
  95.                 }while( StackLen(s) && '(' != e);
  96.                 Push(&s,c);
  97.             }
  98.         }
  99.         else if ('*' == c || '/' == c || '(' == c)
  100.         {
  101.             Push(&s,c);
  102.         }
  103.         else if( '#' == c)
  104.         {
  105.             break;
  106.         }
  107.         else
  108.         {
  109.             printf("\n输入出错!");
  110.             return -1;
  111.         }
  112.         scanf("%c",&c);
  113.     }
  114.     while ( StackLen(s))
  115.     {
  116.         Pop(&s,&e);
  117.         printf("%c ",e);
  118.     }
  119.     return 0;
  120. }
复制代码

哪位大佬看看是哪块出错了?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-9-18 10:54:29 | 显示全部楼层
运行就会显示这个!
2VW8`K%GI_%0]U]6LK8E(K6.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 18:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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