Files
CoursCPP/TP8/ParcoursVita/main.cpp
2024-12-01 20:38:22 +01:00

197 lines
3.4 KiB
C++

#include <iostream>
#include <list>
#include <vector>
#include <set>
#include <iomanip>
#include <algorithm>
#include <string>
using namespace std;
class Participant {
string Nom;
int age;
public:
void Affichage();
void Saisie();
int getAge() { return age; }
string getNom() { return Nom; }
// Tri par l'age
bool operator<(Participant p) {
return Nom < p.Nom;
}
};
template <class C, class T>
void saisie(C& c) {
int n;
cout << "Nombre de participants à saisir : ";
cin >> n; cin.ignore();
while ((n--) > 0) {
T participant;
participant.Saisie();
c.insert(c.begin(), participant);
}
}
template <class C>
void affichage(C c) {
for (auto p : c) {
p.Affichage(); cout << endl;
}
cout << endl;
}
void affichage(set<string> c) {
for (auto a : c) {
cout << a << endl;
}
cout << endl;
}
template <class C>
void affichage(C c, const string name) {
cout << "Affichage de " << name << endl;
affichage(c);
}
// template <class A, class B>
bool tri_age(Participant a, Participant b) {
return a.getAge() < b.getAge();
}
// template <class A, class B>
bool tri_age_dec(Participant a, Participant b) {
return !tri_age(a, b);
}
template <class A, class B, class F>
void erase_max(A& a, B& b, F cmp, string txt) {
auto ita = max_element(a.begin(), a.end(), cmp);
auto itb = max_element(b.begin(), b.end(), cmp);
cout << txt;
if (ita != a.end()
&& (itb == b.end() || cmp(*itb, *ita))) {
ita->Affichage();
a.erase(ita);
}
else if (itb != b.end()
&& (ita == a.end() || cmp(*ita, *itb))) {
itb->Affichage();
b.erase(itb);
}
else {
cout << "NULL";
}
cout << endl;
}
template <class A, class B>
void erase_jeune_age(A& a, B& b) {
erase_max(a, b, tri_age, "Le plus vieux = ");
erase_max(a, b, tri_age_dec, "Le plus jeune = ");
}
template <class A, class B>
void add_allnom(A source, B& b) {
for(auto a : source) {
b.insert(a.getNom());
}
}
int menu() {
int c;
cout << "1. Saisie liste L1 + affichage" << endl
<< "2. Saisie vector V1 + affichage" << endl
<< "3. Efface le + jeune et le + agé (L1/V1)" << endl
<< "4. Tri croissant selon age (L1/V1) + affichage" << endl
<< "5. Conteneur de noms de L1/V1 trié+sans doublons" << endl
<< "6. Afficher L1 et V1" << endl;
cout << "Choix : " << endl;
cin >> c; cin.ignore();
return c;
}
int main() {
list<Participant> L1;
vector<Participant> V1;
set<string> noms;
setlocale(LC_ALL, "");
while (true) {
switch (menu()) {
case 1:
saisie<list<Participant>, Participant>(L1);
affichage(L1, "L1");
break;
case 2:
saisie<vector<Participant>, Participant>(V1);
affichage(V1, "V1");
break;
case 3:
erase_jeune_age(L1, V1);
affichage(L1, "L1");
affichage(V1, "V1");
break;
case 4:
L1.sort(tri_age);
sort(V1.begin(), V1.end(), tri_age);
affichage(L1, "L1");
affichage(V1, "V1");
break;
case 5:
add_allnom(L1, noms);
add_allnom(V1, noms);
affichage(noms, "Affichage du conteneur :");
break;
case 6:
affichage(L1, "L1");
affichage(V1, "V1");
break;
default:
return 0;
}
}
}
template <class T>
void affiche(T t, int w) {
cout << left << setw(w) << t << " ";
}
void Participant::Affichage() {
affiche(Nom, 10);
affiche(age, 4);
}
void Participant::Saisie() {
// Saisie de du nom : via getline();
cout << "Son nom ? : ";
getline(cin, Nom);
// Saisie de l'age
cout << "Son age ? : ";
cin >> age; cin.ignore();
}