diff --git "a/S\303\251rie 11/amis.cc" "b/S\303\251rie 11/amis.cc" new file mode 100644 index 0000000..c11519e --- /dev/null +++ "b/S\303\251rie 11/amis.cc" @@ -0,0 +1,68 @@ +// amis.cc +// Auteur : Quentin Berling +// Version : 1.0 +#include +#include +#include +using namespace std; + +// prédéclaration nécessaire pour le typedef +struct Personne; + +typedef vector Liste_Personnes; + +struct Personne { + string nom; + Liste_Personnes amis; +}; + +void afficher_ami(Personne pers); +bool est_ami_avec(const Personne &pers, const Personne &pers2); +void ajouter_amis(Personne &pers, const Personne &new_friend); +void enlever_amis(Personne &pers, const Personne &old_friend); + +int main() { + Personne alice={"Alice",{}}; + Personne bob={"Bob",{}}; + Personne bertrand={"Bertrand",{}}; + afficher_ami(alice); + ajouter_amis(alice,bob); + ajouter_amis(alice,bertrand); + afficher_ami(alice); + enlever_amis(alice,bob); + afficher_ami(alice); + exit(0); +} + +void afficher_ami(Personne pers) { + cout << "Les amis de " << pers.nom << " sont : " << endl; + for (auto ami : pers.amis) { + cout << ami->nom << endl; + } +} + +bool est_ami_avec(const Personne &pers, const Personne &pers2) { + for (auto ami : pers.amis) { + if (ami->nom == pers2.nom) { + return 1; + break; + } + } + return 0; +} + +void ajouter_amis(Personne &pers, const Personne &new_friend) { + if (est_ami_avec(pers, new_friend) == 0) { + pers.amis.push_back(&new_friend); + } +} + +void enlever_amis(Personne &pers, const Personne &old_friend) { + for (Liste_Personnes::iterator + iter = pers.amis.begin(); iter != pers.amis.end(); ++iter) { + if (*iter == &old_friend) { + pers.amis.erase( iter ); + break; + } + } +}