Advertisement
pablosoares

SeuZé

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