Advertisement
luciana1237

Untitled

Jul 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <time.h>
  5.  
  6. #define MAX 3
  7.  
  8. typedef struct No {
  9.     int x,size,pista[MAX];
  10.    
  11.     struct No *prox;
  12.     struct No *inicio;
  13.     struct No *fim;
  14.  
  15. }fila;
  16.  
  17. int tam=0;
  18.  
  19. int random_id()
  20. {
  21.     /* funçãão que retorna um numero aleatorio */
  22.     srand(time(NULL) *5);
  23.  
  24.     return rand() % 1000;
  25. }
  26.  
  27. fila *fila_cria(void)
  28. {
  29.     fila *p = (fila *) malloc(sizeof(fila));
  30.    
  31.     if (p == NULL)
  32.         fprintf(stderr,"erro");
  33.     else
  34.         p->inicio = NULL;
  35.         p->fim = NULL;
  36.         p->size =0;
  37.     return p;
  38. }
  39.  
  40. bool isEmpty(fila *f)
  41. {
  42.     return (f->prox == NULL);
  43. }
  44.  
  45. bool inserir(fila *f,int p,int IdAviao)
  46. {
  47.     fila *no = (fila *) malloc(sizeof(fila));
  48.    
  49.     if (no == NULL)
  50.         fprintf(stdout," err ");
  51.    
  52.     no->x =p;
  53.     no->pista[p] = IdAviao;
  54.     no->prox = NULL;
  55.    
  56.     if (isEmpty(no)){ /* se a fila estiver vazia */
  57.         printf("vazia \n");
  58.         f->prox= no;
  59.         no->pista[p] = IdAviao;
  60.     }else{ /* se nao */
  61.         fila *tmp;
  62.         tmp = f->prox;
  63.         while(tmp->prox != NULL)
  64.             no->pista[p] = IdAviao;
  65.             tmp = tmp->prox;
  66.             tmp->prox = no; /* novo nóó que vai ser alocado */
  67.        
  68.     }
  69.     tam++;
  70.     f->fim =no;
  71.    
  72.    
  73.     return true;
  74. }
  75.  
  76. void imprime(fila **f)
  77. {
  78.    fila *p;
  79.    p = (*f)->prox;
  80.    int y;
  81.  
  82.    for(y=0; y <= tam; y++){
  83.        printf("%d [%d] \n",p->x,p->pista[0]);
  84.    }
  85. }
  86. bool checkar_pista(fila *f)
  87. {
  88.     fila *no;
  89.     int count=0;
  90.  
  91.     if (!isEmpty(f))
  92.         while(no->prox != NULL){
  93.             count++;
  94.             no = no->prox;
  95.     }
  96.  
  97.     if (count == 3)
  98.         return 0;
  99.         if (count <= 2)
  100.             return 1;
  101. }
  102.  
  103. bool _pouso_(fila *f,int aviao)
  104. {
  105.     fila *no;
  106.     int _n_pista,i,y;
  107.  
  108.     for(y=0; y <=MAX; y++)
  109.         printf("%d \n",no->pista[y]);
  110.  
  111.     do {
  112.         printf("\n Informe o Numero Da Pista Para O Pouso (-1 Para Sair)\n");
  113.         printf("> "); scanf("%d",&_n_pista);
  114.  
  115.             if (no->pista[_n_pista] != 0)
  116.                 printf("Pista Ocupada informe outra pista \n");
  117.             else
  118.                 no->pista[_n_pista] = aviao;
  119.        
  120.     }while(_n_pista != -1);
  121.  
  122.     return 1;
  123.  
  124. }
  125.  
  126.  
  127. int main()
  128. {
  129.     fila *f = fila_cria();
  130.     int num,opc,id = random_id();
  131.  
  132.     for(;;){
  133.  
  134.         printf("(0)---------------- Sair \n");
  135.         printf("(1)---------------- inserir Aviao Para Pouso\n");
  136.         printf("(2)---------------- imprimir \n");
  137.         printf("(3)---------------- checkar pista \n");
  138.         printf("(4)---------------- Pouso \n");
  139.  
  140.         printf("> "); scanf("%d",&opc);
  141.        
  142.         switch(opc){
  143.             case 1:
  144.                printf(" \n Digite o numero da pista para o Pouso \n");
  145.                printf("> "); scanf("%d",&num);
  146.                 inserir(f,num,id);
  147.                 break;
  148.             case 2:
  149.                 imprime(&f);
  150.                 break;
  151.             case 3:
  152.                 checkar_pista(f);
  153.                 break;
  154.             case 4:
  155.                  _pouso_(f,id);
  156.             default:
  157.                 break;
  158.         }
  159.     }
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement