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