Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define max 5
- struct Pilha{
- char letra[max];
- int topo;
- };
- typedef struct Pilha pilha;
- /*void inicia(pilha *p){
- p->topo = 0;
- } */
- void push(pilha *p, char x ){
- if(p->topo < 0)
- p->topo = 0;
- p->letra[p->topo] = x;
- p->topo = p->topo +1;
- }
- char pop(pilha *p){
- char y;
- y = p->letra[p->topo-1];
- p->topo = p->topo - 1;
- return y;
- }
- void show(pilha *p){
- pilha p2;
- p2.topo = 0;
- int y;
- printf("Valores contidos na pilha : \n");
- while(p->topo > 0){
- y = pop(p);
- push(&p2, y);
- }
- while(p2.topo > 0){
- y = pop(&p2);
- push(p, y);
- printf(" |%c|\n", p->letra[p->topo-1]);
- }
- }
- int letra(char x){
- if (x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z'){
- return 1;
- }
- else
- return 0;
- }
- //void show(pilha *p){
- //for(int i = 0; i < 5 ; i++){
- // printf("|%c|\n", p->letra[i]);
- // }
- //}
- void pushO(char x, pilha *p, int count){
- char y;
- int k = 0;
- pilha aux;
- aux.topo = 0;
- if(p->topo == max){
- printf("A pilha ja esta cheia!\n");
- return;
- }
- if(p->topo == 0)
- push(p, x);
- else{
- for(int i = 0; i < p->topo; i++){
- while(k < count){
- if(p->letra[p->topo-1] > x){
- y = pop(p);
- push(&aux, y);
- }
- k++;
- }
- }
- push(p, x);
- while(aux.topo > 0){
- y = pop(&aux);
- push(p, y);
- }
- }
- }
- int main(){
- pilha p;
- p.topo = 0;
- int count = 0;
- int recebe;
- char x;
- int op;
- while(1){
- scanf("%d", &op);
- switch(op){
- case 1:
- fflush(stdin);
- scanf("%c", &x);
- recebe = letra(x);
- if(recebe == 1){
- count++;
- pushO(x, &p, count);
- fflush(stdin);
- }
- else
- printf("caracter invalido\n");
- break;
- case 2:
- show(&p);
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement