Advertisement
pablosoares

SeuZéPilha

Oct 16th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #define max 10
  5.  
  6. struct stack{
  7.    
  8.     int vet[max];
  9.     int top;
  10.     int qtd[max];
  11.    
  12.     }typedef node;
  13.    
  14.    
  15. struct row{
  16.    
  17.     int vet[20];
  18.     int begin;
  19.     int end;
  20.    
  21.     }typedef fila;
  22.    
  23.        
  24. void iniciar(node *p) {
  25.    
  26.     p->top=-1;
  27.    
  28. }
  29.  
  30.  
  31. void inicia(fila *f){
  32.    
  33.     f->begin=0;
  34.     f->end=0;
  35.    
  36.     }
  37.    
  38.  
  39. void push(node *p,int x){
  40.        
  41.          p->top++;
  42.          p->vet[p->top]=x;
  43.    
  44.     }
  45.    
  46.    
  47. int pop(node *p){
  48.    
  49.     int x;
  50.  
  51.        
  52.        x=p->vet[p->top];
  53.        p->top--;
  54.    
  55.     return x;
  56.      
  57.     }
  58.    
  59.    
  60. void add(fila *f,int x){
  61.      
  62.      f->vet[f->end]=x;
  63.      f->end++;
  64.      
  65.     }
  66.    
  67.    
  68. int remover(fila *f){
  69.    
  70.     int x=f->vet[f->begin];
  71.    
  72.         for(int i=0;i<f->end;i++) f->vet[i]=f->vet[i+1];
  73.                
  74.         f->end--;
  75.        
  76.         return x;
  77.     }
  78.    
  79.        
  80. void chegada(node *p,fila *f,int x,int vet[]){
  81.    
  82.     node aux;
  83.    
  84.     int NaoAchou=1;
  85.    
  86.     iniciar(&aux);
  87.    
  88.     while(p->top!=-1 && aux.vet[aux.top]!=x){
  89.        
  90.           if(p->vet[p->top]==x) NaoAchou=0;
  91.          
  92.           push(&aux,pop(p));
  93.          
  94.          
  95.          }
  96.      
  97.      while(aux.top!=-1) push(p,pop(&aux));
  98.      
  99.      
  100.      if(NaoAchou){
  101.                          
  102.      if((p->top==max-1)) {
  103.          
  104.         if(f->end==20) printf("Fila de espera ta cheia.\n");
  105.        
  106.         else{
  107.                          
  108.              printf("Fila de espera  estacionamento cheio.\n");
  109.              
  110.              vet[f->end]=x;
  111.              add(f,x);
  112.            
  113.             }
  114.                                  
  115.          }
  116.                  
  117.                  
  118.      else {
  119.                      
  120.           printf("Carro estacionado com sucesso.\n");
  121.           push(p,x);
  122.           p->qtd[p->top]=0;
  123.                      
  124.           }
  125.       }
  126.      
  127.      else printf("Carro ja esta estacionado.\n");
  128.    
  129.     }  
  130.  
  131.  
  132. void mostrar1(fila *f){
  133.    
  134.     fila aux;
  135.     int x;
  136.    
  137.     inicia(&aux);
  138.    
  139.         printf(" _________________________________________\n\n");
  140.        
  141.     while(f->end!=f->begin){
  142.        
  143.         x=remover(f);
  144.        
  145.         add(&aux,x);
  146.        
  147.         printf("  Placa: %d\n",x);
  148.        
  149.        
  150.         }
  151.        
  152.         inicia(f);
  153.        
  154.         while(aux.end!=aux.begin) add(f,remover(&aux));
  155.        
  156.        
  157.         printf(" __________________________________________\n");
  158.    
  159.     }
  160.    
  161.    
  162. void mostrar(node *p){
  163.    
  164.     node aux;
  165.    
  166.     iniciar(&aux);
  167.    
  168.         printf(" _________________________________________\n\n");
  169.        
  170.     while(p->top!=-1){
  171.        
  172.         printf("  Placa: %d  Quantidade de movimento: %d\n",p->vet[p->top],p->qtd[p->top]);
  173.         push(&aux,pop(p));
  174.        
  175.         }
  176.        
  177.         while(aux.top!=-1)push(p,pop(&aux));
  178.        
  179.         printf(" _________________________________________\n");
  180.    
  181.     }  
  182.  
  183.    
  184. void partida(node *p,fila *f,int x,int vet[]){
  185.    
  186.     node aux;
  187.    
  188.     int achou=0,c;
  189.    
  190.     iniciar(&aux);
  191.    
  192.     while(p->top!=-1&&p->vet[p->top]!=x){
  193.        
  194.           c=p->qtd[p->top];
  195.           push(&aux,pop(p));
  196.           aux.qtd[aux.top]=c;
  197.        
  198.           if(p->vet[p->top]==x) achou=1;         
  199.          
  200.          }
  201.          
  202.           if(p->vet[p->top]==x) achou=1;
  203.                      
  204.     if(achou){
  205.        
  206.         c=p->qtd[p->top];
  207.        
  208.         printf("Placa: %d\nQuantidade de Movimento: %d\n",pop(p),c);
  209.        
  210.        
  211.         while(aux.top!=-1){
  212.              
  213.              c=aux.qtd[aux.top];
  214.  
  215.              push(p,pop(&aux));
  216.              p->qtd[p->top]=c;
  217.              p->qtd[p->top]++;
  218.                      
  219.            }
  220.            
  221.  
  222.        }
  223.        
  224.        
  225.         else while(aux.top!=-1) push(p,pop(&aux));
  226.        
  227.        
  228.         achou=0;
  229.      
  230.         fila aux1;
  231.    
  232.         inicia(&aux1);
  233.        
  234.         for(int i=0;i<20;i++) if(vet[i]==x)achou=1;
  235.        
  236.         if(achou){
  237.            
  238.         while(f->vet[f->begin]!=x) add(&aux1,remover(f));
  239.        
  240.               printf("Placa: %d\n",remover(f));
  241.               printf("Removido da fila de espera.\n");
  242.    
  243.         while(f->end!=f->begin)add(&aux1,remover(f));
  244.    
  245.    
  246.         while(aux1.end!=aux1.begin){
  247.        
  248.             vet[f->end]=aux1.vet[aux1.begin];
  249.            
  250.             add(f,remover(&aux1));
  251.        
  252.         }
  253.          
  254.        }
  255.        
  256.        
  257.         if(achou==0){
  258.        
  259.        
  260.         printf("Carro nao encontrado no estacionamento.\n");
  261.  
  262.        
  263.         }
  264.    
  265.  
  266.            
  267.             if(f->end!=f->begin)
  268.                 if(p->top<max-1) push(p,remover(f));
  269.                
  270.    
  271.    
  272.    
  273.    
  274.     }
  275.    
  276.  
  277.  
  278. int menu(){
  279.    
  280.     char opc;
  281.     printf(" _________________________________\n");
  282.     printf("|_________Estacionamento__________|\n");
  283.     printf("|                                 |\n");
  284.     printf("|S-Sair                           |\n");
  285.     printf("|C-Chegar                         |\n");
  286.     printf("|P-Partir                         |\n");
  287.     printf("|M-Mostrar                        |\n");
  288.     printf("|_________________________________|\n");
  289.    
  290.     printf("Digite uma opcao: ");
  291.     fflush(stdin);
  292.     scanf("%c",&opc);
  293.     opc = toupper(opc);
  294.    
  295.     return opc;
  296.    
  297.     }
  298.    
  299.        
  300. int main(){
  301.    
  302.    int x,opc,vet[20];
  303.    
  304.    node pilha;
  305.    fila fil;
  306.    
  307.    inicia(&fil);
  308.    iniciar(&pilha);
  309.    
  310.    do{
  311.        
  312.        opc=menu();
  313.        
  314.        switch(opc){
  315.            
  316.            case 'S':
  317.                
  318.                  printf("....Obrigado por usar o programa....\n");
  319.                  
  320.                  break;
  321.                  
  322.            case 'C':
  323.            
  324.                  printf("Digite o numero da placa: ");
  325.                  scanf("%d",&x);
  326.                  
  327.                  chegada(&pilha,&fil,x,vet);
  328.                  
  329.                  break;
  330.                  
  331.            case 'P':
  332.                  
  333.                  printf("Digite o numero da placa: ");
  334.                  scanf("%d",&x);
  335.                  
  336.                  partida(&pilha,&fil,x,vet);
  337.                  
  338.                      
  339.        
  340.                  break;
  341.                  
  342.            case 'M':
  343.            
  344.                   mostrar(&pilha);
  345.                   if(fil.end!=fil.begin)mostrar1(&fil);
  346.                  
  347.                    break;
  348.                  
  349.                  
  350.            default:
  351.            
  352.                   printf("Opcao invalida\n");
  353.                  
  354.                   break;
  355.            
  356.            
  357.            }
  358.            
  359.            system("pause");
  360.            system("cls");
  361.        
  362.        }while(opc!='S');
  363.    
  364.    
  365. return 0;
  366.  
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement