C language problem with queues and write to text file

Asked

Viewed 53 times

0

I have a college job to do, but I have a lot of difficulty in language C the job is to create the following functions

  • int InserirAviaoFila(char *voo);
  • int MostrarFila();
  • int AutorizarDecolagem();

The flight is a string, as an example "TP6689".

The function MostrarFila() you should check if there are any planes in the queue. If yes, you should show the contents of the queue and return zero to the main, if not, you must inform that the queue is empty and return -1. The function AutorizarDecolagem() You should check if there’s a plane in line. If yes, you must inform (show on screen) which flight you are allowed to take off and print this information in a text file, along with the date and time of the system. At this point the plane leaves the queue. The file must be opened in the attachment mode, that is, previously existing information must be preserved. In this case it is returned zero to the main. If the queue is empty, the function should show this on the screen and return -1 to the main.

I have no problem setting up the functions, but when I have to call them and mount them on main, I always have problems. In this case I had already made a program that read strings along with the time and passed them to a text file that is the following:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
int main(void)
{
    time_t rawtime;
    time (&rawtime);
    FILE*pont_arq;

    char palavra[20];

    pont_arq = fopen("testeum.txt", "a");

    if(pont_arq == NULL)
    {
        printf("Erro na abertura do arquivo");
        return 1;
    }
    else{
        printf("Escreva uma palavra: ");
        scanf("%s", palavra);
        //armazena a string no arquivo
        fprintf(pont_arq, "%s : %s\n", palavra, ctime (&rawtime));
        //para fechar o arquivo
        fclose(pont_arq);

        printf("Dados gravados");
    }
    getch();




    }

And the following functions:

    #define MAX 10
    
    // Variaveis declaradas aqui são visiveis por todas as funções
    char pilha[MAX][6];
    char topo = 0;
    
    //Insere um item na pilha
    char empilha (char novo[6])
    {
        if (topo >= MAX)
        {
            printf("empilha: Pilha cheia.\n");
            return -1;
        }
        
        int i;
        for (i = 0; i < 6; i++) {
            pilha[topo][i] = novo[i];
        }
        
        topo++;
    
        return 0;
    }
    
    //Remove um item da pilha (o ultimo que entrou)
    int desempilha (char *dado)
    {
        if (topo <= 0)
        {
            printf("desempilha:Pilha vazia.\n");
            return -1;
        }
        topo --;
        int i;
        for (i = 0; i < 6; i++) 
            dado[i] = pilha[topo][i];
    
        return 0;
    }
    
    
    //Mostra o conteúdo da pilha
    char mostraPilha (void) {
        int i;
        int j;
        
        if (topo <= 0){
            printf("\nmostraPilha: Pilha vazia.\n");
            return 0;
        }
        
        printf("\nmostraPilha: Dados da pilha:\n\n");
        for (i=0; i < topo; i++) {
            for (j=0; j < 6; j++) {
                printf("%c", pilha[i][j]);
            }
            printf("%s", "\n");
        }
        printf("\n\n");
    }

I believe that they are enough for the work, but I’m not getting the two functions together so that they see the function int AutorizarDecolagem() and so remove the string from the queue and add it to the text file along with the time. If anyone can help me would be very grateful.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.