class System; class User { string myName; System * mySystem; vector <PrivateMessage> mySentMessages; vector <PrivateMessager *> myRecievedMessages; vector <User *> myFriends; public : User (const string & name, System * system = NULL) : myName (name), mySystem (system) { mySentMessages.reserve (20); } void addFriend (User * aFriend) { myFriends.push_back (aFriend); } // addFriend() const string & getName (void) const { return myName; } User * getFriend (unsigned i) const { return myFriends [i]; } const PrivateMessage & getSentMessage (unsigned i) const { return mySentMessages [i]; } const PrivateMessage * getRecievedMessage (unsigned i) const { return myRecievedMessages [i]; } System * getSystem (void) const { return mySystem; } unsigned getNbFriends (void) const { return myFriends.size (); } unsigned getNbSentMessages (void) const { return mySentMessages.size (); } unsigned getNbRecievedMessages (void) const { return myRecievedMessages.size (); } void sendMessage (const PrivateMessage * message) { myRecievedMessages.push_back (message); } // sendMessage() void addMessage (const PrivateMessage & message) { mySentMessages.push_back (message); } }; // User
Archives du 25 mai 2018
M2103-TP9-EXO-6
Dans l’espace de noms anonyme du fichier main.cpp
déclarer la classe System
, comme elle est décrite sur le schéma UML
. Le constructeur sera utilisé, comme dans le TD3 (TP4) pour tester le programme.
La fonction getUser()
renvoie le ième User
(pas une copie).
Idem pour getPublicMessage()
.
Voici le constructeur :
System (void) : myNbUsers (0), myNbMessages (0) { myUsers.reserve (1000); myPublicMessages.reserve (1000); // création des Users addUser ("Alfred"); addUser ("Alain"); addUser ("Sophie"); addUser ("Emmanuel"); addUser ("Christian"); addUser ("Petru"); addUser ("Marc"); /* verification des Users */ cout << myUsers.size () << endl; for (const User & user : myUsers) { cout << user.getName () << endl; } /* création de leurs amis * / for (unsigned i (0); i < myUsers.size (); ++i) for (unsigned j (0); j < myUsers.size () / 2; ++j) myUsers [i].addFriend (&myUsers [(i + ((j * 2) + 1)) % myUsers.size ()]); /* verification des amis * / for (unsigned i (0); i < myUsers.size (); ++i) { User user = myUsers [i]; cout << user.getName () << " a " << user.getNbFriends() << " amis : " << endl; for (unsigned j (0); j < user.getNbFriends (); ++j) cout << (user.getFriend (j))->getName () << endl; } /* création et envoi des messages privés * / for (User & user : myUsers) { for (unsigned j (0); j < user.getNbFriends (); ++j) { string content (string ("salut") + ' ' + (user.getFriend (j))->getName ()); PrivateMessage message (content, & user); user.addMessage (message); (user.getFriend (j))->sendMessage (&(user.getSentMessage (user.getNbSentMessages () - 1))); } } /* vérifications des messages envoyés * / for (unsigned i (0); i < myUsers.size (); ++i) { User user = myUsers [i]; cout << user.getName () << " a envoyé " << user.getNbSentMessages() << " messages : " << endl; for (unsigned j (0); j < user.getNbSentMessages (); ++j) { (user.getSentMessage (j)).displayContent(); cout << endl; } } /* vérifications des messages reçus * / for (unsigned i (0); i < myUsers.size (); ++i) { User user = myUsers [i]; cout << user.getName () << " a reçu " << user.getNbRecievedMessages() << " messages : " << endl; for (unsigned j (0); j < user.getNbRecievedMessages (); ++j) { cout << "de " << (user.getRecievedMessage (j))->getSender ()->getName () << " : "; (user.getRecievedMessage (j))->displayContent(); cout << endl; } } /* création et envoie des messages publiques * / string oneMessage ("a"); for (const User & user : myUsers) for (unsigned i (0); i < 5; ++i) { oneMessage += 'a'; (user.getSystem ())->addPublicMessage (oneMessage); } /* test de la création des messages * / for (const PublicMessage & message : myPublicMessages) { message.displayContent (); cout << endl; } /* création des like * / for (User & user : myUsers) for (unsigned i (0); i < myNbMessages; ++i) (user.getSystem ())->getPublicMessage (i).add (); /* test des likes * / for (const PublicMessage & message : myPublicMessages) { message.displayContent (); cout << " a " << message.getNbLikers () << " likers" << endl; } /* */ } // System()
Dans la fonction main()
il suffit de déclarer un objet System
pour effectuer les tests.
Au fur et à mesure de vos mises au point, déplacer l'espace entre *
et /
d'un commentaire à l'autre.
M2103-TP9-EXO-6-Corrigé
class System { unsigned myNbUsers; vector myUsers; unsigned myNbMessages; vector myPublicMessages; public : User & getUser (unsigned i) { return myUsers [i]; } PublicMessage & getPublicMessage (unsigned i) { return myPublicMessages [i]; } void addUser (const string & name) { myUsers.push_back (User (name, this)); ++myNbUsers; } // addUsers() void addPublicMessage (const string & message) { myPublicMessages.push_back (message); ++myNbMessages; } // addPublicMessage()) System (void) : myNbUsers (0), myNbMessages (0) { myUsers.reserve (1000); myPublicMessages.reserve (1000); // création des Users addUser ("Alfred"); addUser ("Alain"); addUser ("Sophie"); addUser ("Emmanuel"); addUser ("Christian"); addUser ("Petru"); addUser ("Marc"); /* verification des Users */ cout << myUsers.size () << endl; for (const User & user : myUsers) { cout << user.getName () << endl; } /* création de leurs amis * / for (unsigned i (0); i < myUsers.size (); ++i) for (unsigned j (0); j < myUsers.size () / 2; ++j) myUsers [i].addFriend (&myUsers [(i + ((j * 2) + 1)) % myUsers.size ()]); /* verification des amis * / for (unsigned i (0); i < myUsers.size (); ++i) { User user = myUsers [i]; cout << user.getName () << " a " << user.getNbFriends() << " amis : " << endl; for (unsigned j (0); j < user.getNbFriends (); ++j) cout << (user.getFriend (j))->getName () << endl; } /* création et envoi des messages privés * / for (User & user : myUsers) { for (unsigned j (0); j < user.getNbFriends (); ++j) { string content (string ("salut") + ' ' + (user.getFriend (j))->getName ()); PrivateMessage message (content, & user); user.addMessage (message); (user.getFriend (j))->sendMessage (&(user.getSentMessage (user.getNbSentMessages () - 1))); } } /* vérifications des messages envoyés * / for (unsigned i (0); i < myUsers.size (); ++i) { User user = myUsers [i]; cout << user.getName () << " a envoyé " << user.getNbSentMessages() << " messages : " << endl; for (unsigned j (0); j < user.getNbSentMessages (); ++j) { (user.getSentMessage (j)).displayContent(); cout << endl; } } /* vérifications des messages reçus * / for (unsigned i (0); i < myUsers.size (); ++i) { User user = myUsers [i]; cout << user.getName () << " a reçu " << user.getNbRecievedMessages() << " messages : " << endl; for (unsigned j (0); j < user.getNbRecievedMessages (); ++j) { cout << "de " << (user.getRecievedMessage (j))->getSender ()->getName () << " : "; (user.getRecievedMessage (j))->displayContent(); cout << endl; } } /* création et envoie des messages publiques * / string oneMessage ("a"); for (const User & user : myUsers) for (unsigned i (0); i < 5; ++i) { oneMessage += 'a'; (user.getSystem ())->addPublicMessage (oneMessage); } /* test de la création des messages * / for (const PublicMessage & message : myPublicMessages) { message.displayContent (); cout << endl; } /* création des like * / for (User & user : myUsers) for (unsigned i (0); i < myNbMessages; ++i) (user.getSystem ())->getPublicMessage (i).add (); /* test des likes * / for (const PublicMessage & message : myPublicMessages) { message.displayContent (); cout << " a " << message.getNbLikers () << " likers" << endl; } /* */ } // System() }; // System