Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
LISTE CHAÎNÉE (FIFO) COMPILÉ AVEC C++BUILDER 5
Information sur la source
Description
Programme montrant la manipulation des listes chaînées à l'aide de fichiers de données.
Source
- #include <iostream.h>
- #include <conio.h>
- #include <fstream.h>
-
- struct Personne
- {
- string NomPrenom;
- char Sexe;
- float Taille, Poids;
- };
-
- struct Element
- {
- Personne Pers;
- struct Element *Suivant;
- };
-
- typedef Element *Pointeur;
-
- /*************************** Prototypes de fonctions **************************/
-
- void Lire(fstream &A_Lire, Personne *P);
- void CreerFIFO(Pointeur *P);
- void Afficher(Pointeur Laliste);
- void Creation_Fichier_Bin(Pointeur Liste);
- void Creation_Fichier_Bin_Sexe(Pointeur Liste, char Quel_Sexe, char * Nom_Fichier);
- void Relire_Fichier_Bin(Pointeur Liste, char * Nom_fichier);
- void Afficher_Position(Pointeur Liste, int Position);
- void Afficher_Qqun(Personne Pers);
- void Trouver_Nom(Pointeur Liste, Pointeur *Avant, Pointeur *A_Trouver, string Nom);
- void Lourd(Pointeur Liste, Pointeur * Avant, Pointeur *A_Trouver, char Sexe);
- void Long(Pointeur Liste, Pointeur * Avant, Pointeur *A_Trouver, char Sexe);
- void Affichage_Taille_Sup(Pointeur Liste, char Sexe, float Max);
- int Nb_Pers(Pointeur Liste);
-
- /******************** Fonction qui créé La liste ******************************/
-
- void CreerFIFO(Pointeur *P)
- {
- Pointeur Laliste = *P, Courant, Tempo;
- fstream A_Lire;
-
- Laliste = NULL;
-
- A_Lire.open("j:\\metrique.tp3", ios::in);
- while (!A_Lire.eof())
- {
- Element *Tempo;
- Tempo = new(Element);
-
- Lire(A_Lire, &Tempo->Pers);
-
- if(!Laliste)
- Laliste = Tempo;
- else
- Courant->Suivant = Tempo;
-
- Courant = Tempo;
- }
- A_Lire.close();
-
- if(Laliste)
- Courant->Suivant = NULL;
-
- *P = Laliste;
- }
-
- /************ Fonction qui fait la lecture du fichier de données ***************/
-
- void Lire(fstream &A_Lire, Personne *P)
- {
- Personne Qqun;
-
- getline(A_Lire, Qqun.NomPrenom);
- A_Lire >> Qqun.Sexe >> Qqun.Taille >> Qqun.Poids;
- A_Lire.ignore();
-
- *P = Qqun;
- }
-
- /*************** Fonction qui affiche le contenu de la liste *******************/
-
- void Afficher(Pointeur Laliste)
- {
- cout << "\nVoici le contenu de la liste : \n";
-
- while(Laliste)
- {
- cout << "\n" << Laliste->Pers.NomPrenom
- << "\n" << Laliste->Pers.Sexe << "\t" << Laliste->Pers.Taille
- << "\t" << Laliste->Pers.Poids;
-
- Laliste = Laliste->Suivant;
- }
- }
-
- /********* Fonction qui compte le nombre de personne dans la liste**************/
-
- int Nb_Pers(Pointeur Liste)
- {
- int Nombre_Personne = 0;
-
- while (Liste)
- {
- Nombre_Personne++;
- Liste = Liste->Suivant;
- }
- return Nombre_Personne;
- }
-
- /******************** Fonction qui créé un fichier binaire ********************/
-
- void Creation_Fichier_Bin(Pointeur Liste)
- {
- fstream Creer_Bin;
-
- Creer_Bin.open("Metrique.bin", ios::out|ios::binary);
-
- while(Liste)
- {
- Creer_Bin.write((char *)&Liste->Pers, sizeof(Personne));
- Liste = Liste->Suivant;
- }
- Creer_Bin.close();
- cout << "\n\nCreation du Fichier Binaire\n";
- }
-
- /************ Fonction qui créé un fichier binaire selon le sexe **************/
-
- void Creation_Fichier_Bin_Sexe(Pointeur Liste, char Quel_Sexe, char * Nom_Fichier)
- {
- fstream Creer_Bin_Sexe;
-
- Creer_Bin_Sexe.open(Nom_Fichier, ios::out | ios::binary);
-
- while(Liste)
- {
- if (Liste->Pers.Sexe == Quel_Sexe)
- Creer_Bin_Sexe.write((char *)&Liste->Pers, sizeof(Personne));
-
- Liste = Liste->Suivant;
- }
- Creer_Bin_Sexe.close();
- cout << "\n\nCreation du Fichier Binaire pour les personnes du meme sexe\n";
- }
-
- /***************** Fonction qui lit les fichiers binaires **********************/
-
- void Relire_Fichier_Bin(char * Nom_fichier)
- {
- fstream A_Relire;
- Personne Qqun;
-
- A_Relire.open(Nom_fichier, ios::in | ios::binary);
- cout << "\n----------------------------------------\n";
- A_Relire.read((char *)&Qqun, sizeof(Personne));
- while(!A_Relire.eof())
- {
- cout << "\n" << Qqun.NomPrenom
- << "\n" << Qqun.Sexe << "\t" << Qqun.Taille
- << "\t" << Qqun.Poids;
-
- A_Relire.read((char *)&Qqun, sizeof(Personne));
- }
- A_Relire.close();
- cout << "\n----------------------------------------\n\n";
- }
-
- /***************** Fonction d'affichage d'une personne ************************/
-
- void Afficher_Qqun(Personne Pers)
- {
- cout << "----------------------------------------\n";
- cout << Pers.NomPrenom << "\n" << Pers.Sexe << "\t"
- << Pers.Taille << "\t" << Pers.Poids;
- cout << "\n----------------------------------------\n";
- }
-
- /************* Fonction d'affichage selon la position **************************/
-
- void Afficher_Position(Pointeur Liste, int Position)
- {
- int Rang = 1;
- Pointeur Courant = Liste;
-
-
- if(Position > Nb_Pers(Liste))
- cout << "\nErreur, cette position n'existe pas ";
-
- while(Courant && Rang != -1)
- {
- if(Rang == Position)
- {
- Afficher_Qqun(Courant->Pers);
- Rang = -1;
- }
- else
- {
- Courant = Courant->Suivant;
- Rang++;
- }
- }
- }
-
- /******************** Fonction d'affichage de Nom ***************************/
-
-
- void Trouver_Nom(Pointeur Liste, Pointeur *Avant, Pointeur *A_Trouver, string Nom)
- {
- Pointeur Precedent;
- bool Trouver = FALSE;
-
- Precedent = NULL;
-
- while(Liste && Trouver == FALSE)
- {
- if(Liste->Pers.NomPrenom != Nom)
- {
- Precedent = Liste;
- Liste = Liste->Suivant;
- }
- else
- Trouver = TRUE;
- }
- *Avant = Precedent;
- *A_Trouver = Liste;
- }
-
- /******************** Fonction d'affichage de poids ***************************/
-
-
- void Lourd(Pointeur Liste, Pointeur * Avant, Pointeur *A_Trouver, char Sexe)
- {
- Pointeur Precedent, Maximum;
- float PoidsLourd;
-
- Precedent = NULL;
- PoidsLourd = 0;
-
- while(Liste)
- {
- if(Liste->Pers.Sexe == Sexe && Liste->Pers.Poids > PoidsLourd)
- {
- PoidsLourd = Liste->Pers.Poids;
- Maximum = Liste;
- }
- Precedent = Liste;
- Liste = Liste->Suivant;
- }
-
- *Avant = Precedent;
- *A_Trouver = Maximum;
- }
-
- /******************** Fonction de recherche de taille **************************/
-
- void Long(Pointeur Liste, Pointeur * Avant, Pointeur *A_Trouver, char Sexe)
- {
- Pointeur Courant = Liste, Maximum;
- float LongPoil;
-
- //Precedent = NULL;
- LongPoil = 0;
-
- while(Courant->Suivant)
- {
- if(Courant->Pers.Sexe == Sexe && Courant->Pers.Taille > LongPoil)
- {
- LongPoil = Courant->Pers.Taille;
- Maximum = Courant;
- }
- //Precedent = Liste;
- Courant = Courant->Suivant;
- }
-
- *Avant = Courant;
- *A_Trouver = Maximum;
- }
-
- /******************** Fonction d'affichage de taille ***************************/
-
- void Affichage_Taille_Sup(Pointeur Liste, float Max, char Sexe )
- {
- bool Trouver = FALSE;
-
- while(Liste && Trouver == FALSE)
- {
- if(Liste->Pers.Sexe == Sexe && Liste->Pers.Taille > Max)
- {
- cout << "-----------------------------------\n";
- cout << "\n" << Liste->Pers.NomPrenom
- << "\n" << Liste->Pers.Sexe << "\t" << Liste->Pers.Taille
- << "\t" << Liste->Pers.Poids;
- cout << "\n-----------------------------------\n";
-
- Trouver = TRUE;
- }
- Liste = Liste->Suivant;
- }
- }
-
- void main(void)
- {
- Pointeur Liste, A_Trouver, Avant;
-
- CreerFIFO(&Liste);
-
- cout << "Appuyez sur une touche pour commencer\n\n";
- getch();
-
- cout << "Cette Liste contient : "
- << Nb_Pers(Liste) << " personnes";
-
- Afficher(Liste);
-
- cout << "\n\nAppuyez sur une touche pour continuer";
- getch();
-
- Creation_Fichier_Bin(Liste);
- cout << "\n\nAppuyez sur une touche pour continuer";
- getch();
-
- cout << "\n\nLecture du fichier binaire";
- Relire_Fichier_Bin("Metrique.bin");
- getch();
-
- Creation_Fichier_Bin_Sexe(Liste,'F', "Femmes.bin");
- Creation_Fichier_Bin_Sexe(Liste,'F', "Hommes.bin");
- cout << "\n\nAppuyez sur une touche pour continuer";
- getch();
-
- cout << "\n\nLecture du fichier binaire des femmes \n\n";
- Relire_Fichier_Bin("Femmes.bin");
- cout << "\n\nAppuyez sur une touche pour continuer";
- getch();
-
- cout << "\nLes informations sur la premiere personne de la liste\n";
- Afficher_Position(Liste, 1);
- getch();
-
- cout << "\nLes informations sur la deuxième personne de la liste\n";
- Afficher_Position(Liste, 2);
- getch();
-
- cout << "\nLes informations sur la personne #100 de la liste\n";
- Afficher_Position(Liste, 100);
- getch();
-
- Trouver_Nom(Liste, &Avant, &A_Trouver, "DESMARAIS DENISE");
-
- if (A_Trouver != NULL)
- {
- cout << "\n\nLes informations sur DESMARAIS DENISE :\n";
- Afficher_Qqun(A_Trouver->Pers);
- }
- else
- cout << "\n\nCes informations ne sont pas disponibles\n";
-
- cout << "\n\nAppuyez sur une touche pour continuer";
- getch();
-
- Trouver_Nom(Liste, &Avant, &A_Trouver, "DUMITRU PIERRE");
-
- if (A_Trouver != NULL)
- {
- cout << "\n\nLes informations sur DUMITRU PIERRE :\n";
- Afficher_Qqun(A_Trouver->Pers);
- }
- else
- cout << "\n\nCes informations ne sont pas disponibles\n";
-
- cout << "\n\nAppuyez sur une touche pour continuer";
- getch();
-
- Trouver_Nom(Liste, &Avant, &A_Trouver, "GRETZKY WAYNE");
-
- if (A_Trouver != NULL)
- {
- cout << "\n\nLes informations sur GRETZKY WAYNE :\n";
- Afficher_Qqun(A_Trouver->Pers);
- }
- else
- cout << "\n\nLes informations sur GRETZKY WAYNE ne sont pas disponibles\n";
-
- cout << "\n\nAppuyez sur une touche pour continuer";
- getch();
-
- cout << "\nL'homme le plus lourd est : \n";
- Lourd(Liste, &Avant, &A_Trouver, 'M');
- Afficher_Qqun(A_Trouver->Pers);
-
- cout << "\n\nAppuyez sur une touche pour continuer";
- getch();
-
- cout << "\nLa femme la plus grande est : \n";
- Long(Liste, &Avant, &A_Trouver, 'F');
- Afficher_Qqun(A_Trouver->Pers);
-
- cout << "\n\nAppuyez sur une touche pour continuer";
- getch();
-
- cout << "\nLa premiere femme ayant une taille superieur a 1,65m est : \n";
- Affichage_Taille_Sup(Liste, 1.65, 'F');
-
- cout << "\n\nAppuyez sur une touche pour continuer";
- getch();
-
-
- }
-
#include <iostream.h>
#include <conio.h>
#include <fstream.h>
struct Personne
{
string NomPrenom;
char Sexe;
float Taille, Poids;
};
struct Element
{
Personne Pers;
struct Element *Suivant;
};
typedef Element *Pointeur;
/*************************** Prototypes de fonctions **************************/
void Lire(fstream &A_Lire, Personne *P);
void CreerFIFO(Pointeur *P);
void Afficher(Pointeur Laliste);
void Creation_Fichier_Bin(Pointeur Liste);
void Creation_Fichier_Bin_Sexe(Pointeur Liste, char Quel_Sexe, char * Nom_Fichier);
void Relire_Fichier_Bin(Pointeur Liste, char * Nom_fichier);
void Afficher_Position(Pointeur Liste, int Position);
void Afficher_Qqun(Personne Pers);
void Trouver_Nom(Pointeur Liste, Pointeur *Avant, Pointeur *A_Trouver, string Nom);
void Lourd(Pointeur Liste, Pointeur * Avant, Pointeur *A_Trouver, char Sexe);
void Long(Pointeur Liste, Pointeur * Avant, Pointeur *A_Trouver, char Sexe);
void Affichage_Taille_Sup(Pointeur Liste, char Sexe, float Max);
int Nb_Pers(Pointeur Liste);
/******************** Fonction qui créé La liste ******************************/
void CreerFIFO(Pointeur *P)
{
Pointeur Laliste = *P, Courant, Tempo;
fstream A_Lire;
Laliste = NULL;
A_Lire.open("j:\\metrique.tp3", ios::in);
while (!A_Lire.eof())
{
Element *Tempo;
Tempo = new(Element);
Lire(A_Lire, &Tempo->Pers);
if(!Laliste)
Laliste = Tempo;
else
Courant->Suivant = Tempo;
Courant = Tempo;
}
A_Lire.close();
if(Laliste)
Courant->Suivant = NULL;
*P = Laliste;
}
/************ Fonction qui fait la lecture du fichier de données ***************/
void Lire(fstream &A_Lire, Personne *P)
{
Personne Qqun;
getline(A_Lire, Qqun.NomPrenom);
A_Lire >> Qqun.Sexe >> Qqun.Taille >> Qqun.Poids;
A_Lire.ignore();
*P = Qqun;
}
/*************** Fonction qui affiche le contenu de la liste *******************/
void Afficher(Pointeur Laliste)
{
cout << "\nVoici le contenu de la liste : \n";
while(Laliste)
{
cout << "\n" << Laliste->Pers.NomPrenom
<< "\n" << Laliste->Pers.Sexe << "\t" << Laliste->Pers.Taille
<< "\t" << Laliste->Pers.Poids;
Laliste = Laliste->Suivant;
}
}
/********* Fonction qui compte le nombre de personne dans la liste**************/
int Nb_Pers(Pointeur Liste)
{
int Nombre_Personne = 0;
while (Liste)
{
Nombre_Personne++;
Liste = Liste->Suivant;
}
return Nombre_Personne;
}
/******************** Fonction qui créé un fichier binaire ********************/
void Creation_Fichier_Bin(Pointeur Liste)
{
fstream Creer_Bin;
Creer_Bin.open("Metrique.bin", ios::out|ios::binary);
while(Liste)
{
Creer_Bin.write((char *)&Liste->Pers, sizeof(Personne));
Liste = Liste->Suivant;
}
Creer_Bin.close();
cout << "\n\nCreation du Fichier Binaire\n";
}
/************ Fonction qui créé un fichier binaire selon le sexe **************/
void Creation_Fichier_Bin_Sexe(Pointeur Liste, char Quel_Sexe, char * Nom_Fichier)
{
fstream Creer_Bin_Sexe;
Creer_Bin_Sexe.open(Nom_Fichier, ios::out | ios::binary);
while(Liste)
{
if (Liste->Pers.Sexe == Quel_Sexe)
Creer_Bin_Sexe.write((char *)&Liste->Pers, sizeof(Personne));
Liste = Liste->Suivant;
}
Creer_Bin_Sexe.close();
cout << "\n\nCreation du Fichier Binaire pour les personnes du meme sexe\n";
}
/***************** Fonction qui lit les fichiers binaires **********************/
void Relire_Fichier_Bin(char * Nom_fichier)
{
fstream A_Relire;
Personne Qqun;
A_Relire.open(Nom_fichier, ios::in | ios::binary);
cout << "\n----------------------------------------\n";
A_Relire.read((char *)&Qqun, sizeof(Personne));
while(!A_Relire.eof())
{
cout << "\n" << Qqun.NomPrenom
<< "\n" << Qqun.Sexe << "\t" << Qqun.Taille
<< "\t" << Qqun.Poids;
A_Relire.read((char *)&Qqun, sizeof(Personne));
}
A_Relire.close();
cout << "\n----------------------------------------\n\n";
}
/***************** Fonction d'affichage d'une personne ************************/
void Afficher_Qqun(Personne Pers)
{
cout << "----------------------------------------\n";
cout << Pers.NomPrenom << "\n" << Pers.Sexe << "\t"
<< Pers.Taille << "\t" << Pers.Poids;
cout << "\n----------------------------------------\n";
}
/************* Fonction d'affichage selon la position **************************/
void Afficher_Position(Pointeur Liste, int Position)
{
int Rang = 1;
Pointeur Courant = Liste;
if(Position > Nb_Pers(Liste))
cout << "\nErreur, cette position n'existe pas ";
while(Courant && Rang != -1)
{
if(Rang == Position)
{
Afficher_Qqun(Courant->Pers);
Rang = -1;
}
else
{
Courant = Courant->Suivant;
Rang++;
}
}
}
/******************** Fonction d'affichage de Nom ***************************/
void Trouver_Nom(Pointeur Liste, Pointeur *Avant, Pointeur *A_Trouver, string Nom)
{
Pointeur Precedent;
bool Trouver = FALSE;
Precedent = NULL;
while(Liste && Trouver == FALSE)
{
if(Liste->Pers.NomPrenom != Nom)
{
Precedent = Liste;
Liste = Liste->Suivant;
}
else
Trouver = TRUE;
}
*Avant = Precedent;
*A_Trouver = Liste;
}
/******************** Fonction d'affichage de poids ***************************/
void Lourd(Pointeur Liste, Pointeur * Avant, Pointeur *A_Trouver, char Sexe)
{
Pointeur Precedent, Maximum;
float PoidsLourd;
Precedent = NULL;
PoidsLourd = 0;
while(Liste)
{
if(Liste->Pers.Sexe == Sexe && Liste->Pers.Poids > PoidsLourd)
{
PoidsLourd = Liste->Pers.Poids;
Maximum = Liste;
}
Precedent = Liste;
Liste = Liste->Suivant;
}
*Avant = Precedent;
*A_Trouver = Maximum;
}
/******************** Fonction de recherche de taille **************************/
void Long(Pointeur Liste, Pointeur * Avant, Pointeur *A_Trouver, char Sexe)
{
Pointeur Courant = Liste, Maximum;
float LongPoil;
//Precedent = NULL;
LongPoil = 0;
while(Courant->Suivant)
{
if(Courant->Pers.Sexe == Sexe && Courant->Pers.Taille > LongPoil)
{
LongPoil = Courant->Pers.Taille;
Maximum = Courant;
}
//Precedent = Liste;
Courant = Courant->Suivant;
}
*Avant = Courant;
*A_Trouver = Maximum;
}
/******************** Fonction d'affichage de taille ***************************/
void Affichage_Taille_Sup(Pointeur Liste, float Max, char Sexe )
{
bool Trouver = FALSE;
while(Liste && Trouver == FALSE)
{
if(Liste->Pers.Sexe == Sexe && Liste->Pers.Taille > Max)
{
cout << "-----------------------------------\n";
cout << "\n" << Liste->Pers.NomPrenom
<< "\n" << Liste->Pers.Sexe << "\t" << Liste->Pers.Taille
<< "\t" << Liste->Pers.Poids;
cout << "\n-----------------------------------\n";
Trouver = TRUE;
}
Liste = Liste->Suivant;
}
}
void main(void)
{
Pointeur Liste, A_Trouver, Avant;
CreerFIFO(&Liste);
cout << "Appuyez sur une touche pour commencer\n\n";
getch();
cout << "Cette Liste contient : "
<< Nb_Pers(Liste) << " personnes";
Afficher(Liste);
cout << "\n\nAppuyez sur une touche pour continuer";
getch();
Creation_Fichier_Bin(Liste);
cout << "\n\nAppuyez sur une touche pour continuer";
getch();
cout << "\n\nLecture du fichier binaire";
Relire_Fichier_Bin("Metrique.bin");
getch();
Creation_Fichier_Bin_Sexe(Liste,'F', "Femmes.bin");
Creation_Fichier_Bin_Sexe(Liste,'F', "Hommes.bin");
cout << "\n\nAppuyez sur une touche pour continuer";
getch();
cout << "\n\nLecture du fichier binaire des femmes \n\n";
Relire_Fichier_Bin("Femmes.bin");
cout << "\n\nAppuyez sur une touche pour continuer";
getch();
cout << "\nLes informations sur la premiere personne de la liste\n";
Afficher_Position(Liste, 1);
getch();
cout << "\nLes informations sur la deuxième personne de la liste\n";
Afficher_Position(Liste, 2);
getch();
cout << "\nLes informations sur la personne #100 de la liste\n";
Afficher_Position(Liste, 100);
getch();
Trouver_Nom(Liste, &Avant, &A_Trouver, "DESMARAIS DENISE");
if (A_Trouver != NULL)
{
cout << "\n\nLes informations sur DESMARAIS DENISE :\n";
Afficher_Qqun(A_Trouver->Pers);
}
else
cout << "\n\nCes informations ne sont pas disponibles\n";
cout << "\n\nAppuyez sur une touche pour continuer";
getch();
Trouver_Nom(Liste, &Avant, &A_Trouver, "DUMITRU PIERRE");
if (A_Trouver != NULL)
{
cout << "\n\nLes informations sur DUMITRU PIERRE :\n";
Afficher_Qqun(A_Trouver->Pers);
}
else
cout << "\n\nCes informations ne sont pas disponibles\n";
cout << "\n\nAppuyez sur une touche pour continuer";
getch();
Trouver_Nom(Liste, &Avant, &A_Trouver, "GRETZKY WAYNE");
if (A_Trouver != NULL)
{
cout << "\n\nLes informations sur GRETZKY WAYNE :\n";
Afficher_Qqun(A_Trouver->Pers);
}
else
cout << "\n\nLes informations sur GRETZKY WAYNE ne sont pas disponibles\n";
cout << "\n\nAppuyez sur une touche pour continuer";
getch();
cout << "\nL'homme le plus lourd est : \n";
Lourd(Liste, &Avant, &A_Trouver, 'M');
Afficher_Qqun(A_Trouver->Pers);
cout << "\n\nAppuyez sur une touche pour continuer";
getch();
cout << "\nLa femme la plus grande est : \n";
Long(Liste, &Avant, &A_Trouver, 'F');
Afficher_Qqun(A_Trouver->Pers);
cout << "\n\nAppuyez sur une touche pour continuer";
getch();
cout << "\nLa premiere femme ayant une taille superieur a 1,65m est : \n";
Affichage_Taille_Sup(Liste, 1.65, 'F');
cout << "\n\nAppuyez sur une touche pour continuer";
getch();
}
Conclusion
Pour plus de d'efficacité il est aussi possible de trier la liste. Si ca vous tente essayer de crer cette fonction.... :) Le fichier de donnée est dans le .zip
Sources de la même categorie
Commentaires
|
|