fork download
  1. #include <stdio.h>
  2.  
  3. #define SIZE 10
  4. double stack[SIZE];
  5. int sp;
  6.  
  7. void push(double value);
  8. double pop(void);
  9. int isFull(void);
  10. int isEmpty(void);
  11. void answer(void);
  12. void reset(void);
  13.  
  14. int main(void)
  15. {
  16. int cmd;
  17. double a, b, num;
  18. int isEnd=0;
  19.  
  20. reset();
  21. while (1)
  22. {
  23. scanf("%d", &cmd);
  24.  
  25. switch (cmd)
  26. {
  27. case 1:
  28. if (sp >= 2)
  29. {
  30. b = pop();
  31. a = pop();
  32. push(a + b);
  33. }
  34. else
  35. {
  36. printf("スタックが足りません\n");
  37. }
  38. break;
  39. case 2:
  40. if (sp >= 2)
  41. {
  42. b = pop();
  43. a = pop();
  44. push(a - b);
  45. }
  46. else
  47. {
  48. printf("スタックが足りません\n");
  49. }
  50. break;
  51. case 3:
  52. if (sp >= 2)
  53. {
  54. b = pop();
  55. a = pop();
  56. push(a * b);
  57. }
  58. else
  59. {
  60. printf("スタックが足りません\n");
  61. }
  62. break;
  63. case 4:
  64. if (sp >= 2)
  65. {
  66. b = pop();
  67. a = pop();
  68. if (b != 0)
  69. push(a / b);
  70. else
  71. printf("0で割れません\n");
  72. }
  73. else
  74. {
  75. printf("スタックが足りません\n");
  76. }
  77. break;
  78. case 5:
  79. scanf("%lf", &num);
  80. push(num);
  81. break;
  82. case 9:
  83. isEnd = 1;
  84. }
  85. if (isEnd == 1)
  86. break;
  87. }
  88.  
  89. answer();
  90.  
  91. return 0;
  92.  
  93. }
  94.  
  95. void push(double value)
  96. {
  97. if (!isFull())
  98. {
  99. stack[sp++] = value;
  100. }
  101. else
  102. {
  103. printf("スタックが満杯で入りませんでした\n");
  104. }
  105. }
  106.  
  107. double pop(void)
  108. {
  109. if (!isEmpty())
  110. {
  111. printf("lf",stack[--sp]);
  112. return stack[--sp];
  113. }
  114. else
  115. {
  116. printf("スタックが空で取り出せませんでした\n");
  117. return 0.0;
  118. }
  119. }
  120.  
  121. int isFull(void)
  122. {
  123. return sp >= SIZE;
  124. }
  125.  
  126. int isEmpty(void)
  127. {
  128. return sp <= 0;
  129. }
  130.  
  131. void answer(void)
  132. {
  133. if (!isEmpty())
  134. {
  135. printf("answer: %lf\n", stack[sp - 1]);
  136. }
  137. else
  138. {
  139. printf("スタックが空で何も取り出せませんでした\n");
  140. }
  141. }
  142.  
  143. void reset(void)
  144. {
  145. sp = 0;
  146. }
  147.  
Success #stdin #stdout 0.01s 5320KB
stdin
5
1
5
2
1
9
stdout
lfスタックが空で取り出せませんでした
answer: 1.000000