Advertisement
luciana1237

AVIAO_HJ 23q07

Jul 23rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.40 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("\nAVIAO: %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\tAVIAO %d CAIU!!\n",idAviao);
  133.                     no->fall++;
  134.                 }
  135.         }
  136.     }
  137.     size++;
  138.     f->fim = no;
  139.     return true;
  140. }
  141.  
  142. bool decolar_aviao(queue *q,int p, int IdAviao)
  143. {
  144.     decolar *node = (decolar *) malloc(sizeof(decolar));
  145.     int y,r,clock,Mm;
  146.     clock = rand() %10;
  147.  
  148.     srand(time(NULL));
  149.  
  150.     if (node == NULL)
  151.     {
  152.         fprintf(stderr, " error "); exit( 0 );
  153.     }
  154.     node->_next = NULL;
  155.  
  156.     for(y=clock; y >=0; y--){
  157.         sleep ( 1 );
  158.  
  159.         printf("\n\t ==========================================\n");
  160.         printf("\n\t| TEMPO PREVISTO PARA PROXIMA DECOLAGEM:   |\n\n");
  161.         printf("\n\t| TEMPO: \t%d                              |\n",y);
  162.         printf("\n\t ========================================== \n");
  163.        
  164.         if (y== 0){
  165.             Mm = Mm+1;
  166.             if (Mm == 1)
  167.                 r = rand() % 4;
  168.  
  169.                 if (r == 1 || r==2 || r== 3){
  170.                     sleep( 2 );
  171.                     printf("\n\t****************************\n");
  172.                     printf("\n\t| AVIAO => %d Decolando...  |\n",IdAviao);
  173.                     printf("\n\t****************************");
  174.  
  175.                     node->prateleiras1[ p ] = IdAviao;
  176.                     printf("\nAVIAO => %d DECOLADO COM SUCESSO \n",IdAviao);
  177.  
  178.                     if (Empty(q))
  179.                         q->begin =node;
  180.                     else
  181.                         q->end->_next = node;
  182.                 }else {
  183.                     sleep( 1 );
  184.                     printf("\n\nAviao sem combustivel \n\n");
  185.                 }
  186.         }
  187.     }
  188.     q->end = node;
  189.     _size++;
  190.     return true;
  191.  
  192. }
  193.  
  194. void imprime(fila *f,queue *q)
  195. {
  196.     int i,y;
  197.     for(i=0; i < size; i++){
  198.         printf("Elemento da Prateleiras => ID Aviao: 1B: [%d] 2B: [%d] \n",f->inicio->prateleiras[ i ],f->fim->prateleiras[i]);
  199.     }
  200.  
  201. }
  202.  
  203.  
  204. int main()
  205. {
  206.     fila *f = fila_cria();
  207.     queue *q = queue_create();
  208.  
  209.     int x = random_id();
  210.     //srand(time(NULL));
  211.     //aterrisar(f,0,x);
  212.     //imprime(f);
  213.     int opc;
  214.  
  215.     for(;;){
  216.         int x = random_id();
  217.         printf("\n0-----Sair \n");
  218.         printf("\n1----- inserir aviao para pouso \n");
  219.         printf("\n2----- Inserir aviao para decolagem \n");
  220.         printf("\n3-----  imprime \n");
  221.  
  222.         printf("> "); scanf("%d",&opc);
  223.  
  224.         switch(opc){
  225.             case 1:
  226.                 aterrisar(f,0,x);
  227.                 break;
  228.             case 2:
  229.                 decolar_aviao(q,3,x);
  230.                 break;
  231.             case 3:
  232.                 imprime(f,q);
  233.             default:
  234.                 break;
  235.         }
  236.     }
  237.     return 0;
  238.  
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement