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

answer:3.000000