fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. struct Pilha {
  6. int topo;
  7. int capa;
  8. float *pElem;
  9. };
  10.  
  11. void criarpilha(struct Pilha *p, int c) {
  12. p->topo = -1;
  13. p->capa = c;
  14. p->pElem = (float *) malloc(c * sizeof(float));
  15. }
  16.  
  17.  
  18. void push(struct Pilha *p, float v) {
  19. p->topo++;
  20. p->pElem[p->topo] = v;
  21. }
  22.  
  23. float sub(struct Pilha *p) {
  24. int x, y;
  25. float calc;
  26. p->topo--;
  27. x = p->pElem[p->topo];
  28. p->topo++;
  29. y = p->pElem[p->topo];
  30. calc = x - y;
  31. p->topo--;
  32. p->pElem[p->topo] = calc;
  33.  
  34. return calc;
  35. }
  36.  
  37. float mpy(struct Pilha *p) {
  38. int x, y;
  39. float calc;
  40. p->topo--;
  41. x = p->pElem[p->topo];
  42. p->topo++;
  43. y = p->pElem[p->topo];
  44.  
  45. calc = x * y;
  46.  
  47. p->topo--;
  48. p->pElem[p->topo] = calc;
  49.  
  50. return calc;
  51. }
  52.  
  53. float add(struct Pilha *p) {
  54. int x, y;
  55. float calc;
  56. p->topo--;
  57. x = p->pElem[p->topo];
  58. p->topo++;
  59. y = p->pElem[p->topo];
  60. calc = x + y;
  61. p->topo--;
  62. p->pElem[p->topo] = calc;
  63.  
  64. return calc;
  65. }
  66.  
  67.  
  68. float Div(struct Pilha *p) {
  69. int x, y;
  70. float calc;
  71. p->topo--;
  72. x = p->pElem[p->topo];
  73. p->topo++;
  74. y = p->pElem[p->topo];
  75. calc = x / y;
  76. p->topo--;
  77. p->pElem[p->topo] = calc;
  78.  
  79. return calc;
  80. }
  81.  
  82. float dec(struct Pilha *p) {
  83. int x;
  84. p->pElem[p->topo]--;
  85. return p->pElem[p->topo];
  86. }
  87.  
  88.  
  89. float pop(struct Pilha *p) {
  90. float aux = p->pElem[p->topo];
  91. p->topo--;
  92. return aux;
  93. }
  94.  
  95. float monstrarpilha(struct Pilha *p) {
  96. return p->pElem[p->topo];
  97.  
  98. }
  99.  
  100. int main() {
  101.  
  102. struct Pilha p;
  103. int capacidade = 4;
  104. int A = 9, B = 3, C = 2, D = 1, E = 1;
  105.  
  106. criarpilha(&p, capacidade);
  107.  
  108. push(&p, A);
  109. printf("\nPUSH A: %.1f\n", monstrarpilha(&p));
  110.  
  111. push(&p, B);
  112. printf("\nPUSH B: %.1f\n", monstrarpilha(&p));
  113.  
  114. printf("\nSubtracao: %.1f\n", sub(&p));
  115.  
  116. push(&p, C);
  117. printf("\nPUSH C: %.1f\n", monstrarpilha(&p));
  118.  
  119. push(&p, D);
  120. printf("\nPUSH D: %.1f\n", monstrarpilha(&p));
  121.  
  122. push(&p, E);
  123. printf("\nPUSH E: %.1f\n", monstrarpilha(&p));
  124.  
  125. printf("\nmultiplicacao: %.1f\n", mpy(&p));
  126. printf("\nadicao: %.1f\n", add(&p));
  127. printf("\ndecrementar: %.1f\n", dec(&p));
  128. printf("\ndivisao: %.1f\n", Div(&p));
  129.  
  130. printf("\nPOP F%.1f\n", pop(&p));
  131.  
  132. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
PUSH A: 9.0

PUSH B: 3.0

Subtracao: 6.0

PUSH C: 2.0

PUSH D: 1.0

PUSH E: 1.0

multiplicacao: 1.0

adicao: 3.0

decrementar: 2.0

divisao: 3.0

POP F3.0