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