rendered paste body/* IFT 1166, trimestre : H07
* Fichier Tableaux.cpp (plusieurs tableaux, comme numéro 2 du TP1)
*
* Auteurs :
* Labonte Pierre, section A
* Tremblay Lise, section B
*
* Ce programme permet de :
* 1. lire le fichier Met_H07.txt qui contient au maximum 20 personnes
* remplir les tableaux sexe, taille, poids, numero, nomPre
* compter le nombre effectif de personnes lues;
* 2. afficher le contenu de ces tableaux avant le tri
* 3. déterminer + afficher
* - la taille minimale, la taille maximale
* - le poids minimum, maximum
* 4. trier selon les noms et prénoms et ré-afficher ces tableaux
*
* Voir TabPers.cpp pour les améliorations de cette version
*
* Par LVN pour IFT 1166
*/
#include "stdafx.h" // environnement VC++ version 2005
#include <iostream> // entrée-sortie standard
#include <iomanip> // formatage (présentation)
#include <string> // chaîne de caractères (détails : plus tard)
#include <fstream>
using namespace std; // librairie standard
/* ------------------------------------------------------
- Auteur : Tremblay Lise, section B -
------------------------------------------------------
- But : Cette méthode permet de lire un fichier -
- remplir certains tableaux, compter et -
- transmettre par référence le nombre de -
- personnes lues -
------------------------------------------------------
- Dernière mise à jour : mercredi 24 janvier 2007 -
------------------------------------------------------
*/
void lireRemplir(char nomFichier[], char sexe[], int numero[],
double taille[], double poids[], string nomPre[], int & n)
{
ifstream aLire (nomFichier, ios::in); // localiser et ouvrir pour la lecture
n = 0; // initialiser le compteur
while (aLire >> sexe[n] >> taille[n] >> poids[n] >> numero[n] ,
getline(aLire,nomPre[n], '\n') )
{
n++;
}
aLire.close(); // fermer le fichier à lire
}
// notez le paramètre par défaut (ici, le dernier paramètre)
void afficher(char sexe[], int numero[],double taille[], double poids[],
string nomPre[], int nbPers, string mess = "avant le tri")
{
/* 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 << "Informations des personnes " << mess << " :\n";
cout << " Rang Nom et prenom Numero Taille Poids Sexe"
<< endl;
for (int i = 0 ; i < nbPers; i++)
cout << setw(5) << i << ") "
<< setw(30) << nomPre[i]
<< setw(6) << numero[i]
<< setw(8) << setprecision(2) << taille[i]
<< setw(6) << setprecision(1) << poids[i]
<< setw(10) << ( sexe[i] == 'F' ? "feminin":"masculin")
<< endl;
cout << endl;
}
/* ------------------------------------------------------
- Auteur : Labonte Pierre, section A -
------------------------------------------------------
- But : Cette méthode permet de déterminer et -
- d'afficher la valeur minimale, maximale -
- d'un tableau des réels (taille si le code -
- vaut 't', poids si le code vaut 'p' -
------------------------------------------------------
- Dernière mise à jour : samedi 27 janvier 2007 -
------------------------------------------------------
*/
void determinerMinMax(double tab[], int nbElem, char code)
{
double mini, maxi;
mini = maxi = tab[0];
for (int i = 1; i < nbElem; i++)
{
if (tab[i] < mini) mini = tab[i];
if (tab[i] > maxi) maxi = tab[i];
}
/* afficher le point de décimal */
cout.setf(ios::showpoint);
/* afficher la partie décimale en mode point flottant fixe */
cout.setf(ios :: fixed);
if (code == 't')
cout << "La taille (en metre) :\n";
else
cout << "Le poids (en kg) :\n";
cout << " minimum : " << setw(8) << setprecision(2) << mini << endl;
cout << " maximum : " << setw(8) << setprecision(2) << maxi << endl;
cout << endl ;
}
/* Ici, vous trouvez la surdéfinition (surcharge) des fonctions
On améliorera ces écritures dans TabPers.cpp
*/
void permuter(int & a, int &b)
{
int tempo = a;
a = b;
b = tempo;
}
void permuter(double & a, double &b)
{
double tempo = a;
a = b;
b = tempo;
}
void permuter(char & a, char &b)
{
char tempo = a;
a = b;
b = tempo;
}
void permuter(string & a, string &b)
{
string tempo = a;
a = b;
b = tempo;
}
void trier(char sexe[], int numero[],double taille[], double poids[],
string nomPre[], int nbPers)
{
for (int i = 0 ; i < nbPers-1; i++)
{ int indMin = i;
for (int j = i+1; j < nbPers; j++)
if (nomPre[j] < nomPre[indMin])
indMin = j;
if ( indMin != i)
{
permuter(sexe[i], sexe[indMin]);
permuter(numero[i], numero[indMin]);
permuter(taille[i], taille[indMin]);
permuter(poids[i], poids[indMin]);
permuter(nomPre[i], nomPre[indMin]);
}
}
}
int main()
{
const int MAX_PERS = 20;
char sexe[MAX_PERS];
int numero[MAX_PERS];
double taille[MAX_PERS], poids[MAX_PERS];
string nomPre[MAX_PERS];
int nbPers;
lireRemplir("R:\\Met_H07.txt", sexe, numero, taille, poids, nomPre, nbPers);
afficher(sexe, numero, taille, poids, nomPre, nbPers); // avant le tri
determinerMinMax(taille, nbPers, 't');
determinerMinMax(poids, nbPers, 'p');
trier(sexe, numero, taille, poids, nomPre, nbPers);
afficher(sexe, numero, taille, poids, nomPre, nbPers, "apres le tri");
return 0;
}
/* Exécution :
Informations des personnes avant le tri :
Rang Nom et prenom Numero Taille Poids Sexe
0) ROY CHANTAL 2754 1.63 54.9 feminin
1) MOLAISON CLAUDE 1848 1.57 62.2 masculin
2) BEDARD MARC-ANDRE 2636 1.43 80.5 masculin
3) MONAST STEPHANE 1750 1.65 61.7 masculin
4) JALBERT LYNE 2168 1.63 52.6 feminin
5) DUBE FRANCOISE 4612 1.68 67.5 feminin
6) ROBITAILLE SUZANNE 2325 1.72 65.4 feminin
7) LABELLE LISE 1512 1.79 68.0 feminin
8) RIVERIN HELENE 2340 1.71 60.8 feminin
9) MICHAUD NORMAND 3428 1.73 103.7 masculin
10) RICHER AGATHE 3563 1.65 53.1 feminin
11) BEGIN MARIE-LUCE 4101 1.62 49.0 feminin
12) ROBITAILLE SUZANNE 4371 1.48 61.5 feminin
13) DUMITRU PIERRE 3629 1.92 99.4 masculin
14) FILLION ERIC 2630 1.78 75.7 masculin
15) DESMARAIS DENISE 3215 1.75 58.7 feminin
16) TREMBLAY MARC 3529 1.79 64.9 masculin
17) TREMBLAY SYLVAIN 1538 1.83 86.2 masculin
18) ROBITAILLE SUZANNE 4119 1.58 60.2 feminin
La taille (en metre) :
minimum : 1.43
maximum : 1.92
Le poids (en kg) :
minimum : 49.00
maximum : 103.70
Informations des personnes apres le tri :
Rang Nom et prenom Numero Taille Poids Sexe
0) BEDARD MARC-ANDRE 2636 1.43 80.5 masculin
1) BEGIN MARIE-LUCE 4101 1.62 49.0 feminin
2) DESMARAIS DENISE 3215 1.75 58.7 feminin
3) DUBE FRANCOISE 4612 1.68 67.5 feminin
4) DUMITRU PIERRE 3629 1.92 99.4 masculin
5) FILLION ERIC 2630 1.78 75.7 masculin
6) JALBERT LYNE 2168 1.63 52.6 feminin
7) LABELLE LISE 1512 1.79 68.0 feminin
8) MICHAUD NORMAND 3428 1.73 103.7 masculin
9) MOLAISON CLAUDE 1848 1.57 62.2 masculin
10) MONAST STEPHANE 1750 1.65 61.7 masculin
11) RICHER AGATHE 3563 1.65 53.1 feminin
12) RIVERIN HELENE 2340 1.71 60.8 feminin
13) ROBITAILLE SUZANNE 2325 1.72 65.4 feminin
14) ROBITAILLE SUZANNE 4371 1.48 61.5 feminin
15) ROBITAILLE SUZANNE 4119 1.58 60.2 feminin
16) ROY CHANTAL 2754 1.63 54.9 feminin
17) TREMBLAY MARC 3529 1.79 64.9 masculin
18) TREMBLAY SYLVAIN 1538 1.83 86.2 masculin
Appuyez sur une touche pour continuer...
*/