From 6c5fec1823c8fdeca524dde41ee2d0ecbbe55fff Mon Sep 17 00:00:00 2001 From: theking90000 Date: Tue, 26 Nov 2024 20:35:44 +0100 Subject: [PATCH] TP3 --- TP3/3.1.cpp | 123 +++++++++++++++++++++++++++++++++++++++++ TP3/3.2.cpp | 122 ++++++++++++++++++++++++++++++++++++++++ TP3/3.3.cpp | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 401 insertions(+) create mode 100644 TP3/3.1.cpp create mode 100644 TP3/3.2.cpp create mode 100644 TP3/3.3.cpp diff --git a/TP3/3.1.cpp b/TP3/3.1.cpp new file mode 100644 index 0000000..dcc1bdc --- /dev/null +++ b/TP3/3.1.cpp @@ -0,0 +1,123 @@ +#include +#include + +using namespace std; + +const int Nmax = 10; + +struct PROF +{ + int Id; + string nom; + string service; +}; + +void SaisieProf(PROF &prof); +void AfficherProf(const PROF prof); + +template +void DeplacerDroite(T tab[], int N, int i = 0) +{ + while (N-- > i) + { + tab[N + 1 + i] = tab[N + i]; + } +} + +int ChercherProf(const PROF profs[], const int N, string name, string service); + +int menu() +{ + int choix; + + cout + << " 1. Saisir + Afficher\n" + << " 2. Ajouter un PROF au début + Afficher\n" + << " 3. Chercher si PROF1 existe ou pas dans le tableau\n" + << " 4. Quitter " << "\n" + << " 5. Afficher la liste de profs\n" + << "Entrez votre choix : "; + + cin >> choix; + + return choix; +} + +void main() +{ + PROF profs[Nmax]; + int choix = 0, N = 0; + + setlocale(LC_ALL, ""); + + while (choix != 4) + { + switch (choix = menu()) + { + case 1: + SaisieProf(profs[N]); + AfficherProf(profs[N++]); + break; + case 2: + DeplacerDroite(profs, N); + N++; + SaisieProf(profs[0]); + AfficherProf(profs[0]); + break; + case 3: + if (ChercherProf(profs, N, "PROF1", "SERVICE1") != -1) + { + cout << "Le PROF1 est dans la liste" << "\n"; + } + else + { + cout << "Le PROF1 n'est pas dans la liste" << "\n"; + } + break; + case 4: + return; + case 5: + for (int i = 0; i < N; i++) + { + cout << "-- Prof " << i << " --" << endl; + AfficherProf(profs[i]); + } + } + } +} + +void SaisieProf(PROF &prof) +{ + cout << "Identifiant: "; + cin >> prof.Id; + cin.ignore(); + cout << "Nom : \n"; + // cin.ignore(); + + // cin >> prof.nom; + getline(cin, prof.nom); + cout << "Service : \n"; + // cin.ignore(); + getline(cin, prof.service); + // cin >> prof.service; +} + +void AfficherProf(const PROF prof) +{ + cout << "Prof " << prof.Id << "\n" + << "Nom : " << prof.nom << "\n" + << "Service : " << prof.service << "\n"; +} + +int ChercherProf(const PROF profs[], int N, string name, string service) +{ + for (int i = 0; i < N; i++) + { + if (profs[i].nom == name && profs[i].service == service) + { + return i; + } + } + + return -1; +} \ No newline at end of file diff --git a/TP3/3.2.cpp b/TP3/3.2.cpp new file mode 100644 index 0000000..cb19111 --- /dev/null +++ b/TP3/3.2.cpp @@ -0,0 +1,122 @@ +#include +#include + +using namespace std; + +const int Nmax = 10; + +struct PROF +{ + int Id; + string nom; + string service; + + void saisie(); + void afficher(); +}; + +template +void DeplacerDroite(T tab[], int N, int i = 0) +{ + while (N-- > i) + { + tab[N + 1 + i] = tab[N + i]; + } +} + +int ChercherProf(const PROF profs[], const int N, string name, string service); + +int menu() +{ + int choix; + + cout + << " 1. Saisir + Afficher\n" + << " 2. Ajouter un PROF au d�but + Afficher\n" + << " 3. Chercher si PROF1 existe ou pas dans le tableau\n" + << " 4. Quitter " << "\n" + << " 5. Afficher la liste de profs\n" + << "Entrez votre choix : "; + + cin >> choix; + + return choix; +} + +void main() +{ + PROF profs[Nmax]; + int choix = 0, N = 0; + + setlocale(LC_ALL, ""); + + while (choix != 4) + { + switch (choix = menu()) + { + case 1: + profs[N].saisie(); + profs[N++].afficher(); + break; + case 2: + DeplacerDroite(profs, N); + N++; + profs[0].saisie(); + profs[0].afficher(); + break; + case 3: + if (ChercherProf(profs, N, "PROF1", "SERVICE1") != -1) + { + cout << "Le PROF1 est dans la liste" << "\n"; + } + else + { + cout << "Le PROF1 n'est pas dans la liste" << "\n"; + } + break; + case 4: + return; + case 5: + for (int i = 0; i < N; i++) + { + cout << "-- Prof " << i << " --" << endl; + profs[i].afficher(); + } + } + } +} + +void PROF::saisie() +{ + cout << "Identifiant: "; + cin >> Id; + cin.ignore(); + cout << "Nom : \n"; + + // cin >> prof.nom; + getline(cin, nom); + cout << "Service : \n"; + // cin.ignore(); + getline(cin, service); + // cin >> prof.service; +} + +void PROF::afficher() +{ + cout << "Prof " << Id << "\n" + << "Nom : " << nom << "\n" + << "Service : " << service << "\n"; +} + +int ChercherProf(const PROF profs[], int N, string name, string service) +{ + for (int i = 0; i < N; i++) + { + if (profs[i].nom == name && profs[i].service == service) + { + return i; + } + } + + return -1; +} \ No newline at end of file diff --git a/TP3/3.3.cpp b/TP3/3.3.cpp new file mode 100644 index 0000000..5375007 --- /dev/null +++ b/TP3/3.3.cpp @@ -0,0 +1,156 @@ +#include +#include + +using namespace std; + +const int Nmax = 10; + +struct PROF +{ + int Id; + string nom; + string service; + + PROF(); + + void saisie(); + void afficher(); + + static int counter; + + bool operator==(PROF P) + { + return P.nom == nom && P.service == service; + } +}; + +int PROF::counter = 0; + +template +void DeplacerDroite(T tab[], int N, int i = 0) +{ + PROF tmp = tab[Nmax - 1]; + while (N-- > i) + { + tab[N + 1 + i] = tab[N + i]; + } + tab[0] = tmp; +} + +int ChercherProf(PROF profs[], const int N, PROF p); + +int menu() +{ + int choix; + + cout + << " 1. Saisir + Afficher\n" + << " 2. Ajouter un PROF au début + Afficher\n" + << " 3. Chercher si un prof existe ou pas dans le tableau\n" + << " 4. Quitter " << "\n" + << " 5. Afficher la liste de profs\n" + << "Entrez votre choix : "; + + cin >> choix; + + return choix; +} + +void main() +{ + PROF profs[Nmax]; + PROF p; + int choix = 0, N = 0; + + setlocale(LC_ALL, ""); + + while (choix != 4) + { + switch (choix = menu()) + { + case 1: + if (N >= 10) + { + break; + } + profs[N].saisie(); + profs[N++].afficher(); + break; + case 2: + if (N >= 10) + { + break; + } + + DeplacerDroite(profs, Nmax); + N++; + profs[0].saisie(); + profs[0].afficher(); + break; + case 3: + cout << "Entrez le nom du prof à rechercher : \n"; + cin.ignore(); + getline(cin, p.nom); + cout << "Entrez le nom du service du prof à rechercher : \n"; + // cin.ignore(); + getline(cin, p.service); + // cout << "Service:" << p.service << "\n"; + if (ChercherProf(profs, N, p) != -1) + { + cout << "Le " << p.nom << " est dans la liste" << "\n"; + } + else + { + cout << "Le " << p.nom << " n'est pas dans la liste" << "\n"; + } + break; + case 4: + return; + case 5: + for (int i = 0; i < N; i++) + { + cout << "-- Prof " << i << " --" << endl; + profs[i].afficher(); + } + } + } +} + +PROF::PROF() +{ + Id = ++counter; +} + +void PROF::saisie() +{ + + cout << "Nom : "; + cin.ignore(); + // cin >> prof.nom; + getline(cin, nom); + cout << "Service : "; + // cin.ignore(); + getline(cin, service); + // cin >> prof.service; +} + +void PROF::afficher() +{ + cout << "Prof " << Id << "\n" + << "Nom : " << nom << "\n" + << "Service : " << service << "\n"; +} + +int ChercherProf(PROF profs[], int N, PROF P) +{ + + for (int i = 0; i < N; i++) + { + if (profs[i] == P) + { + return i; + } + } + + return -1; +} \ No newline at end of file