1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
| //TP5 - le 3 décembre 2017
#include<iostream>
#include<vector>
#include<list>
#include<algorithm>
#include<iomanip>
#include<string>
#include<locale>
using namespace std;
const int Nmax = 10;
const int espace = 15;
class Etudiant {
int Id;
string nom;
public:
//Etudiant(int, string);
void saisie();
void affichage();
int operator==(Etudiant);
int getId();
string getnom();
};
/*Etudiant::Etudiant(int Id1=0, string nom1=0) {
Id = Id1;
nom = nom1;
}*/
void Etudiant::saisie() {
bool test;
do {
test = 0;
cout << "Id:";
cin >> Id;
if (cin.fail()) {
test = 1;
cin.clear();
cin.ignore();
}
} while (test==1);
cout << "nom:";
cin.ignore();
getline(cin, nom);
}
void Etudiant::affichage() {
cout << endl << setw(espace) << "Id:" << Id<<setw(espace)<<"nom:"<<nom;
}
int Etudiant::operator==(Etudiant E1) {
if ((Id == E1.Id) && (nom == E1.nom)) return 1;
else return 0;
}
int Etudiant::getId(){
return Id;
}
string Etudiant::getnom() {
return nom;
}
class Prof {
int Id;
string nom, service;
public:
Prof();
~Prof();
void saisie();
void affichage();
int operator==(Prof);
int getId();
string getnom();
string getservice();
};
Prof::Prof(){
//cout << "\n+++Prof+++";
cout << "Dans constructeur par defaut" << endl;
}
Prof::~Prof(){
//cout << "\n---Prof---";
cout << "Affichage dans le destructeur" << endl;
cout << "Dans destructeur" << endl;
cout << "Destruction du Prof" << endl;
}
void Prof::saisie() {
bool test;
do {
test = 0;
cout << "Id:";
cin >> Id;
if (cin.fail()) {
test = 1;
cin.clear();
cin.ignore();
}
} while (test == 1);
cout << "nom:";
cin.ignore();
getline(cin, nom);
cout << "service:";
//cin.ignore(); //quand 2 cin.ignore() n'affiche pas le service
getline(cin, service);
}
void Prof::affichage() {
cout << endl << setw(espace) << "Id:" << Id << setw(espace) << "nom:" << nom << setw(espace) << "service:" << service;
}
int Prof::operator==(Prof P1) {
if ((Id == P1.Id) && (nom == P1.nom) && (service == P1.service)) return 1;
else return 0;
}
int Prof::getId() {
return Id;
}
string Prof::getnom() {
return nom;
}
string Prof::getservice() {
return service;
}
void menu(int &); //menu
void choix_E_P(int &); //case 0
void saisie(Etudiant [], int&); //quand tableau
void affichage(Etudiant [], int);//quand tableau
template <class T, class U> void saisieLV(T &); //case 1
template <class T, class U> void print(U, T, string); // case 1
template <class T, class U, class V> void ajouter(T &); //case 2
template <class T, class U, class V> void chercherstat(T); //case 3 //pose problème
template <class T, class U, class V> void chercherdyn(T); //case 3 //pose problème
void rechercheE(list<Etudiant>); //case 3
void rechercheP1(vector<Prof>); //case 3 //pose problème
template <class T, class U, class V> void rechercheP3(U, T); //case 3 //pose problème
void rechercheP2(vector<Prof>); //case 3
void trinoml(list<Etudiant> &); //case 4 //pose problème
void triIdl(list<Etudiant> &); //case 5 //pose problème
template <class T> bool trinom(T, T); //case 4
template <class T> bool trinom2(); //case 4 //pose problème
template <class T> bool triId(T, T); //case 5
void find2(list<Etudiant>);
template <class T, class U, class V> void RechercherTdyn(T tab, U tempo, V &itlv);
int main(){
setlocale(LC_ALL,"");//à quoi cela sert ?
int choix, N, choixEP;
list<Etudiant> EE;
list<Etudiant>::iterator itl;// A quoi cela sert-il de le déclarer dans le Main ?
vector<Prof> PP;
vector<Prof>::iterator itv;// A quoi cela sert-il de le déclarer dans le Main ?
//Etudiant E[Nmax]; //quand tableau
//Prof P[Nmax]; //quand tableau
Etudiant Student; //pas obligé car on utilise la méthode de libeller le template lors de l'appel
Prof Professor; //pas obligé car on utilise la méthode de libeller le template lors de l'appel
bool flag = 0;
do {
menu(choix);
switch (choix) {
case 0:
cout << endl;
choix_E_P(choixEP);
flag = 1;
break;
case 1:
if (flag == 0) {
cout << endl;
choix_E_P(choixEP);
flag = 1;
}
cout << endl;
if (choixEP == 1) {
cout << "!Etudiants!";
saisieLV<list<Etudiant>, Etudiant>(EE);
cout << endl;
print(itl, EE, "Etudiant:");
}
else {
cout << "!Profs!";
saisieLV<vector<Prof>, Prof>(PP);
cout << endl;
print(itv, PP, "Prof:");
}
//saisie(E, N); //quand tableau
//affichage(E, N);
//cout << endl;
break;
case 2:
if (flag == 0) {
cout << endl;
choix_E_P(choixEP);
flag = 1;
}
cout << endl;
if (choixEP == 1) {
ajouter<list<Etudiant>, Etudiant, list<Etudiant>::iterator>(EE);//1°) Pourquoi intégrer l'itérateur ? 2°) Pourquoi ne pas passer un objet par paramètres pour l'élément individuel ?
cout << endl;
print(itl, EE, "Etudiant:");
}
else {
ajouter<vector<Prof>, Prof, vector<Prof>::iterator>(PP);//1°) Pourquoi intégrer l'itérateur ? 2°) Pourquoi ne pas passer un objet par paramètres pour l'élément individuel ?
cout << endl;
print(itv, PP, "Prof:");
}
break;
case 3:
if (flag == 0) {
cout << endl;
choix_E_P(choixEP);
flag = 1;
}
cout << endl;
if (choixEP == 1) {
//rechercheE(EE);
// find2(EE);
RechercherTdyn <list<Etudiant>, Etudiant> (EE, Student,itl/*"l'Etudiant :"*/);//Cette syntaxe car Etudiant pas instancié dans le main
cout << endl;
}
else {
//rechercheP1(PP); //pose problème
//rechercheP3<vector<Prof>, vector<Prof>::iterator, Prof>(itv, PP); //pose problème
// rechercheP2(PP);
RechercherTdyn <vector<Prof>, Prof> (PP, Professor, itv/*"le Prof :"*/);//Cette syntaxe car Prof pas instancié dans le main
cout << endl;
}
break;
case 4:
if (flag == 0) {
cout << endl;
choix_E_P(choixEP);
flag = 1;
}
cout << endl;
if (choixEP == 1) {
EE.sort(trinom<Etudiant>);
//EE.sort(trinom2<Etudiant>); //pose problème
print(itl, EE, "Etudiant trié par nom:");
}
else {
sort(PP.begin(), PP.end(), trinom<Prof>);
print(itv, PP, "Prof trié par nom:");
}
break;
case 5:
if (flag == 0) {
cout << endl;
choix_E_P(choixEP);
flag = 1;
}
cout << endl;
if (choixEP == 1) {
EE.sort(triId<Etudiant>);
print(itl, EE, "Etudiant trié par Id:");
}
else {
sort(PP.begin(), PP.end(), triId<Prof>);
print(itv, PP, "Prof trié par Id:");
}
break;
case 6:
break;
default:cout <<"\nEntrer un nombre entre 0 et 6" << endl;
}
} while (choix != 6);
//cout << "Hello world!\n";
//system("pause");
return 0;
}
void menu(int & choix1) { //menu
cout << "\n0_Choix entre Etudiants ou Profs";
cout << "\n1_Saisie et affichage d'une List(Etudiant) ou d'un Vector(Profs)";
cout << "\n2_Ajouter au début ou à la fin et affichage de l'ensemble";
cout << "\n3_Créer dynamiquement 1 élément Etudiant ou Prof et chercher s'il existe";
cout << "\n4_Tri selon nom et affichage de l'ensemble";
cout << "\n5_Tri selon Id et affichage de l'ensemble";
cout << "\n6_Quitter";
cout << "\nEntrez votre choix:";
cin >> choix1;
}
void choix_E_P(int & choix1) { //case 0 //Ajouter texte si erreur
bool test;
do {
do {
test = 0;
cout << "Choix entre Etudiants(1) ou Profs(2):";
cin >> choix1;
if (cin.fail()) {
test = 1;
cin.clear();
cin.ignore();
}
} while (test == 1);
} while ((choix1 < 1) || (choix1 > 2));
}
void saisie(Etudiant E1[], int &taille) { //quand tableau
do {
cout << "\nCombien d'élément(s)?:";
cin >> taille;
if (cin.fail()) {
cin.clear();
cin.ignore();
}
} while ((taille <= 0) || (taille > Nmax));
for (int i = 0; i < taille; i++) {
cout << "E[" << i << "] ";
E1[i].saisie();
}
}
void affichage(Etudiant E1[], int taille) { //quand tableau
for (int i = 0; i < taille; i++) {
E1[i].affichage();
}
}
/*OLD = */template <class T, class U> void saisieLV(T &tab1){ //case 1
//Pourquoi ne pas passer l'élément unitaire objet temp et le nombre d'étudiants ou de profs ?
//template <class T, class U> void saisieLV(T &tab1, U temp, int &N) { //case 1
/*OLD =*/ U tab2;
int taille;
do {
cout << "\nCombien d'élément(s)?:";
cin >> taille;
if (cin.fail()) {
cin.clear();
cin.ignore();
}
} while (taille <= 0);
for(int i=0;i<taille;i++){
cout << "[" << i << "]"<<endl;
U temporaire;
/*OLD =*/ tab2.saisie();
// temporaire.saisie();
/*OLD =*/tab1.push_back(tab2);
// tab1.push_back(temporaire);
}
}
template <class T, class U> void print(U it, T tab, string S) { //case 1
//ici on passe l'itérateur par paramètres et par conséquent on ne le définit pas à l'intérieur de la fonction
cout << S;
for (it = tab.begin(); it != tab.end(); it++) {
it->affichage();
}
cout << endl;
}
template <class T, class U, class V> void ajouter(T &tab) { //case 2 //Ajouter texte si erreur
//Pourquoi la 3e classe V pour l'itérateur et pourquoi ne pas utiliser directement tab.begin() dans les paramètres des fonctions insert et push_back ?
//Et pourquoi ne pas utiliser l'élément individuel U temp dans les paramètres de la fonction et aussi le nombre de profs ou d'étudiants par références ?
bool test;
int choix1=0;
V it;
it = tab.begin();
U temp;
do {
do {
test = 0;
cout << "Choix entre ajouter au Début(1) ou à la Fin(2):";
cin >> choix1;
if (cin.fail()) {
test = 1;
cin.clear();
cin.ignore();
}
} while (test == 1);
} while ((choix1 < 1) || (choix1 > 2));
cout << "\nNouvel élément:"<< endl;
temp.saisie();
if (choix1 == 1) {
tab.insert(it, temp);
}
else {
tab.push_back(temp);
}
}
template <class T, class U, class V> void chercherstat(T tab) { //case 3 //pose problème
V it;
U temp;
cout << "\nQuel élément chercher:" << endl;
temp.saisie();
it = find(tab.begin(), tab.end(), 15);
if (it != tab.end()) {
temp.affichage();
cout << "\n existe";
}
else {
temp.affichage();
cout << "\n n'existe pas";
}
}
template <class T, class U, class V> void chercherdyn(T tab) { //case 3 //pose problème
V it;
U *temp=new U;
cout << "\nQuel élément chercher:" << endl;
temp->saisie();
it = find(tab.begin(),tab.end(), temp);
if (it != tab.end()) {
temp->affichage();
cout << "\n existe";
}
else {
temp->affichage();
cout << "\n n'existe pas";
}
delete temp;
temp = 0;
}
template <class T, class U, class V> void RechercherTdyn(T tab, U tempo, V &itlv) { //case 3 //version Carlo
V it;
U *temp=new U;
cout << "\nQuel élément chercher:" << endl;
temp->saisie();
it = find(tab.begin(),tab.end(), temp);
if (it != tab.end()) {
temp->affichage();
cout << "\n existe";
}
else {
temp->affichage();
cout << "\n n'existe pas";
}
delete temp;
temp = 0;
}
void trinoml(list<Etudiant> &tab) { //case 4 //pose problème
Etudiant temp;
list<Etudiant>::iterator it;
bool tri;
do {
tri = false;
for (it = tab.begin(); it != tab.end()--; it++) {
if (it->getnom() > (it++)->getnom()) {
temp = *it; // entre parenthèse?
*it = *(it++);
*(it++) = temp;
}
}
} while (tri);
}
void triIdl(list<Etudiant> &tab) { //case 4 //pose problème
Etudiant temp;
list<Etudiant>::iterator it;
bool tri;
do {
tri = false;
for (it = tab.begin(); it != tab.end()--; it++) {
if (it->getId() > (it++)->getId()) {
temp = *it;
*it = *it++;
*it++ = temp;
}
}
} while (tri);
}
//ceci fonctionne--------------------------------------------------------------QUESTIONS------------------------------------------------------
//template <class T> bool trinom(T, T);
//EE.sort(trinom<Etudiant>); //pq pas trinom<Etudiant>?()?
template <class T> bool trinom(T a, T b) { //case 4
return a.getnom() < b.getnom();
}
//ceci ne fonctionne pas pq?
//template <class T> bool trinom2();
//EE.sort(trinom2<Etudiant>); // pose problème quand ceci dans le main
template <class T> bool trinom2() { //case 4
T a, b;
return a.getnom() < b.getnom();
}
template <class T> bool triId(T a, T b) { //case 5
return a.getId() < b.getId();
}
void rechercheE(list<Etudiant> liste) { //case 3
bool test=0;
Etudiant *E = new Etudiant();
E->saisie();
int j = 1;
list <Etudiant>::iterator i;
for (i = liste.begin(); i != liste.end(); i++, j++) {
if (*i == *E) {
cout << "\nEtudiant a la position:" << j;
test = 1;
}
}
if (test == 0) {
cout << "\nEtudiant inexistant";
}
delete E;
E = 0;
}
void rechercheP2(vector<Prof> vect) { //case 3
bool test = 0;
Prof *P = new Prof; //Prof();?
P->saisie();
for (int i = 0; i<vect.size(); i++) {
if (vect[i] == *P) {
cout << "\nLe prof est a la position:" << i + 1 << endl;
test = 1;
}
}
if (test == 0) {
cout << "\nProf inexistant";
}
delete P;
P = 0;
}
//Exemple qui fonctionne pour essayer de faire un template
/*template <class T, class U> void print(U it, T tab, string S) { //case 1 //pour list et vector
cout << S;
for (it = tab.begin(); it != tab.end(); it++) { // pourquoi fonctionne ici et non pour recherche vect
it->affichage();
}
cout << endl;
}*/
void rechercheP1(vector<Prof> vect) { //pose problème
bool test = 0;
Prof *P = new Prof; //Prof()
P->saisie();
int j = 1;
vector <Prof>::iterator i;
for (i = vect.begin(); i != vect.end(); i++, j++) { //pourquoi ne fonctionne pas ici et fonctionne pour print
if (*i == *P) {
cout << "\nLe prof est a la position:" << j;
test = 1;
}
}
if (test == 0) {
cout << "\nProf inexistant";
}
delete P;
P = 0;
}
template <class T, class U, class V> void rechercheP3(U it, T tab) { //pose problème
bool test = 0;
V *P = new V;
P->saisie();
int j = 1;
for (it = tab.begin(); it != tab.end(); it++, j++) { //pourquoi ne fonctionne pas ici et fonctionne pour print
if (*i == *P) { //rechercheP3<vector<Prof>, vector<Prof>::iterator, Prof>(itv, PP);
cout << "\nLe prof est a la position:" << j;
test = 1;
}
}
if (test == 0) {
cout << "\nProf inexistant";
}
delete P;
P = 0;
}
void find2(list<Etudiant> liste) { //case 3
Etudiant *E = new Etudiant();
E->saisie();
list <Etudiant>::iterator it;
it = find(liste.begin(), liste.end(), *E);
if (it != liste.end()) {
cout << "\nElément trouver";
}
else {
cout << "\nElément inexistant";
}
delete E;
E = 0;
} |
Partager