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