Use hpp c++ files

Asked

Viewed 331 times

0

Boas, I’m doing a project for the school, and at some point I realized that I had to divide the work into files (because I was getting confused to find the procedures), but I’m not being able to divide it, I believe I’m not using the hpp files as well, because I never gave them. Thanks help fix the code.

main.cpp

#include "introduzir.hpp"
#include "mostrar.hpp"
#include "eliminar.hpp"
#include "ordenar.hpp"
#include "classe.hpp"
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <unistd.h>

using namespace std;
unsigned int microseconds;
int op;

//segunda lista
copiar_lista(){
    struct info *p=NULL;
    struct info *q=NULL;
    p=lista;
    while(p!=NULL){
        q=(struct info *)malloc(sizeof(struct info));
        for(int i=0;i<50;i++){
            q->tarefa[i]=' ';
            q->tarefa[i]=p->tarefa[i];
        }
        for(int i=0;i<50;i++){
        q->id[i]=' ';
            q->id[i]=p->id[i];
        }
        q->nivel=p->nivel;
        q->next=lista2;
        lista2=q;
        p=p->next;
    }
}

//menu e switch para ordenar
escolha_ordenar(){
    copiar_lista();
    switch(op){
        case 1:{
            recentes();
            eliminar();
            break;
        }
        case 2:{
            listar();
            system("pause");
            eliminar();
            break;
        }
        case 3:{
            break;
        }
    }
}
menu_ordenar(){
    cout<<"1 - Mais recentes\n";
    cout<<"2 - Mais antigos\n";
    cout<<"3 - Prioridade Maior\n";
    cout<<"4 - Prioridade Menor\n";
    cin>>op;
    escolha_ordenar();
}

//menu e switch das escolhas
escolha(){
    switch(op){
        case 1:{
            introduzir();
            break;
        }
        case 2:{
            int esc;
            listar();
            cout<<"0 - Sair";
            cin>>esc;
            if(esc==0){
            system("cls");
            return 0;}
            break;
        }
        case 3:{
            eliminar();
            break;
        }
        case 4:{
            menu_ordenar();
            break;
        }
        case 0:{
            cout<<"Obrigado por utilizar este programa!\n";
            break;
        }
    }
}
menu(){
    cout<<"1 - Introduzir\n";
    cout<<"2 - Listar\n";
    cout<<"3 - Eliminar\n";
    cout<<"4 - Ordenar\n";
    cout<<"0 - Sair\n";
    cin>>op;
    escolha();
}

//main
int main(int argc, char** argv) {
    setlocale(LC_ALL,"Portuguese");
    system("color 0e");
    do{
        menu();
        system("cls");
    }while(op!=0);
    system("pause");
}

introduce.hpp

#ifndef INTRODUZIR_HPP
#define INTRODUZIR_HPP
#include "classe.hpp"
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <unistd.h>
using namespace std;

introduzir(){
    struct info *p;
    p=(struct info *)malloc(sizeof(struct info));
    cout<<"Introduza o ID: ";
    _flushall();
    cin.getline(p->id,50);
    cout<<"Introduza a tarefa: ";
    _flushall();
    cin.getline(p->tarefa,50);
    do{
    cout<<"Introduza o nível de prioridade(1-5): ";
    _flushall();
    cin>>p->nivel;
}while(p->nivel<1||p->nivel>5);
p->next=lista;
lista=p;
}
#endif

mostar.hpp

#ifndef MOSTRAR_HPP
#define MOSTRAR_HPP
#include "classe.hpp"
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <unistd.h>
using namespace std;
listar(){
    struct info *p;
    p=lista;
    while(p!=NULL){
        cout<<"ID: "<<p->id<<endl;
        cout<<"Tarefa: "<<p->tarefa<<endl;
        cout<<"Nível: "<<p->nivel<<endl;
        p=p->next;
    }
}
mostrar(){
    struct info *p;
    p=lista2;
    while(p!=NULL){
    cout<<"ID: "<<p->id<<endl;
        cout<<"Tarefa: "<<p->tarefa<<endl;
        cout<<"Nível: "<<p->nivel<<endl;
        p=p->next;
    }

}
#endif

eliminate.hpp

#idndef ELIMINAR_HPP
#define ELIMINAR_HPP
#include "classe.hpp"
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <unistd.h>

using namespace std;

eliminar(){
    struct info *p;
    p=lista;

    while(p!=NULL){
        lista=p->next;
        free(p);
        p=lista;
    }
}
#endif

sort.hpp

#ifndef ORDENAR_HPP
#define ORDENAR_HPP
#include "classe.hpp"
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <unistd.h>
using namespace std;
recentes(){
    copiar_lista();
    struct info *p;
    p=lista2;
    struct info *prev=NULL;
    struct info *next=NULL;
    while(p!=NULL){
        next=p->next;
        p->next=prev;
        prev=p;
        p=next;
    }
    lista2=prev;
    mostrar();
}
#endif

class.hpp

#ifndef CLASSE_HPP
#define CLASSE_HPP
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <unistd.h>


struct info{
    char tarefa[50],id[50];
    int nivel;  
    struct info *next;
};
struct info *lista=NULL;
struct info *head_ref=NULL;
struct info *lista2=NULL;
#endif
  • You understand that the lifecycle of a C++ code is: pre-compile, transform into object/file .o/filing cabinet .obj, and only then link all the object files to an executable in the linkediting phase?

  • 1

    @Jeffersonquesado can come in to do inline :) But the code there does not need, the use is even wrong. In fact the code has numerous problems, including it is essentially C.

  • @Maniero, super simplified. And I did it wrong. Even to use classes/functions with template it is necessary to put in the header the code. Try to put in correct terms.

  • 1

    @Jeffersonquesado Especially because with template is a inlining. I said more not to get hung for laymen, I understood the simplification, I do it too.

  • This reading will give you a good idea of what you should enter in the/header file. hpp: https://answall.com/a/76787/64969; a hint: usually you put only declarations, avoid putting implementation of functions and methods. There are cases where it is required by implementations, but you do not make use of these cases.

  • Thanks in advance for the explanations, I decided to go back and leave everything together (despite getting lost in so many xD procedures), someone has tips to better organize the code?

  • @Jeffersonquesado you don’t happen to have a link that helps me sort a Linked list; because I can’t sort it, I have this: prio_maior(){ struct info *p; p=Lista2; struct info *temp=NULL; struct info *j=NULL; while(p!=NULL){ j=Lista2; j=j->next; while(j!=NULL){ if(p->nivel<=j->nivel){ Cout<<p->nivel; temp=p; p=j; j=temp; Cout<<p->nivel; j=j->next; }Else { j=j->next; } }p=p->next; }}

Show 2 more comments
No answers

Browser other questions tagged

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