luciana1237

AVIAO 14:36

Jul 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.95 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 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. size_t  size=0,_size=0,fall=0,Combustivel=0;
  39.  
  40. int pista[MAX_PISTAS],aviao[MAX_PISTAS],aviao1[MAX_PISTAS];
  41.  
  42. int random_id()
  43. {
  44.     srand(time(NULL));
  45.  
  46.     return rand() %1000;
  47. }
  48.  
  49.  
  50. fila *fila_cria(void)
  51. {
  52.     fila *f= (fila *) malloc(sizeof(fila));
  53.  
  54.     if (f != NULL)
  55.         f->inicio = NULL;
  56.         f->fim = NULL;
  57.     return f;
  58.  
  59. }
  60. queue *queue_create(void)
  61. {
  62.     queue *q = (queue *) malloc(sizeof(queue));
  63.  
  64.     if (q != NULL)
  65.         q->begin = NULL;
  66.         q->end = NULL;
  67.     return q;
  68. }
  69.  
  70. bool isEmpty(fila *f)
  71. {
  72.     return (f->inicio == NULL);
  73. }
  74. bool Empty(queue *q)
  75. {
  76.     return (q->begin == NULL);
  77. }
  78.  
  79. bool aterrisar(fila *f,int p,int idAviao)
  80. {
  81.     elem *no = (elem *) malloc(sizeof(elem));
  82.     int y,r,Mm=0,clock;
  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.                     aviao1[p] = idAviao;
  134.                     fall++;
  135.                     size--;
  136.                 }
  137.         }
  138.     }
  139.     size++;
  140.     f->fim = no;
  141.     return true;
  142. }
  143.  
  144. bool decolar_aviao(queue *q,int p, int IdAviao)
  145. {
  146.     decolar *node = (decolar *) malloc(sizeof(decolar));
  147.     int y,r,clock,Mm;
  148.     clock = rand() %10;
  149.  
  150.     srand(time(NULL));
  151.  
  152.     if (node == NULL)
  153.     {
  154.         fprintf(stderr, " error "); exit( 0 );
  155.     }
  156.     node->_next = NULL;
  157.  
  158.     for(y=clock; y >=0; y--){
  159.         sleep ( 1 );
  160.  
  161.         printf("\n\t ==========================================\n");
  162.         printf("\n\t| TEMPO PREVISTO PARA PROXIMA DECOLAGEM:   |\n\n");
  163.         printf("\n\t| TEMPO: \t%d                              |\n",y);
  164.         printf("\n\t ========================================== \n");
  165.        
  166.         if (y== 0){
  167.             Mm = Mm+1;
  168.             if (Mm == 1)
  169.                 r = rand() % 6;
  170.  
  171.                 if (r == 1 || r==2 || r== 3 || r==4 || r==5){
  172.                    
  173.                     sleep( 2 );
  174.                    
  175.                     printf("\n\t****************************\n");
  176.                     printf("\n\t| AVIAO => %d Decolando...  |\n",IdAviao);
  177.                     printf("\n\t****************************");
  178.  
  179.                     node->prateleiras1[ p ] = IdAviao;
  180.                    
  181.                     printf("\nAVIAO => %d DECOLADO COM SUCESSO \n",IdAviao);
  182.  
  183.                     if (Empty(q))
  184.                         q->begin =node;
  185.                     else
  186.                         q->end->_next = node;
  187.                 }else {
  188.                     sleep( 1 );
  189.                     printf("\n\n[-] AVIAO SEM COMBUSTIVEL [-]\n\n");
  190.                    
  191.                     aviao[p] = IdAviao;
  192.                     Combustivel++;
  193.                     _size--;
  194.                 }
  195.         }
  196.     }
  197.     q->end = node;
  198.     _size++;
  199.     return true;
  200.  
  201. }
  202.  
  203. void mostra_pouso(fila *f)
  204. {
  205.     int i,y;
  206.    
  207.     printf("\n***********************\n");
  208.     printf("\n|---------POUSO--------|\n");
  209.     printf("\n************************\n");
  210.    
  211.     sleep( 1 );
  212.    
  213.     printf("QUANTIDADE DE AVIAO => %d\n",size);
  214.    
  215.     for(i=0; i < 4; i++)
  216.         printf("\nPISTA %d AVIAO ID: =>  [ %d ]\n",i,f->fim->prateleiras[i]);
  217.    
  218.    
  219.     for(y=0; y < fall; y++)
  220.         printf("\n Aviao Caidos => %d \n",aviao1[y]);
  221.  
  222.    
  223. }
  224.  
  225. void mostra_decolar(queue *q)
  226. {
  227.     int y,j;
  228.    
  229.     printf("\n********************\n");
  230.     printf("|-------DECOLAR-------|");
  231.     printf("***********************");
  232.    
  233.     sleep( 1 );
  234.    
  235.     printf("\nQUANTIDADE DE AVIAOES => %d \n",_size);
  236.    
  237.    
  238.     for(y=0; y <4; y++)
  239.         printf("\nPISTA %d AVIAO ID: => [ %d ] \n",y,q->end->prateleiras1[y]);
  240.    
  241.     for(j=0; j <=Combustivel; j++)
  242.             printf("AVIOES SEM COMBUSTIVEL => %d ",aviao[j]);
  243.        
  244. }
  245.  
  246.  
  247. int main()
  248. {
  249.     fila *f = fila_cria();
  250.     queue *q = queue_create();
  251.  
  252.     int opc,pis,prat,x;
  253.  
  254.     for(;;){
  255.        
  256.         x = random_id();
  257.        
  258.         printf("\n\t( 0 )-----SAIR \n");
  259.         printf("\n\t( 1 )----- INSERIR AVIAO NA FILA DE POUSO \n");
  260.         printf("\n\t( 2 )----- INSERIR AVIAO NA FILA DE DECOLAGEM \n");
  261.         printf("\n\t( 3 )----- MOSTRAR FILA ( POUSO ) \n");
  262.         printf("\n\t( 4 )----- MOSTRAR FILA ( DECOLAGEM )\n");
  263.  
  264.         printf("> "); scanf("%d",&opc);
  265.  
  266.         switch(opc){
  267.             default:
  268.                 break;
  269.             case 0:
  270.                 printf("Saindo...");
  271.                 sleep( 1 );
  272.                 exit( 0 );
  273.             case 1:
  274.                 printf("\n (Informe o numero da pista): ");
  275.                 scanf("%d",&pis);
  276.                 aterrisar(f,pis,x);
  277.                 break;
  278.             case 2:
  279.                 printf("\n (Informe o numero da prateleira): ");
  280.                 scanf("%d",&prat);
  281.                 decolar_aviao(q,prat,x);
  282.                 break;
  283.             case 3:
  284.                 mostra_pouso(f);
  285.                 break;
  286.             case 4:
  287.                 mostra_decolar(q);
  288.                 break;
  289.         }
  290.     }
  291.     return 0;
  292.  
  293. }
Advertisement
Add Comment
Please, Sign In to add comment