鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 小甲鱼

第二十九讲 栈和队列7(视频+课件+源代码)

[复制链接]
发表于 2019-9-12 10:12:16 | 显示全部楼层
xxzj01 发表于 2015-4-14 20:39
中缀转后缀的程序中有个函数Push,总报错。大神帮忙看看错哪了。
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define STACK_INIT_SIZE 20
  4. #define STACKINCREMENT  10

  5. typedef char ElemType;
  6. typedef struct
  7. {
  8.     ElemType *base;
  9.     ElemType *top;
  10.     int stackSize;
  11. }sqStack;

  12. InitStack(sqStack *s)
  13. {
  14.     s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
  15.     if( !s->base )
  16.         exit(0);

  17.     s->top = s->base;
  18.     s->stackSize = STACK_INIT_SIZE;
  19. }

  20. Push(sqStack *s, ElemType e)
  21. {
  22.     // 栈满,追加空间,鱼油必须懂!
  23.     if( s->top - s->base >= s->stackSize )
  24.     {
  25.         s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
  26.         if( !s->base )
  27.             exit(0);

  28.         s->top = s->base + s->stackSize;
  29.         s->stackSize = s->stackSize + STACKINCREMENT;
  30.     }

  31.     *(s->top) = e;      // 存放数据
  32.     s->top++;
  33. }

  34. Pop(sqStack *s, ElemType *e)
  35. {
  36.     if( s->top == s->base )
  37.         return;

  38.     *e = *--(s->top);   // 将栈顶元素弹出并修改栈顶指针
  39. }

  40. int StackLen(sqStack s)
  41. {
  42.     return (s.top - s.base);
  43. }

  44. int main()
  45. {
  46.     sqStack s;
  47.     char c, e;

  48.     InitStack( &s );

  49.     printf("请输入中缀表达式,以#作为结束标志:");
  50.     scanf("%c", &c);

  51.     while( c != '#' )
  52.     {
  53.         while( c>='0' && c<='9' )//输入连续的数字
  54.         {
  55.             printf("%c", c);
  56.             scanf("%c", &c);
  57.             if( c<'0' || c>'9' )
  58.             {
  59.                 printf(" ");
  60.             }
  61.         }

  62.         if( ')' == c )
  63.         {
  64.             Pop(&s, &e);
  65.             while( '(' != e )
  66.             {
  67.                 printf("%c ", e);
  68.                 Pop(&s, &e);
  69.             }
  70.         }
  71.         else if( '+'==c || '-'==c )//加减运算符优先级较小,需要把原本栈中的运算符弹出
  72.         {
  73.             if( !StackLen(s) )//若栈中没有运算符,只能把‘+’,‘-’入栈
  74.             {
  75.                 Push(&s, c);
  76.             }
  77.             else
  78.             {
  79.                 do
  80.                 {
  81.                     Pop(&s, &e);
  82.                     if( '(' == e )
  83.                     {
  84.                         Push(&s, e);
  85.                     }
  86.                     else
  87.                     {
  88.                         printf("%c ", e);
  89.                     }
  90.                 }while( StackLen(s) && '(' != e);
  91.                 Push(&s, c);
  92.             }
  93.         }
  94.         else if( '*'==c || '/'==c || '('==c )
  95.         {
  96.             Push(&s, c);
  97.         }
  98.         else if( '#'== c )
  99.         {
  100.             break;
  101.         }
  102.         else
  103.         {
  104.             printf("\n出错:输入格式错误!\n");
  105.             return -1;
  106.         }

  107.         scanf("%c", &c);
  108.     }

  109.     while( StackLen(s) )//将栈中存储的所有符号弹出
  110.     {
  111.         Pop(&s, &e);
  112.         printf("%c ", e);
  113.     }

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

使用道具 举报

发表于 2019-9-12 10:13:25 | 显示全部楼层
小泉向西流 发表于 2014-7-10 15:11
队列的出入输出作业:

老哥,厉害呀!!!学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 17:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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