Student Queue. Help in structuring code

Asked

Viewed 2,918 times

2

My doubt is in the case 1, how to include items in the queue, in case, mat and media. Follows code below:

#include <iostream>
#include <cstdlib>
#define tamanho 20
using namespace std;
struct Aluno{
    int mat;
    float media;
};
struct FILA{
      int inicio;
      int fim;
      int item [tamanho];
}; 
int main(){
    Aluno a;
    FILA f;
    int op;
    cout << "-------------------------------------" << endl;
    cout << "- 1 - Inserir Aluno              ----" << endl;
    cout << "- 2 - Exibir dados do Aluno      ----" << endl;
    cout << "- 3 - Aluno aprovados            ----" << endl;
    cout << "- 4 - Remover Aluno              ----" << endl;
    cout << "- 9 - Sair                       ----" << endl;
    cout << "-------------------------------------" << endl;

    cout << "Selecione a opção desejada: ";
    cin >> op;
    system("cls");
    switch(op){
        case 1:
            system("cls");
            cout<<"\nMatricula: "<<endl;
            cin>>a.mat;
            cout<<"\nMedia: "<<endl;
            a.media;
            enfileira(f, a.mat, a.media);

                break;
        case 9: 
            system("cls");
            cout<<"\nPROGRAMA FINALIZADO !!!"<<endl;
            return 0;


    }
}
void iniciaFila(FILA &f) {
     f.inicio = 0;
     f.fim=f.inicio;
}
bool filaVazia(FILA f){
     return f.fim==0;
}
bool filaCheia(FILA f){
     return f.fim==tamanho;
}
void enfileira(FILA &f, int x){
    if(!filaCheia(f))
       f.item[f.fim++]=x;
    else
       cout<<"fila cheia\n";   
}
void desenfileiraf(FILA &f){
     if(!filaVazia(f)){
        cout<<"desenfileirou "<<f.item[f.inicio]<<endl;
        for(int i=0;i<f.fim-1;i++)
            f.item[i]=f.item[i+1];               
        f.fim--;
     }
     else
        cout<<"fila vazia\n";
} 

int desenfileira(FILA &f){
     if(!filaVazia(f)){
        int n=f.item[f.inicio];
        for(int i=0;i<f.fim-1;i++)
            f.item[i]=f.item[i+1];               
        f.fim--;
        return n;
     }
     cout<<"fila vazia\n";
     return -1; 
} 
void mostra(FILA f){
     if(!filaVazia(f)){
        for(int i=f.inicio;i<f.fim;i++)
              cout<<f.item[i]<<' ';

        cout<<endl;        
     } 
     else
     cout<<"fila vazia\n";
} 

The exercise consists mainly of what the menu asks for.

  • 1

    I edited your question. When you are going to post some code, select it in your question and click on the icon {} in the menu. So you have a differentiation of what is text and what is code.

  • I clicked but did not select, thank you.

  • 1

    I think Voce could put more writing so that someone can help you

1 answer

1


In your case you would need to modify the row structure to the following.

struct FILA{
  int inicio;
  int fim;
  Aluno aluno[tamanho];
}; 

Thus, you will have an array of students where you can add the data. In addition, the queue function must receive a data of the Student type to be inserted in the queue. If you modify the struct and functions correctly I think your code will work.

Another suggestion would be to use the data structure that is already present in the C++ language by default. Take a look at reference and if possible try to change the code it will get much shorter and clear.

Browser other questions tagged

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