鱼C论坛

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

求检查哪里出错了

[复制链接]
发表于 2013-5-9 14:01:12 | 显示全部楼层 |阅读模式

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

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

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

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

  5. typedef double 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. }

  116. // 5 - (6 + 7) * 8 + 9 / 4
  117. // 5 - 13 * 8 + 9 / 4
  118. // 5 6 7 + 8 * - 9 4 / +
复制代码
怎么编译结果这样子?
QQ截图20130509135640.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2013-6-8 14:01:53 | 显示全部楼层
无回帖,不论坛,这才是人道。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-3 02:58:27 | 显示全部楼层
路过学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-3 02:59:26 | 显示全部楼层
路过学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-23 14:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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