/* Fichier revise.cpp Revision sur list ou T est la classe Personne. A adapter+ameliorer selon vos besoins. Une partie est utile pour le TP3 */ #include // entrée-sortie standard #include // formatage (présentation) #include // chaîne de caractères (détails : plus tard) #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() ; 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(); 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 B - ------------------------------------------------------ - But : Cette méthode permet de lire un fichier - - remplir la liste des personnes. - ------------------------------------------------------ - Dernière mise à jour : ... - ------------------------------------------------------ */ void lireRemplir(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[0]; double taille = atof(ligneLue.substr(7,4).c_str()); double poids = atof(ligneLue.substr(21,5).c_str()); int numero = atoi(ligneLue.substr(34,4).c_str()); string nomPre = ligneLue.substr(38); 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_e12.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 19 personnes apres la creation : 1) ROY CHANTAL 2754 1.63 54.9 feminin 2) MOLAISON CLAUDE 1848 1.57 62.2 masculin 3) BEDARD MARC-ANDRE 2636 1.43 80.5 masculin 4) MONAST STEPHANE 1750 1.65 61.7 masculin 5) JALBERT LYNE 2168 1.63 52.6 feminin 6) DUBE FRANCOISE 4612 1.68 67.5 feminin 7) ROBITAILLE SUZANNE 2325 1.72 65.4 feminin 8) LABELLE LISE 1512 1.79 68.0 feminin 9) RIVERIN HELENE 2340 1.71 60.8 feminin 10) MICHAUD NORMAND 3428 1.73 103.7 masculin 11) RICHER AGATHE 3563 1.65 53.1 feminin 12) BEGIN MARIE-LUCE 4101 1.62 49.0 feminin 13) ROBITAILLE SUZANNE 4371 1.48 61.5 feminin 14) DUMITRU PIERRE 3629 1.92 99.4 masculin 15) FILLION ERIC 2630 1.78 75.7 masculin 16) DESMARAIS DENISE 3215 1.75 58.7 feminin 17) TREMBLAY MARC 3529 1.79 64.9 masculin 18) TREMBLAY SYLVAIN 1538 1.83 86.2 masculin 19) ROBITAILLE SUZANNE 4119 1.58 60.2 feminin 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 3 fois Liste de 19 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) DESMARAIS DENISE 3215 1.75 58.7 feminin 4) DUBE FRANCOISE 4612 1.68 67.5 feminin 5) DUMITRU PIERRE 3629 1.92 99.4 masculin 6) FILLION ERIC 2630 1.78 75.7 masculin 7) JALBERT LYNE 2168 1.63 52.6 feminin 8) LABELLE LISE 1512 1.79 68.0 feminin 9) MICHAUD NORMAND 3428 1.73 103.7 masculin 10) MOLAISON CLAUDE 1848 1.57 62.2 masculin 11) MONAST STEPHANE 1750 1.65 61.7 masculin 12) RICHER AGATHE 3563 1.65 53.1 feminin 13) RIVERIN HELENE 2340 1.71 60.8 feminin 14) ROBITAILLE SUZANNE 2325 1.72 65.4 feminin 15) ROBITAILLE SUZANNE 4371 1.48 61.5 feminin 16) ROBITAILLE SUZANNE 4119 1.58 60.2 feminin 17) ROY CHANTAL 2754 1.63 54.9 feminin 18) TREMBLAY MARC 3529 1.79 64.9 masculin 19) TREMBLAY SYLVAIN 1538 1.83 86.2 masculin Appuyez sur une lettre suivie de Entree */