/* Fichier List_Pers.cpp Revision sur list ou T est la classe Personne. A adapter+ameliorer selon vos besoins. Une partie est utile pour le TP3 Sujet de questions du final */ #include // entrée-sortie standard #include // formatage (présentation) #include // chaîne de caractères #include #include #include using namespace std; // librairie standard const int LONG_NOMPRE = 30; // 30 caractères pour le nomPre class Personne { private : char sexe; int numero; double taille, poids; string nomPre; public : Personne(char sexe, int numero, double taille, double poids, string nomPre); // pour la recherche à partir d'un nomPre donné Personne(string); Personne(); string getNomPre() ; // pour min, max et sort bool operator < (const Personne&); // pour la recherche avec find bool operator == (const Personne & autre); // surcharge de l'opérateur d'affichage friend ostream& operator << (ostream&, const Personne &); void afficher(); }; // Partie implémentation Personne::Personne(char sexe, int numero, double taille, double poids, string nomPre) { this->sexe = sexe; this->numero = numero; this->taille = taille; this->poids = poids; this->nomPre = nomPre; } bool Personne::operator < (const Personne& autre) { return this->nomPre < autre.nomPre; // l'operateur < de string } Personne::Personne() { } string Personne::getNomPre() { return nomPre; } // ce constructeur est utile pour la recherche Personne::Personne(string aChercher) { int nbCar = aChercher.length(); // convertir en lettres majuscules for (int i = 0; i < nbCar; i++) if (aChercher[i] >= 'a' && aChercher[i] <= 'z') aChercher[i] += 'A' - 'a'; //ajouter des blancs à la fin for (int i = nbCar ; i < LONG_NOMPRE; i++) aChercher += " "; nomPre = aChercher; sexe = ' '; taille = poids = 0.0; numero = 0; } void Personne::afficher() { /* afficher le point de décimal */ cout.setf(ios::showpoint); /* afficher la partie décimale en mode point flottant fixe */ cout.setf(ios :: fixed); cout << setw(30) << nomPre << setw(6) << numero << setw(8) << setprecision(2) << taille << setw(6) << setprecision(1) << poids << setw(10) << ( sexe == 'F' ? "feminin":"masculin") << endl; } bool Personne::operator == (const Personne & autre) { return nomPre == autre.nomPre; } // ----- Fin de partie d'implémentation ----- ostream& operator << (ostream& sortie, const Personne & unePers) { sortie.setf(ios::fixed); // format fixe sortie.setf(ios::showpoint); // montrer le . de décimal sortie << setw(30) << unePers.nomPre << setw(6) << unePers.numero << setw(8) << setprecision(2) << unePers.taille << setw(8) << setprecision(1) << unePers.poids << setw(10) << ( unePers.sexe == 'F' ? "feminin":"masculin") << endl; return sortie; } void continuer() { cout << endl << endl << "Appuyez sur une lettre suivie de Entree " ; char lettre; cin >> lettre ; cout << endl << endl; } /* ------------------------------------------------------ - Auteur : Tremblay Lise, section A Groupe 1 - ------------------------------------------------------ - But : Cette méthode permet de lire un fichier - - remplir la liste des personnes. - ------------------------------------------------------ - Dernière mise à jour : ... - ------------------------------------------------------ */ void lireRemplir(const char nomFichier[], list & liste) { ifstream aLire (nomFichier, ios::in); // localiser et ouvrir pour la lecture string ligneLue; while ( getline(aLire, ligneLue, '\n')) { char sexe = ligneLue[30]; float taille = atof(ligneLue.substr(37,4).c_str()); float poids = atof(ligneLue.substr(51,5).c_str()); int numero = atoi(ligneLue.substr(64,4).c_str()); string nomPre = ligneLue.substr(0, 30); liste.push_back(Personne(sexe, numero, taille, poids, nomPre)); } aLire.close(); // fermer le fichier à lire } void afficher(list liste, string message) { int rang = 0; cout << "Liste de " << liste.size() << " personnes " << message << " :\n"; for (list::iterator il = liste.begin(); il != liste.end() ; il++) { cout << setw(3) << ++rang << ") " << *il ; } cout << endl; } int main() { list liste ; lireRemplir("met_e15.txt", liste); afficher(liste, "apres la creation"); // une petite demo de la recherche : Personne aRecherche = Personne("Dube Francoise"); list::iterator cestLui = find(liste.begin(), liste.end(), aRecherche); if (cestLui != liste.end()) cout << "On trouve la personne :\n" << *cestLui; else cout << "La personne est introuvable " << endl << endl; list::iterator il = min_element(liste.begin(), liste.end()); cout << "La personne ayant le plus petit nomPre de la liste :\n" << *il << endl ; il = max_element(liste.begin(), liste.end()); cout << "La personne ayant le plus grand nomPre de la liste :\n" << *il << endl ; // combien de Robitaille Suzanne dans la liste ? aRecherche = Personne("Robitaille suzanne"); int nbFois = count(liste.begin(), liste.end(), aRecherche); cout << "On rencontre " << aRecherche.getNomPre() << nbFois << " fois\n"; liste.sort(); afficher(liste, "apres le tri"); continuer(); return 0; } /* Exécution : Liste de 24 personnes apres la creation : 1) ROY CHANTAL 2754 1.63 54.9 feminin 2) MOLAISON CLAUDE 1848 1.57 62.2 masculin 3) ROBITAILLE SUZANNE 2007 1.79 72.3 feminin 4) BEDARD MARC-ANDRE 2636 1.43 80.5 masculin 5) MONAST STEPHANE 1750 1.65 61.7 masculin 6) JALBERT LYNE 2168 1.63 52.6 feminin 7) DUBE FRANCOISE 4612 1.68 67.5 feminin 8) ROBITAILLE SUZANNE 2325 1.72 65.4 feminin 9) LEMELIN SOPHIE 7777 1.68 57.8 feminin 10) LABELLE LISE 1512 1.79 68.0 feminin 11) RIVERIN HELENE 2340 1.71 60.8 feminin 12) MICHAUD NORMAND 3428 1.73 103.7 masculin 13) RICHER AGATHE 3563 1.65 53.1 feminin 14) BEGIN MARIE-LUCE 4101 1.62 49.0 feminin 15) COUTU PIERRE 4008 1.72 62.1 masculin 16) ROBITAILLE SUZANNE 4371 1.48 61.5 feminin 17) BERGEVIN GUILLAUME 2277 1.84 86.4 masculin 18) DUMITRU PIERRE 3629 1.92 99.4 masculin 19) ROBITAILLE MICHEL 6002 1.78 85.1 masculin 20) FILLION ERIC 2630 1.78 75.7 masculin 21) DESMARAIS DENISE 3215 1.75 58.7 feminin 22) TREMBLAY MARC 3529 1.79 64.9 masculin 23) TREMBLAY SYLVAIN 1538 1.83 86.2 masculin 24) ROBITAILLE SUZANNE 4119 1.58 60.2 feminin On trouve la personne : DUBE FRANCOISE 4612 1.68 67.5 feminin La personne ayant le plus petit nomPre de la liste : BEDARD MARC-ANDRE 2636 1.43 80.5 masculin La personne ayant le plus grand nomPre de la liste : TREMBLAY SYLVAIN 1538 1.83 86.2 masculin On rencontre ROBITAILLE SUZANNE 4 fois Liste de 24 personnes apres le tri : 1) BEDARD MARC-ANDRE 2636 1.43 80.5 masculin 2) BEGIN MARIE-LUCE 4101 1.62 49.0 feminin 3) BERGEVIN GUILLAUME 2277 1.84 86.4 masculin 4) COUTU PIERRE 4008 1.72 62.1 masculin 5) DESMARAIS DENISE 3215 1.75 58.7 feminin 6) DUBE FRANCOISE 4612 1.68 67.5 feminin 7) DUMITRU PIERRE 3629 1.92 99.4 masculin 8) FILLION ERIC 2630 1.78 75.7 masculin 9) JALBERT LYNE 2168 1.63 52.6 feminin 10) LABELLE LISE 1512 1.79 68.0 feminin 11) LEMELIN SOPHIE 7777 1.68 57.8 feminin 12) MICHAUD NORMAND 3428 1.73 103.7 masculin 13) MOLAISON CLAUDE 1848 1.57 62.2 masculin 14) MONAST STEPHANE 1750 1.65 61.7 masculin 15) RICHER AGATHE 3563 1.65 53.1 feminin 16) RIVERIN HELENE 2340 1.71 60.8 feminin 17) ROBITAILLE MICHEL 6002 1.78 85.1 masculin 18) ROBITAILLE SUZANNE 2007 1.79 72.3 feminin 19) ROBITAILLE SUZANNE 2325 1.72 65.4 feminin 20) ROBITAILLE SUZANNE 4371 1.48 61.5 feminin 21) ROBITAILLE SUZANNE 4119 1.58 60.2 feminin 22) ROY CHANTAL 2754 1.63 54.9 feminin 23) TREMBLAY MARC 3529 1.79 64.9 masculin 24) TREMBLAY SYLVAIN 1538 1.83 86.2 masculin Appuyez sur une lettre suivie de Entree */