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