TP8: EtudProf
This commit is contained in:
220
TP8/EtudProf/main.cpp
Normal file
220
TP8/EtudProf/main.cpp
Normal file
@@ -0,0 +1,220 @@
|
||||
#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 d’Etudiants + 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 l’Effacer + 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();
|
||||
}
|
||||
Reference in New Issue
Block a user