All pastes #348410 Raw Edit

Something

public text v1 · immutable
·published 2007-02-10 03:55 UTC
rendered paste body
//#ifdef WIN32
//#include "stdafx.h" // environnement VC++ version 2005
//#endif
#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
const double PIED_EN_METRE = 0.3048;
const double LBS_EN_KG = 0.424;


void lireRemplir(char nomALire[], int chandail[], double taille[], double poids[],
		 int naissance[], int position[], string nomPre[], int & n)
{ 
  ifstream  aLire (nomALire, ios::in); // localiser et ouvrir pour la lecture
  n = 0; // initialiser le compteur
  string ligne;
  while(  getline(aLire, ligne, '\n'))
    {				
      chandail[n] = atoi((ligne.substr(0,3)).c_str()); 
      int pieds = atoi((ligne.substr(4,1)).c_str());
      int pouces = atoi((ligne.substr(6,2)).c_str());
      taille[n] = (pieds + pouces / 12.0) * PIED_EN_METRE; 
      // On demande la taille en système métrique
      int lbs = atoi((ligne.substr(13,3)).c_str());
      poids[n] = lbs * LBS_EN_KG;
      // On demande le poids en système métrique 
      naissance[n]  = atoi((ligne.substr(19,4)).c_str());
      position[n]  = atoi((ligne.substr(26,1)).c_str());
      nomPre[n] = ligne.substr(30); // de l'indice 30 jusqu'à la fin
      n++;
    }
  aLire.close();   
}


