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

answer:3.000000