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

answer:3.000000