void afficher(int chandail[], double taille[], double poids[], int naissance[],
	      int position[], string nomPre[], int & n, int & prem, int & dern,
	      string mess = "Informations des 8 premiers et des 5 derniers joueurs lus")
{
  /* 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 << mess << " :\n\n";
  cout << "Rang Chandail Taille Poids Naissance Position Nom et Prenom"
       << endl;
  
  for (int i = 0; i < prem; i++)
    cout << setw(3) << i << ") " << setw(5) << chandail[i]
	 << setw(9) << setprecision(3) << taille[i] << setw(7)
	 << setprecision(2) << poids[i] << setw(7) << naissance[i]
	 << setw(8) << position[i] << setw(28) << nomPre[i] << endl;
  cout << endl; 
  for (int i = n-dern; i < n; i++)
    cout << setw(3) << i << ") " << setw(5) << chandail[i]
	 << setw(9) << setprecision(3) << taille[i] << setw(7)
	 << setprecision(2) << poids[i] << setw(7) << naissance[i]
	 << setw(8) << position[i] << setw(28) << nomPre[i] << endl;
  cout << endl;
}


int indiceMaxi(double aChercher, double numero[], int nbJou)
{
  for (int i = 0; i < nbJou; i++)
    if (numero[i] == aChercher)
      return i;
}


void determinerMax(int chandail[], double taille[], double poids[], int naissance[],
		   int position[], string nomPre[], int & n, char code)		
{
  double  maxi;
  int indice;
  if (code == 'p')
    {
      maxi = poids[0];
      for (int i = 1; i < n; i++)
	{
	  if (poids[i] > maxi) maxi = poids[i];
	}
      indice = indiceMaxi(maxi, poids, n);
      cout << "Joueur le plus lourd de l'équipe : \n";
    }
  else
    {
      maxi = taille[0];
      for (int i = 1; i < n; i++)
	{
	  if (taille[i] > maxi) maxi = taille[i];
	}
      indice = indiceMaxi(maxi, taille, n);
      cout << "Joueur le plus grand de l'équipe : \n";
    }
     
  /* 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 << "Rang Chandail Taille Poids Naissance Position Nom et Prenom"
       << endl;
  cout << setw(3) << indice << ") " << setw(5) << chandail[indice]
       << setw(9) << setprecision(3) << taille[indice] << setw(7)
       << setprecision(2) << poids[indice] << setw(7) << naissance[indice]
       << setw(8) << position[indice] << setw(28) << nomPre[indice] << endl;
  cout << endl;
}

void moyenne(int chandail[], double taille[], double poids[], int naissance[],
	     int position[], string nomPre[], int & n, char code)
{
  double moy;
  int k;
  if (code == '1')
    {
      moy = 0;
      k = 0;
      for (int i = 0; i < n; i++)
	if (position[i] == 1)
	  {
	    moy += taille[i];
	    k++;
	  }
      cout << "La taille moyenne des gardiens de buts est : " << (moy/k) << endl;
    }
  else if (code == '2')
    {
      moy = 0; 
      k = 0;     
      for (int i = 0; i < n; i++)
	if (position[i] == 2)
	  {
	    moy += taille[i];
	    k++;
	  }
      cout << "La taille moyenne des défenseurs est : " << (moy/k) << endl;
    }
 else if (code == 't')
    {
      moy = 0;   
      for (int i = 0; i < n; i++)
	moy += taille[i];
      cout << "La taille moyenne de tous les joueurs est : " << (moy/n) << endl;
    }
 else if (code == '3')
    {
      moy = 0; 
      k = 0;     
      for (int i = 0; i < n; i++)
	if (position[i] == 3)
	  {
	    moy += poids[i];
	    k++;
	  }
      cout << "Le poids moyen des joueurs de centre est : " << (moy/k) << endl;
    }
 else if (code == '4')
    {
      moy = 0; 
      k = 0;     
      for (int i = 0; i < n; i++)
	if (position[i] == 4)
	  {
	    moy += poids[i];
	    k++;
	  }
      cout << "Le poids moyen des ailiers gauches est : " << (moy/k) << endl;
    }
else
    {
      moy = 0;         
      for (int i = 0; i < n; i++)
	moy += poids[i];
      cout <<  "Le poids moyen de tous les joueurs est : " << (moy/n) << endl;
    }
}


int indiceMaxi(double aChercher, int numero[], int nbJou)
{
  for (int i = 0; i < nbJou; i++)
    if (numero[i] == aChercher)
      return i;
}

void jeune(int naissance[], string nomPre[], int & n)
{
  int maxi = naissance[0];
  for (int i = 1; i < n; i++)
    {
      if (naissance[i] > maxi) maxi = naissance[i];
    }
  int indice = indiceMaxi(maxi, naissance, n);
  cout << "Le joueur le plus jeune de l'équipe est "
       << nomPre[indice] << endl << endl;
}

template <class T>
void permuter(T & a, T &b)
{
  T tempo = a;
  a = b;
  b = tempo;
}


void trier(int chandail[], double taille[], double poids[], int naissance[],
	   int position[], string nomPre[], int & n)
{
  for (int i = 0 ; i < n-1; i++)
    {
      int indMin = i;
      for (int j = i+1; j < n; j++)
	if (taille[j] < taille[indMin])
	  indMin = j;
      if ( indMin != i)
	{
	  permuter(chandail[i], chandail[indMin]);
	  permuter(taille[i], taille[indMin]);
	  permuter(poids[i], poids[indMin]);
	  permuter(naissance[i], naissance[indMin]);
	  permuter(position[i], position[indMin]);
	  permuter(nomPre[i], nomPre[indMin]);
	}
    }
}

void rechercher(int chandail[], double taille[], double poids[], int naissance[],
		int position[], string nomPre[], int & n, string aChercher)
{
  int k;
  string nom;
  for(int i = 0; i < n; i++)
   {
     k = nomPre[i].length()-1;
     nom = nomPre[i];
     while (k > 0 && nom[k] == ' ')
       k--;
     nomPre[i] = nom.substr(0, k-2);
     if (nomPre[i] == aChercher)
       {
	 cout << "Rang Chandail Taille Poids Naissance Position Nom et Prenom"
	      << endl;
	 cout << setw(3) << i << ") " << setw(5) << chandail[i]
	      << setw(9) << setprecision(3) << taille[i] << setw(7)
	      << setprecision(2) << poids[i] << setw(7) << naissance[i]
	      << setw(8) << position[i] << setw(28) << nomPre[i] << endl;
	 cout << endl;
       }
   }
  cout << "Le nom demandé n'est pas dans la liste" << endl;
}

int main()
{
  const  int MAX_PERS = 25;
  int    chandail[MAX_PERS], position[MAX_PERS], naissance[MAX_PERS];
  double taille[MAX_PERS], poids[MAX_PERS];
  string nomPre[MAX_PERS];
  int nbPers;
  lireRemplir("Canadiens.Mtl", chandail, taille, poids, naissance, position, nomPre, nbPers);
  
  int premier = 8, dernier = 5;
  afficher(chandail, taille, poids, naissance, position, nomPre, nbPers, premier, dernier);
  
  determinerMax(chandail, taille, poids, naissance, position, nomPre, nbPers, 'p');
  determinerMax(chandail, taille, poids, naissance, position, nomPre, nbPers, 't');
  
  moyenne(chandail, taille, poids, naissance, position, nomPre, nbPers, '1');
  moyenne(chandail, taille, poids, naissance, position, nomPre, nbPers, '2');
  moyenne(chandail, taille, poids, naissance, position, nomPre, nbPers, 't');
  cout << endl;

  moyenne(chandail, taille, poids, naissance, position, nomPre, nbPers, '3');
  moyenne(chandail, taille, poids, naissance, position, nomPre, nbPers, '4');
  moyenne(chandail, taille, poids, naissance, position, nomPre, nbPers, 'p');
  cout << endl;

  jeune(naissance, nomPre, nbPers);
  
  premier = 4, dernier = 6;
  trier(chandail, taille, poids, naissance, position, nomPre, nbPers);
  afficher(chandail, taille, poids, naissance, position, nomPre, nbPers, premier, dernier,
	   "Informations des 4 premiers et des 6 derniers joueurs lus triés");

  rechercher(chandail, taille, poids, naissance, position, nomPre, nbPers, "Maxime Lapierre");
  rechercher(chandail, taille, poids, naissance, position, nomPre, nbPers, "Alexei Kovalev");
  rechercher(chandail, taille, poids, naissance, position, nomPre, nbPers, "Guy Lafleur");

  return 0;
}