Files
CoursCPP/TP8/EtudProf/main.cpp
2024-11-27 18:33:48 +01:00

220 lines
3.8 KiB
C++
Raw Permalink 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 <string>
#include <map>
#include <fstream>
#include <iomanip>
#include <set>
using namespace std;
const int ENTREE_MAX = 10;
template <class T>
class Personne {
string nom;
int nbr_entrees;
T entrees[ENTREE_MAX];
public:
void Lire(ifstream& f);
void afficher();
T lire_entree(ifstream& f);
string get_key() { return nom; }
string get_nom() { return nom; }
};
class Etud : public Personne<float> {
string Id;
public:
void Lire(ifstream& f);
void afficher();
string get_key() { return Id; }
};
class Prof : public Personne<string> {};
template <class M, class T>
void lecture(M& mp, const string fname) {
ifstream f(fname);
if (!f.is_open()) {
cout << "Erreur lors de la lecture du fichier"<<endl;
exit(1);
}
while (!f.eof()) {
T obj;
obj.Lire(f);
f.ignore();
mp[obj.get_key()] = obj;
}
}
template <class C>
void afficher(const C& mp) {
for (auto pair : mp) {
pair.second.afficher();
cout << endl;
}
cout << endl;
}
template <class C>
void afficher(const set<C>& s) {
for (auto e : s) {
cout << e;
cout << endl;
}
cout << endl;
}
int menu() {
int c;
cout << "1 : Lire Etud_M.txt et stocker les données dans une Map1 dEtudiants + affichage" << endl
<< "2 : Lire Prof_M.txt et stocker les données dans une Map2 de PROFs + affichage" << endl
<< "3 : Chercher selon le nom dans Map1 et dans Map2 si élément existe et lEffacer + affichage" << endl
<< "4 : Mettre les noms des étudiants et des PROFs dans un CnTri + affichage" << endl
<< "5 : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
cout << "Choix : ";
cin >> c; cin.ignore();
return c;
}
bool is_equal_ignorecase(string s1, string s2) {
if (s1.size() != s2.size()) return false;
for (int i = 0; i < s1.size(); i++) {
if (tolower(s1[i]) != tolower(s2[i]))
return false;
}
return true;
}
template <class T>
auto find(T& t, const string nom) {
auto it = t.begin();
for (; it != t.end(); it++) {
if (is_equal_ignorecase(it->second.get_nom(), nom))
break;
}
return it;
}
void effacer_nom(map<string, Etud>& etuds, map<string, Prof>& profs) {
string nom;
cout << "Nom : ";
getline(cin, nom);
auto itE = find(etuds, nom);
auto itP = find(profs, nom);
if (itE == etuds.end() && itP == profs.end()) {
cout << "Ce nom n'existe pas" << endl;
return;
}
if (itE != etuds.end()) {
etuds.erase(itE);
}
if (itP != profs.end()) {
profs.erase(itP);
}
afficher(etuds);
afficher(profs);
}
template <class T>
void ajout_nom(set<string>& noms, T& map) {
for (auto& p : map) {
noms.insert(p.second.get_nom());
}
}
int main() {
map<string, Etud> etuds;
map<string, Prof> profs;
set<string> noms;
while (true) {
switch (menu()) {
case 1:
lecture<map<string, Etud>, Etud>(etuds, "Etud_M.txt");
afficher(etuds);
break;
case 2:
lecture<map<string, Prof>, Prof>(profs, "Prof_M.txt");
afficher(profs);
break;
case 3:
effacer_nom(etuds, profs);
break;
case 4:
noms.clear();
ajout_nom(noms, etuds);
ajout_nom(noms, profs);
afficher(noms);
break;
default:
return 0;
}
}
}
template <class T>
void Personne<T>::Lire(ifstream& f) {
getline(f, nom, ';');
f >> nbr_entrees; f.ignore();
for (int i = 0; i < nbr_entrees; i++) {
entrees[i] = lire_entree(f);
}
}
string Personne<string>::lire_entree(ifstream& f) {
string entree;
getline(f, entree, ';');
return entree;
}
float Personne<float>::lire_entree(ifstream& f) {
float entree;
f >> entree; f.ignore();
return entree;
}
void Etud::Lire(ifstream& f) {
getline(f, Id, ';');
Personne::Lire(f);
}
template <class T>
void Personne<T>::afficher() {
cout << left << setw(22) << nom << " " << nbr_entrees << " ";
for (int i = 0; i < nbr_entrees; i++) {
cout << left << setw(8) << entrees[i] << " ";
}
}
void Etud::afficher() {
cout << setw(6) << Id << " ";
Personne<float>::afficher();
}