Files
CoursCPP/TP8/LocalProfEtud/main.cpp
2024-11-29 10:28:40 +01:00

224 lines
3.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <iostream>
#include <vector>
#include <list>
#include <string>
using namespace std;
class Personne {
string nom;
int mat1, mat2;
public:
void saisie();
void affiche();
string getnom() { return nom; }
int getmat1() { return mat1; }
int getmat2() { return mat2; }
};
class Local {
string nom;
list<int> mat;
public:
void saisie();
void affiche();
string getnom() { return nom; }
void personnes(list<Personne> etuds, vector<Personne> profs);
template <class T>
bool affiche_local(T t, int m);
};
template <class C, class T>
void saisie(C& cont, const string name) {
int n;
cout << "Nbr " << name << " : ";
cin >> n; cin.ignore();
while (n-- > 0) {
T obj;
obj.saisie();
cont.insert(cont.end(), obj);
//cont.push_back(saisie);
}
}
template <class T>
void affiche_el(T t) {
t.affiche();
}
void affiche_el(string t) {
cout << t;
}
template <class C>
void affiche(C cont) {
for (auto v : cont) {
affiche_el(v);
cout << endl;
}
cout << endl;
}
template <class C>
void copie_nom(list<string>& cont, C c) {
for (auto a : c) {
cont.push_back(a.getnom());
}
}
void tri_doublon(list<string>& c) {
c.sort();
c.unique();
}
void local_prof_etud(list<Local> locaux, list<Personne> etuds, vector<Personne> profs) {
for (auto local : locaux) {
local.personnes(etuds, profs);
}
}
int menu() {
int c;
cout << "1) Saisie d1 Vector de Prof (insertion à la Fin) + Affichage" << endl
<< "2) Saisie d1 List de Etud(insertion à la Fin) + Affichage" << endl
<< "3) Copie des noms des Prof et Etud dans List2" << endl
<< "4) TRI et Supprime les doublons(noms) de la List2 + Affichage" << endl
<< "5) Saisie des \"Local\" + Affichage" << endl
<< "6) Pour tout mat_i appartenant au Local Afficher Prof + Etud" << endl;
cout << "Choix : ";
cin >> c; cin.ignore();
return c;
}
int main() {
vector<Personne> profs;
list<Personne> etuds;
list<string> list2;
list<Local> locaux;
setlocale(LC_ALL, "");
while (true) {
switch (menu()) {
case 1:
saisie<vector<Personne>, Personne>(profs, "Prof");
affiche(profs);
break;
case 2:
saisie<list<Personne>, Personne>(etuds, "Etud");
affiche(etuds);
break;
case 3:
copie_nom(list2, profs);
copie_nom(list2, etuds);
affiche(list2);
break;
case 4:
tri_doublon(list2);
affiche(list2);
break;
case 5:
saisie<list<Local>, Local>(locaux, "Locaux");
affiche(locaux);
break;
case 6:
local_prof_etud(locaux, etuds, profs);
break;
default:
return 0;
}
}
return 0;
}
void Personne::saisie() {
cout << "Son nom ? : ";
getline(cin, nom);
cout << "Sa mat1 ? : ";
cin >> mat1; cin.ignore();
cout << "Sa mat2 ? : ";
cin >> mat2; cin.ignore();
}
void Personne::affiche() {
cout << "Nom : " << nom << endl;
cout << "Mat1 : " << mat1 << endl;
cout << "Mat2 : " << mat2 << endl;
}
void Local::saisie() {
cout << "Son nom ? : ";
getline(cin, nom);
cout << "nbr mat ? :";
int m;
cin >> m;
for(int i = 0; i< m;i++) {
int a;
cout << "Mat"<<i<<" ? : ";
cin >> a; cin.ignore();
mat.insert(mat.begin(), a);
}
}
void Local::affiche() {
cout << "Nom : " << nom << endl;
int i = 0;
for (auto m : mat) {
cout << "Mat"<<i<<" : " << m << endl;
i++;
}
}
void Local::personnes(list<Personne> etuds, vector<Personne> profs) {
cout << nom << " " << endl;
for (auto m : mat) {
cout << " " << "Mat" << m << " : ";
bool f;
f = affiche_local(etuds, m);
f |= affiche_local(profs, m);
if (!f) {
cout << "NULL";
}
cout << endl;
}
}
template <class T>
bool Local::affiche_local(T t, int m) {
bool f = false;
for (auto e : t) {
if (e.getmat1() == m || e.getmat2() == m) {
cout << e.getnom() << ", ";
f = true;
}
}
return f;
}