Advertisement
TheBoar

ExPrep3

Jun 25th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. class Person{
  7. public:
  8.     string email;
  9.     string name;
  10.     string surname;
  11.  
  12.     Person() = default;
  13.  
  14.     Person(const char* email, const char* name, const char* surname){
  15.         this->email = email;
  16.         this->name = name;
  17.         this->surname = surname;
  18.     }
  19. };
  20.  
  21. class Following{
  22. public:
  23.     Person* follower;
  24.     Person* follows;
  25.  
  26.     Following() = default;
  27.     Following(Person* bjacz, Person* mastier){
  28.         this->follower = bjacz;
  29.         this->follows - mastier;
  30.     }
  31. };
  32.  
  33. class SocialGraph{
  34. public:
  35.     list <Person> people;
  36.     list <Following> foll;
  37.  
  38.     bool addPerson(const char* email, const char* name, const char* surname){
  39.         for(auto person:people){
  40.             if (person.email == email){
  41.                 return false;
  42.             }
  43.         }
  44.         this->people.emplace_back(Person(email,name,surname));
  45.     }
  46.     bool removePerson(const char* email){
  47.         for (auto i = foll.begin(); foll.end()!=i; ++i){
  48.             if (((*i).follows->email == email || (*i).follower->email == email)){
  49.                 i = foll.erase(i);
  50.             }
  51.         }
  52.         for (auto i = people.begin(); people.end()!=i; ++i){
  53.             if ((*i).email == email){
  54.                 i = people.erase(i);
  55.             }
  56.         }
  57.     }
  58.  
  59.     Person* findPerson(const char* email){
  60.         for(auto i = people.begin(); people.end()!=i; ++i){
  61.             if ((*i).email == email){
  62.                 return &(*i);
  63.             }
  64.         }
  65.     }
  66.  
  67.     bool addFollower(const char* emailTo,const char* emailFrom){
  68.         for(auto follow:foll) {
  69.             if (follow.follower->email == emailFrom && follow.follows->email == emailTo) {
  70.                 return false;
  71.             }
  72.         }
  73.         Following abc;
  74.         abc.follower = findPerson(emailFrom);
  75.         abc.follows = findPerson(emailTo);
  76.         foll.push_back(abc);
  77.         return true;
  78.     }
  79.  
  80.     bool unfollow(const char* emailTo,const char* emailFrom){
  81.         for(auto i = foll.begin(); foll.end()!=i; ++i){
  82.             if ((*i).follower->email == emailFrom && (*i).follows->email == emailTo){
  83.                 i = foll.erase(i);
  84.             }
  85.         }
  86.     }
  87.  
  88.     void printPeople(){
  89.         for(auto person:people){
  90.             cout << "name: " << person.name << "\nsurname: " << person.surname << "\nemail: " << person.email;
  91.             cout << "\n___________________________________________\n";
  92.         }
  93.     };
  94.  
  95.     void printFollowers(){
  96.         for(auto i = foll.begin(); foll.end()!=i; ++i){
  97.             cout << (*i).follower->name << " -> " << (*i).follows->name << "\n";
  98.         }
  99.     }
  100. };
  101.  
  102.  
  103. int main() {
  104.     SocialGraph a;
  105.     a.addPerson("obajtekmagda@gmail.com","Magdalena","Obajtek");
  106.     a.addPerson("aaaaaaaaaa@gmail.com","Aaaaa","Bbbbbbb");
  107.     a.addPerson("ccccccccccc@gmail.com","Ccccc","Dddddddddddd");
  108.     a.printPeople();
  109.     a.addFollower("obajtekmagda@gmail.com","ccccccccccc@gmail.com");
  110.  
  111.     cout << "\n\n";
  112.     a.printFollowers();
  113.     a.unfollow("obajtekmagda@gmail.com","ccccccccccc@gmail.com");
  114.     a.printFollowers();
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement