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("\nAVIAO: %d ATERRISADO COM SUCESSO!!! \n",idAviao);
- if (isEmpty(f))
- f->inicio = no;
- else
- f->fim->prox = no;
- }else {
- sleep( 3 );
- printf("\n\tAVIAO %d CAIU!!\n",idAviao);
- no->fall++;
- }
- }
- }
- 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() % 4;
- if (r == 1 || r==2 || r== 3){
- 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\nAviao sem combustivel \n\n");
- }
- }
- }
- q->end = node;
- _size++;
- return true;
- }
- void imprime(fila *f,queue *q)
- {
- int i,y;
- for(i=0; i < size; i++){
- printf("Elemento da Prateleiras => ID Aviao: 1B: [%d] 2B: [%d] \n",f->inicio->prateleiras[ i ],f->fim->prateleiras[i]);
- }
- }
- int main()
- {
- fila *f = fila_cria();
- queue *q = queue_create();
- int x = random_id();
- //srand(time(NULL));
- //aterrisar(f,0,x);
- //imprime(f);
- int opc;
- for(;;){
- int x = random_id();
- printf("\n0-----Sair \n");
- printf("\n1----- inserir aviao para pouso \n");
- printf("\n2----- Inserir aviao para decolagem \n");
- printf("\n3----- imprime \n");
- printf("> "); scanf("%d",&opc);
- switch(opc){
- case 1:
- aterrisar(f,0,x);
- break;
- case 2:
- decolar_aviao(q,3,x);
- break;
- case 3:
- imprime(f,q);
- default:
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement