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

answer:3.000000