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

answer:3.000000