Advertisement
argentinapb

Untitled

May 2nd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define max 5
  5.  
  6. struct Pilha{
  7. char letra[max];
  8. int topo;
  9. };
  10.  
  11. typedef struct Pilha pilha;
  12.  
  13. /*void inicia(pilha *p){
  14. p->topo = 0;
  15. } */
  16.  
  17.  
  18. void push(pilha *p, char x ){
  19.  
  20. if(p->topo < 0)
  21. p->topo = 0;
  22.  
  23. p->letra[p->topo] = x;
  24. p->topo = p->topo +1;
  25. }
  26.  
  27. char pop(pilha *p){
  28.  
  29. char y;
  30. y = p->letra[p->topo-1];
  31. p->topo = p->topo - 1;
  32.  
  33. return y;
  34. }
  35.  
  36. void show(pilha *p){
  37.  
  38. pilha p2;
  39. p2.topo = 0;
  40. int y;
  41.  
  42.  
  43.  
  44. printf("Valores contidos na pilha : \n");
  45. while(p->topo > 0){
  46. y = pop(p);
  47. push(&p2, y);
  48. }
  49. while(p2.topo > 0){
  50. y = pop(&p2);
  51. push(p, y);
  52. printf(" |%c|\n", p->letra[p->topo-1]);
  53. }
  54. }
  55.  
  56. int letra(char x){
  57.  
  58. if (x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z'){
  59.  
  60. return 1;
  61. }
  62. else
  63. return 0;
  64. }
  65.  
  66. //void show(pilha *p){
  67.  
  68. //for(int i = 0; i < 5 ; i++){
  69. // printf("|%c|\n", p->letra[i]);
  70. // }
  71.  
  72. //}
  73. void pushO(char x, pilha *p, int count){
  74.  
  75.  
  76. char y;
  77. int k = 0;
  78. pilha aux;
  79. aux.topo = 0;
  80.  
  81. if(p->topo == max){
  82. printf("A pilha ja esta cheia!\n");
  83. return;
  84. }
  85. if(p->topo == 0)
  86. push(p, x);
  87. else{
  88. for(int i = 0; i < p->topo; i++){
  89. while(k < count){
  90. if(p->letra[p->topo-1] > x){
  91. y = pop(p);
  92. push(&aux, y);
  93. }
  94. k++;
  95. }
  96. }
  97. push(p, x);
  98.  
  99.  
  100. while(aux.topo > 0){
  101. y = pop(&aux);
  102. push(p, y);
  103. }
  104.  
  105. }
  106. }
  107.  
  108. int main(){
  109.  
  110. pilha p;
  111. p.topo = 0;
  112. int count = 0;
  113.  
  114. int recebe;
  115.  
  116.  
  117. char x;
  118. int op;
  119.  
  120. while(1){
  121. scanf("%d", &op);
  122. switch(op){
  123. case 1:
  124. fflush(stdin);
  125. scanf("%c", &x);
  126.  
  127. recebe = letra(x);
  128.  
  129. if(recebe == 1){
  130. count++;
  131. pushO(x, &p, count);
  132. fflush(stdin);
  133. }
  134.  
  135. else
  136. printf("caracter invalido\n");
  137.  
  138. break;
  139. case 2:
  140. show(&p);
  141. break;
  142. }
  143. }
  144. return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement