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

answer:3.000000