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. while(1){
  18. int c;
  19. scanf("%d",&c);
  20. switch (c) {
  21. case 1: {
  22. double b=pop();
  23. double a=pop();
  24. push(a+b);
  25. break;
  26. }
  27. case 2: {
  28. double b=pop();
  29. double a=pop();
  30. push(a-b);
  31. break;
  32. }
  33. case 3: {
  34. double b=pop();
  35. double a=pop();
  36. push(a*b);
  37. break;
  38. }
  39. case 4: {
  40. double b=pop();
  41. double a=pop();
  42. if (b==0) {
  43. printf("0で割ることはできません。\n");
  44. reset();
  45. } else {
  46. push(a/b);
  47. }
  48. break;
  49. }
  50. case 5: {
  51. double x;
  52. scanf("%lf",&x);
  53. printf("date:%lf\n",x);
  54. push(x);
  55. break;
  56. }
  57. case 9: {
  58. answer();
  59. return 0;
  60. }
  61. }
  62. }
  63. return 0;
  64. }
  65.  
  66. void push(double value)
  67. {
  68. if(isFull()){
  69. printf("スタックが満杯で入りませんでした。\n");
  70. }
  71. else{
  72. stack[sp++]=value;
  73. }
  74. }
  75.  
  76. double pop(void)
  77. {
  78. if(isEmpty()){
  79. printf("スタックが空で取り出せませんでした\n");
  80. return 0;
  81. }
  82. else{
  83. return stack[--sp];
  84. }
  85. }
  86.  
  87. int isFull(void)
  88. {
  89. return sp >= SIZE;
  90. }
  91.  
  92. int isEmpty(void)
  93. {
  94. return sp <= 0;
  95. }
  96.  
  97. void answer(void)
  98. {
  99. if (sp == 1) {
  100. printf("answer:%lf\n", stack[0]);
  101. } else if (isEmpty()) {
  102. printf("スタックが空です。\n");
  103. } else {
  104. printf("スタックに複数の値が残っています。\n");
  105. }
  106. }
  107.  
  108. void reset(void)
  109. {
  110. sp=0;
  111. }
Success #stdin #stdout 0s 5288KB
stdin
5
1
5
2
1
9
stdout
date:1.000000
date:2.000000
answer:3.000000