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