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