luciana1237

Untitled

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