fork(1) download
  1. #include<stdio.h>
  2.  
  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.  
  13. int main(void)
  14. {
  15. while(1){
  16. double data;
  17. double cal1, cal2;
  18. int resp;
  19. scanf("%d", &resp);
  20. if( resp == 9 )
  21. break;
  22. switch(resp)
  23. {
  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:"); scanf("%lf", &data);
  41. push( data );
  42. printf("%lf\n", data);
  43. break;
  44. }
  45. }
  46. answer();
  47. return 0;
  48. }
  49.  
  50. void push(double value)
  51. {
  52. if( isFull() ){
  53. printf("スタックが満杯で入らない\n");
  54. }else{
  55. stack[sp++] = value;
  56. }
  57. }
  58.  
  59. double pop(void)
  60. {
  61. if( isEmpty() ){
  62. printf("スタックが空で取り出せない\n");
  63. return 0;
  64. }else{
  65. return stack[--sp];
  66. }
  67. }
  68.  
  69. int isFull(void)
  70. {
  71. if(sp >= SIZE){
  72. return 1;
  73. }else{
  74. return 0;
  75. }
  76. }
  77.  
  78. int isEmpty(void)
  79. {
  80. if(sp <= 0){
  81. return 1;
  82. }else{
  83. return 0;
  84. }
  85. }
  86.  
  87. void answer(void)
  88. {
  89. printf ("\n");
  90. printf ("answer:%lf\n", stack[0]);
  91. }
  92. void reset(void)
  93. {
  94. while(!isEmpty())
  95. pop();
  96. }
  97.  
  98.  
  99.  
Success #stdin #stdout 0s 5320KB
stdin
5
2
5
3
1
9
stdout
data:2.000000
data:3.000000

answer:5.000000