Advertisement
luciana1237

Untitled

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