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

answer:0.000000