This commit is contained in:
2024-11-26 20:35:44 +01:00
parent 094bef3d10
commit 6c5fec1823
3 changed files with 401 additions and 0 deletions

123
TP3/3.1.cpp Normal file
View File

@@ -0,0 +1,123 @@
#include <iostream>
#include <string>
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 <typename T>
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;
}

122
TP3/3.2.cpp Normal file
View File

@@ -0,0 +1,122 @@
#include <iostream>
#include <string>
using namespace std;
const int Nmax = 10;
struct PROF
{
int Id;
string nom;
string service;
void saisie();
void afficher();
};
template <typename T>
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;
}

156
TP3/3.3.cpp Normal file
View File

@@ -0,0 +1,156 @@
#include <iostream>
#include <string>
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 <typename T>
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;
}