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

answer:3.000000