Advertisement
luciana1237

AVIAO 14:05

Jul 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.38 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_PISTAS 3
  8. #define MAX_PRATELEIRAS 4
  9.  
  10. typedef struct No {
  11.  
  12.     unsigned int fall,prateleiras[MAX_PRATELEIRAS];
  13.     struct No *prox;
  14.  
  15. }elem;
  16.  
  17. struct Fila{
  18.  
  19.     elem *inicio;
  20.     elem *fim;
  21.  
  22. };
  23.  
  24. typedef struct Node {
  25.     unsigned int prateleiras1[MAX_PRATELEIRAS];
  26.     struct Node *_next;
  27. }decolar;
  28.  
  29. struct Queue {
  30.     decolar *begin;
  31.     decolar *end;
  32. };
  33.  
  34. typedef struct Fila fila;
  35. typedef struct Queue queue;
  36.  
  37.  
  38. int size=0,_size=0;
  39. int pista[MAX_PISTAS];
  40.  
  41. int random_id()
  42. {
  43.     srand(time(NULL));
  44.  
  45.     return rand() %1000;
  46. }
  47.  
  48.  
  49. fila *fila_cria(void)
  50. {
  51.     fila *f= (fila *) malloc(sizeof(fila));
  52.  
  53.     if (f != NULL)
  54.         f->inicio = NULL;
  55.         f->fim = NULL;
  56.     return f;
  57.  
  58. }
  59. queue *queue_create(void)
  60. {
  61.     queue *q = (queue *) malloc(sizeof(queue));
  62.  
  63.     if (q != NULL)
  64.         q->begin = NULL;
  65.         q->end = NULL;
  66.     return q;
  67. }
  68.  
  69. bool isEmpty(fila *f)
  70. {
  71.     return (f->inicio == NULL);
  72. }
  73. bool Empty(queue *q)
  74. {
  75.     return (q->begin == NULL);
  76. }
  77.  
  78. bool aterrisar(fila *f,int p,int idAviao)
  79. {
  80.     elem *no = (elem *) malloc(sizeof(elem));
  81.     int y,r,Mm=0,clock;
  82.     no->fall =0;
  83.     no->prox = NULL;
  84.  
  85.     if (no == NULL)
  86.     {
  87.         fprintf(stderr," erro \n"); exit( 0 );
  88.    
  89.     }
  90.  
  91.     srand(time(NULL));
  92.  
  93.     clock = rand() % 10;
  94.  
  95.     for(y=clock; y >=0; y--){
  96.        
  97.         sleep( 1 );
  98.        
  99.         #ifdef linux
  100.             system("clear");
  101.         #else
  102.             system("cls");
  103.             #endif
  104.  
  105.         printf("\n\t-=========================================-\n");
  106.         printf("\n\t|TEMPO PREVISTO PARA PROXIMA ATERRISAGEM  |\n\n");
  107.         printf("\n\t| TEMPO: \t%d                             |\n",y);
  108.         printf("\n\t-=========================================- \n");
  109.  
  110.         if (y == 0){
  111.             Mm =Mm+1;
  112.             if (Mm == 1)
  113.                 r = rand() % 4;
  114.  
  115.                 if (r == 2 || r== 3 || r==4){
  116.                    
  117.                     printf("\n\t********************************\n");
  118.                     printf("\n\t|[+] AVIAO => %d Aterrisando [+]|\n",idAviao);
  119.                     printf("\n\t********************************");
  120.                     sleep( 2 );
  121.                     //no->prox = NULL;
  122.                     no->prateleiras[ p ] =  idAviao;
  123.                    
  124.                     printf("\n[+] AVIAO: %d ATERRISADO COM SUCESSO!!! [+]\n",idAviao);
  125.  
  126.                     if (isEmpty(f))
  127.                         f->inicio = no;
  128.                     else
  129.                         f->fim->prox = no;
  130.                 }else {
  131.                     sleep( 3 );
  132.                     printf("\n\t[-] AVIAO %d CAIU!! [-]\n",idAviao);
  133.                     no->fall++;
  134.                     size--;
  135.                 }
  136.         }
  137.     }
  138.     size++;
  139.     f->fim = no;
  140.     return true;
  141. }
  142.  
  143. bool decolar_aviao(queue *q,int p, int IdAviao)
  144. {
  145.     decolar *node = (decolar *) malloc(sizeof(decolar));
  146.     int y,r,clock,Mm;
  147.     clock = rand() %10;
  148.  
  149.     srand(time(NULL));
  150.  
  151.     if (node == NULL)
  152.     {
  153.         fprintf(stderr, " error "); exit( 0 );
  154.     }
  155.     node->_next = NULL;
  156.  
  157.     for(y=clock; y >=0; y--){
  158.         sleep ( 1 );
  159.  
  160.         printf("\n\t ==========================================\n");
  161.         printf("\n\t| TEMPO PREVISTO PARA PROXIMA DECOLAGEM:   |\n\n");
  162.         printf("\n\t| TEMPO: \t%d                              |\n",y);
  163.         printf("\n\t ========================================== \n");
  164.        
  165.         if (y== 0){
  166.             Mm = Mm+1;
  167.             if (Mm == 1)
  168.                 r = rand() % 6;
  169.  
  170.                 if (r == 1 || r==2 || r== 3 || r==4 || r==5){
  171.                     sleep( 2 );
  172.                     printf("\n\t****************************\n");
  173.                     printf("\n\t| AVIAO => %d Decolando...  |\n",IdAviao);
  174.                     printf("\n\t****************************");
  175.  
  176.                     node->prateleiras1[ p ] = IdAviao;
  177.                     printf("\nAVIAO => %d DECOLADO COM SUCESSO \n",IdAviao);
  178.  
  179.                     if (Empty(q))
  180.                         q->begin =node;
  181.                     else
  182.                         q->end->_next = node;
  183.                 }else {
  184.                     sleep( 1 );
  185.                     printf("\n\n[-] AVIAO SEM COMBUSTIVEL [-]\n\n");
  186.                     _size--;
  187.                 }
  188.         }
  189.     }
  190.     q->end = node;
  191.     _size++;
  192.     return true;
  193.  
  194. }
  195.  
  196. void mostra_pouso(fila *f)
  197. {
  198.     int i;
  199.  
  200.     printf("\n***********************\n");
  201.     printf("\n|---------POUSO--------|\n");
  202.     printf("\n************************\n");
  203.    
  204.     sleep( 1 );
  205.  
  206.     for(i=0; i < 4; i++)
  207.         printf("\nPISTA %d AVIAO ID: =>  [ %d ]\n",i,f->inicio->prateleiras[i]);
  208.  
  209.    
  210. }
  211.  
  212. void mostra_decolar(queue *q)
  213. {
  214.     int y;
  215.    
  216.     printf("\n********************\n");
  217.     printf("|-------DECOLAR-------|");
  218.     printf("***********************");
  219.    
  220.     sleep( 1 );
  221.    
  222.     for(y=0; y <4 ; y++)
  223.         printf("\nPISTA %d AVIAO ID: => [ %d ] \n",y,q->end->prateleiras1[y]);
  224.  
  225.        
  226. }
  227.  
  228.  
  229. int main()
  230. {
  231.     fila *f = fila_cria();
  232.     queue *q = queue_create();
  233.  
  234.     int opc,pis,prat;
  235.  
  236.     for(;;){
  237.        
  238.         int x = random_id();
  239.         printf("\n\t( 0 )-----SAIR \n");
  240.         printf("\n\t( 1 )----- INSERIR AVIAO NA FILA DE POUSO \n");
  241.         printf("\n\t( 2 )----- INSERIR AVIAO NA FILA DE DECOLAGEM \n");
  242.         printf("\n\t( 3 )----- MOSTRAR FILA ( POUSO ) \n");
  243.         printf("\n\t( 4 )----- MOSTRAR FILA ( DECOLAGEM )\n");
  244.  
  245.         printf("> "); scanf("%d",&opc);
  246.  
  247.         switch(opc){
  248.             default:
  249.                 break;
  250.             case 0:
  251.                 printf("Saindo...");
  252.                 sleep( 1 );
  253.                 exit( 0 );
  254.             case 1:
  255.                 printf("\n (Informe o numero da pista): ");
  256.                 scanf("%d",&pis);
  257.                 aterrisar(f,pis,x);
  258.                 break;
  259.             case 2:
  260.                 printf("\n (Informe o numero da prateleira): ");
  261.                 scanf("%d",&prat);
  262.                 decolar_aviao(q,prat,x);
  263.                 break;
  264.             case 3:
  265.                 mostra_pouso(f);
  266.                 break;
  267.             case 4:
  268.                 mostra_decolar(q);
  269.                 break;
  270.         }
  271.     }
  272.     return 0;
  273.  
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement