fork download
  1. #include <stdio.h>
  2.  
  3. #define SIZE 10
  4.  
  5. double stack[SIZE];
  6. int sp;
  7.  
  8. void push(double value);
  9. double pop(void);
  10. int isFull(void);
  11. int isEmpty(void);
  12. void answer(void);
  13. void reset(void);
  14.  
  15. int main(void)
  16. {
  17. double data;
  18. double cal1, cal2;
  19. int resp;
  20.  
  21. reset();
  22.  
  23. while (1) {
  24. scanf("%d", &resp);
  25. if (resp == 9) {
  26. break;
  27. }
  28.  
  29. switch (resp) {
  30. case 1:
  31. cal1 = pop();
  32. cal2 = pop();
  33. push(cal2 + cal1);
  34. break;
  35.  
  36. case 2:
  37. cal1 = pop();
  38. cal2 = pop();
  39. push(cal2 - cal1);
  40. break;
  41.  
  42. case 3:
  43. cal1 = pop();
  44. cal2 = pop();
  45. push(cal2 * cal1);
  46. break;
  47.  
  48. case 4:
  49. cal1 = pop();
  50. cal2 = pop();
  51. push(cal2 / cal1);
  52. break;
  53.  
  54. case 5:
  55. printf("data:");
  56. scanf("%lf", &data);
  57. push(data);
  58. printf("%lf\n", data);
  59. break;
  60. }
  61. }
  62.  
  63. answer();
  64. return 0;
  65. }
  66.  
  67. void push(double value)
  68. {
  69. if (isFull()) {
  70. printf("スタックが満杯で入りませんでした\n");
  71. } else {
  72. stack[sp++] = value;
  73. }
  74. }
  75.  
  76. double pop(void)
  77. {
  78. if (isEmpty()) {
  79. printf("スタックが空で取り出せませんでした\n");
  80. return 0;
  81. } else {
  82. return stack[--sp];
  83. }
  84. }
  85.  
  86. int isFull(void)
  87. {
  88. if (sp >= SIZE) {
  89. return 1;
  90. } else {
  91. return 0;
  92. }
  93. }
  94.  
  95. int isEmpty(void)
  96. {
  97. if (sp <= 0) {
  98. return 1;
  99. } else {
  100. return 0;
  101. }
  102. }
  103.  
  104. void answer(void)
  105. {
  106. printf("\n");
  107. printf("answer:%lf\n", stack[0]);
  108. }
  109.  
  110. void reset(void)
  111. {
  112. while (!isEmpty()) {
  113. pop();
  114. }
  115. }
Success #stdin #stdout 0s 5312KB
stdin
5
1
5
2
1
9
stdout
data:1.000000
data:2.000000

answer:3.000000