Я пытаюсь использовать быструю сортировку, чтобы отсортировать массив людей по количеству людей. У меня есть результат, но массив отсортирован неправильно. Любая помощь будет оценена. Заранее спасибо.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
class Personne
{
private :
char sexe ;
float taille, poids ;
int numero;
public :
Personne(char sexe, int numero, float taille, float poids);
Personne()
{
}
int getNumero();
void afficher()
{
cout << setw(6) << sexe
<< setw(10) << numero
<< setw(10) << setprecision(2) << taille
<< setw(12) << setprecision(1) << poids << endl;
}
};
int Personne::getNumero()
{
return numero;
}
Personne::Personne(char sexe, int numero, float taille, float poids)
{
this->sexe = sexe;
this->numero = numero;
this->taille = taille;
this->poids = poids;
}
void lireRemplir(const char nomALire[], Personne pers[], int &n)
{
const float PIED_EN_METRE = 0.3048, LIVRE_EN_KG = 0.454;
int numero, nbPieds, nbPouces, nbLivres;
float taille, poids;
char sexe;
ifstream aLire(nomALire, ios::in); // localiser et ouvrir pour la lecture
n = 0;
while(aLire >> sexe >> numero >> nbPieds >> nbPouces >> nbLivres)
{
taille = (nbPieds + nbPouces /12.0) * PIED_EN_METRE;
poids = nbLivres * LIVRE_EN_KG ;
pers[n++] = Personne(sexe, numero, taille, poids);
}
aLire.close();
}
void afficher(Personne tab[], int nbElement, string message = "")
{
cout << "TABLEAU DES PERSONNES " << message << " :"<< endl;
cout << "Indice Sexe Numero Taille(m) Poids(kg)" << endl;
for(int i = 0; i < nbElement; i++)
{
cout << setw(3) << i << ") ";
tab[i].afficher();
}
cout << endl;
}
void permuter(Personne &a, Personne &b)
{
Personne temp = a;
a = b;
b = temp;
}
int partitionner(Personne tab[], int debut, int fin)
{
int d = debut, f = fin;
int valPivot = tab[d].getNumero();
while(d <= f)
{
while(d <= f && tab[d].getNumero() <= valPivot)
d++;
while(tab[f].getNumero() > valPivot)
f--;
if(d < f)
permuter(tab[d], tab[f]);
}
permuter(tab[f], tab[d]);
return f;
}
void quickSort(Personne tab[], int debut, int fin)
{
if (fin > debut)
{
int indPivot = partitionner(tab, debut, fin);
quickSort(tab, debut, indPivot-1);
quickSort(tab, indPivot+1, fin);
}
}
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
const int MAX_PERS = 25 ;
Personne pers[MAX_PERS];
int nbPers;
lireRemplir("imp_e20.txt", pers, nbPers);
afficher(pers, nbPers);
quickSort(pers, 0, nbPers-1);
afficher(pers, nbPers, "APRES LE TRI SELON NUMERO");
return 0;
}
Заявление об ограничении ответственности: я канадский французский язык, поэтому некоторые слова в моем коде написаны на французском языке, извините.