Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <time.h>
- #include <unistd.h>
- #define MAX_PISTAS 3
- #define MAX_PRATELEIRAS 4
- typedef struct No {
- unsigned int fall,prateleiras[MAX_PRATELEIRAS];
- struct No *prox;
- }elem;
- struct Fila{
- elem *inicio;
- elem *fim;
- };
- typedef struct Node {
- unsigned int prateleiras1[MAX_PRATELEIRAS];
- struct Node *_next;
- }decolar;
- struct Queue {
- decolar *begin;
- decolar *end;
- };
- typedef struct Fila fila;
- typedef struct Queue queue;
- int size=0,_size=0;
- int pista[MAX_PISTAS];
- int random_id()
- {
- srand(time(NULL));
- return rand() %1000;
- }
- fila *fila_cria(void)
- {
- fila *f= (fila *) malloc(sizeof(fila));
- if (f != NULL)
- f->inicio = NULL;
- f->fim = NULL;
- return f;
- }
- queue *queue_create(void)
- {
- queue *q = (queue *) malloc(sizeof(queue));
- if (q != NULL)
- q->begin = NULL;
- q->end = NULL;
- return q;
- }
- bool isEmpty(fila *f)
- {
- return (f->inicio == NULL);
- }
- bool Empty(queue *q)
- {
- return (q->begin == NULL);
- }
- bool aterrisar(fila *f,int p,int idAviao)
- {
- elem *no = (elem *) malloc(sizeof(elem));
- int y,r,Mm=0,clock;
- no->fall =0;
- no->prox = NULL;
- if (no == NULL)
- {
- fprintf(stderr," erro \n"); exit( 0 );
- }
- srand(time(NULL));
- clock = rand() % 10;
- for(y=clock; y >=0; y--){
- sleep( 1 );
- #ifdef linux
- system("clear");
- #else
- system("cls");
- #endif
- printf("\n\t-=========================================-\n");
- printf("\n\t|TEMPO PREVISTO PARA PROXIMA ATERRISAGEM |\n\n");
- printf("\n\t| TEMPO: \t%d |\n",y);
- printf("\n\t-=========================================- \n");
- if (y == 0){
- Mm =Mm+1;
- if (Mm == 1)
- r = rand() % 4;
- if (r == 2 || r== 3 || r==4){
- printf("\n\t********************************\n");
- printf("\n\t|[+] AVIAO => %d Aterrisando [+]|\n",idAviao);
- printf("\n\t********************************");
- sleep( 2 );
- //no->prox = NULL;
- no->prateleiras[ p ] = idAviao;
- printf("\n[+] AVIAO: %d ATERRISADO COM SUCESSO!!! [+]\n",idAviao);
- if (isEmpty(f))
- f->inicio = no;
- else
- f->fim->prox = no;
- }else {
- sleep( 3 );
- printf("\n\t[-] AVIAO %d CAIU!! [-]\n",idAviao);
- no->fall++;
- size--;
- }
- }
- }
- size++;
- f->fim = no;
- return true;
- }
- bool decolar_aviao(queue *q,int p, int IdAviao)
- {
- decolar *node = (decolar *) malloc(sizeof(decolar));
- int y,r,clock,Mm;
- clock = rand() %10;
- srand(time(NULL));
- if (node == NULL)
- {
- fprintf(stderr, " error "); exit( 0 );
- }
- node->_next = NULL;
- for(y=clock; y >=0; y--){
- sleep ( 1 );
- printf("\n\t ==========================================\n");
- printf("\n\t| TEMPO PREVISTO PARA PROXIMA DECOLAGEM: |\n\n");
- printf("\n\t| TEMPO: \t%d |\n",y);
- printf("\n\t ========================================== \n");
- if (y== 0){
- Mm = Mm+1;
- if (Mm == 1)
- r = rand() % 6;
- if (r == 1 || r==2 || r== 3 || r==4 || r==5){
- sleep( 2 );
- printf("\n\t****************************\n");
- printf("\n\t| AVIAO => %d Decolando... |\n",IdAviao);
- printf("\n\t****************************");
- node->prateleiras1[ p ] = IdAviao;
- printf("\nAVIAO => %d DECOLADO COM SUCESSO \n",IdAviao);
- if (Empty(q))
- q->begin =node;
- else
- q->end->_next = node;
- }else {
- sleep( 1 );
- printf("\n\n[-] AVIAO SEM COMBUSTIVEL [-]\n\n");
- _size--;
- }
- }
- }
- q->end = node;
- _size++;
- return true;
- }
- void mostra_pouso(fila *f)
- {
- int i;
- printf("\n***********************\n");
- printf("\n|---------POUSO--------|\n");
- printf("\n************************\n");
- sleep( 1 );
- for(i=0; i < 4; i++)
- printf("\nPISTA %d AVIAO ID: => [ %d ]\n",i,f->inicio->prateleiras[i]);
- }
- void mostra_decolar(queue *q)
- {
- int y;
- printf("\n********************\n");
- printf("|-------DECOLAR-------|");
- printf("***********************");
- sleep( 1 );
- for(y=0; y <4 ; y++)
- printf("\nPISTA %d AVIAO ID: => [ %d ] \n",y,q->end->prateleiras1[y]);
- }
- int main()
- {
- fila *f = fila_cria();
- queue *q = queue_create();
- int opc,pis,prat;
- for(;;){
- int x = random_id();
- printf("\n\t( 0 )-----SAIR \n");
- printf("\n\t( 1 )----- INSERIR AVIAO NA FILA DE POUSO \n");
- printf("\n\t( 2 )----- INSERIR AVIAO NA FILA DE DECOLAGEM \n");
- printf("\n\t( 3 )----- MOSTRAR FILA ( POUSO ) \n");
- printf("\n\t( 4 )----- MOSTRAR FILA ( DECOLAGEM )\n");
- printf("> "); scanf("%d",&opc);
- switch(opc){
- default:
- break;
- case 0:
- printf("Saindo...");
- sleep( 1 );
- exit( 0 );
- case 1:
- printf("\n (Informe o numero da pista): ");
- scanf("%d",&pis);
- aterrisar(f,pis,x);
- break;
- case 2:
- printf("\n (Informe o numero da prateleira): ");
- scanf("%d",&prat);
- decolar_aviao(q,prat,x);
- break;
- case 3:
- mostra_pouso(f);
- break;
- case 4:
- mostra_decolar(q);
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement