/* Fichier Mod_Fin.cpp Revision sur list ou T est la classe Personne. A adapter+ameliorer selon vos besoins. Une partie est utile pour le TP3 et préparer le final */ #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&); bool operator == (const Personne & autre); // surcharge de l'opérateur d'affichage friend ostream& operator << (ostream&, const Personne &); }; // 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 } bool Personne::operator == (const Personne & autre) { return nomPre == autre.nomPre; } 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; } // ----- 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(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[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; } void chercher(list liste, string nomRecherche) { Personne aRecherche = Personne(nomRecherche); list::iterator cestLui = find(liste.begin(), liste.end(), aRecherche); if (cestLui != liste.end()) cout << "On trouve la personne :\n" << *cestLui; else cout << "La personne " << aRecherche.getNomPre() << " est introuvable " << endl << endl; } int main() { list liste ; lireRemplir("metrique.txt", liste); afficher(liste, "apres la creation"); chercher(liste, "Dube Francoise"); chercher(liste, "Tremblay Alain"); 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 ; 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 On trouve la personne : DUBE FRANCOISE 4612 1.68 67.5 feminin La personne TREMBLAY ALAIN est introuvable 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 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 */