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