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