Advertisement
luciana1237

Untitled

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