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

計算結果 = 12.000000