fork download
  1. #include <stdio.h>
  2.  
  3. #define SIZE 10
  4. double stack[SIZE];
  5. int sp;
  6.  
  7. // 関数のプロトタイプ宣言
  8. void push(double value);
  9. double pop(void);
  10. int isFull(void);
  11. int isEmpty(void);
  12. void answer(void);
  13. void reset(void);
  14.  
  15. int main(void) {
  16. reset();
  17. while (1) {
  18. int mode;
  19. double a, b;
  20. scanf("%d", &mode);
  21. switch (mode) {
  22. case 1:
  23. b = pop();
  24. a = pop();
  25. push(a + b);
  26. break;
  27. case 2:
  28. b = pop();
  29. a = pop();
  30. push(a - b);
  31. break;
  32. case 3:
  33. b = pop();
  34. a = pop();
  35. push(a * b);
  36. break;
  37. case 4:
  38. b = pop();
  39. a = pop();
  40. push(a / b);
  41. break;
  42. case 5:
  43. scanf("%lf", &a);
  44. push(a);
  45. break;
  46. case 9:
  47. answer();
  48. return 0;
  49. default:
  50. break;
  51. }
  52. }
  53. return 0;
  54. }
  55.  
  56. // (2) push
  57. void push(double value) {
  58. if (isFull()) return;
  59. stack[sp] = value;
  60. sp++;
  61. }
  62.  
  63. // (3) pop
  64. double pop(void) {
  65. if (isEmpty()) return 0;
  66. sp--;
  67. return stack[sp];
  68. }
  69.  
  70. // (4) isFull
  71. int isFull(void) {
  72. return sp >= SIZE;
  73. }
  74.  
  75. // (5) isEmpty
  76. int isEmpty(void) {
  77. return sp <= 0;
  78. }
  79.  
  80. // (6) answer
  81. void answer(void) {
  82. if (!isEmpty()) {
  83. printf("ans = %f\n", stack[sp - 1]);
  84. }
  85. }
  86.  
  87. // (7) reset
  88. void reset(void) {
  89. sp = 0;
  90. }
Success #stdin #stdout 0s 5320KB
stdin
5
3
5
5
1
9
stdout
ans = 8.000000