fork(1) 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. while(1){
  18. int resp;
  19. double data,number1,number2;
  20.  
  21. scanf("%d",&resp);
  22.  
  23. if(resp==9)
  24. break;
  25.  
  26. else
  27. switch(resp){
  28. case 5: scanf("%lf",&data);
  29. printf("data:%f\n",data);
  30. push(data);
  31. break;
  32. case 1: number1=pop();
  33. number2=pop();
  34. push(number2+number1);
  35. break;
  36. case 2: number1=pop();
  37. number2=pop();
  38. push(number2-number1);
  39. break;
  40. case 3: number1=pop();
  41. number2=pop();
  42. push(number2*number1);
  43. break;
  44. case 4: number1=pop();
  45. number2=pop();
  46. push(number2/number1);
  47. break;
  48.  
  49.  
  50. }
  51. }
  52.  
  53. answer();
  54.  
  55. return 0;
  56. }
  57.  
  58. void push(double value)
  59. {
  60. if(isFull()==0)
  61. stack[sp++]=value;
  62. else;
  63. }
  64. double pop(void)
  65. {
  66. if(isEmpty()==0)
  67. return stack[--sp];
  68. else
  69. return 0;
  70. }
  71.  
  72. int isFull(void)
  73. {
  74. if(sp>=SIZE)
  75. return 1;//空きがあるなら0ないなら1を返す
  76. else
  77. return 0;
  78. }
  79. int isEmpty(void)
  80. {
  81. if(sp<=0)//空なら1空でないなら0を返す
  82. return 1;
  83. else
  84. return 0;
  85. }
  86. void answer(void)
  87. {
  88. printf("\nanswer:%f",pop());
  89. }
  90. void reset(void)
  91. {
  92. int i;
  93.  
  94. for(i=0;i<=SIZE-1;i++)
  95. stack[i]=0;
  96. sp=0;
  97. }
  98.  
Success #stdin #stdout 0.01s 5276KB
stdin
5 1 5 2 1 9
stdout
data:1.000000
data:2.000000

answer:3.000000