Advertisement
1cutcut1

bez pas

May 25th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.54 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. class UserAlreadyExistsException
  6. {
  7.     char* user;
  8.  
  9. public:
  10.     UserAlreadyExistsException(char *user="")
  11.     {
  12.         this->user = new char[strlen(user) + 1];
  13.         strcpy(this->user, user);
  14.     }
  15.  
  16.     void message()
  17.     {
  18.         cout << "User with username " << user << " already exists!\n";
  19.     }
  20. };
  21.  
  22. class UserNotFoundException
  23. {
  24.     char* user;
  25.  
  26. public:
  27.     UserNotFoundException(char *user="")
  28.     {
  29.         this->user = new char[strlen(user) + 1];
  30.         strcpy(this->user, user);
  31.     }
  32.  
  33.     void message()
  34.     {
  35.         cout << "User with username " << user << " was not found!\n";
  36.     }
  37. };
  38.  
  39. class FriendsLimitExceededException
  40. {
  41.     int n;
  42.  
  43. public:
  44.     FriendsLimitExceededException(int n=0)
  45.     {
  46.         this->n = n;
  47.     }
  48.  
  49.     void message()
  50.     {
  51.         cout << "Can't have more than " << n << " friends.\n";
  52.     }
  53. };
  54.  
  55. class User
  56. {
  57. private:
  58.     char username[50];
  59.     int age;
  60.     int friends;
  61.     static int maxFriends;
  62.  
  63. public:
  64.     User(char *username = "", int age = 18) : age(age)
  65.     {
  66.         strcpy(this->username, username);
  67.         friends = 0;
  68.     }
  69.  
  70.     friend ostream &operator<<(ostream &os, const User &user)
  71.     {
  72.         os << "Username: " << user.username << " Age: " << user.age << " # of friends: " << user.friends;
  73.         return os;
  74.     }
  75.  
  76.     User &operator++()
  77.     {
  78.         if(friends >= maxFriends)
  79.         {
  80.             throw FriendsLimitExceededException(maxFriends);
  81.         }
  82.  
  83.         ++friends;
  84.         return *this;
  85.     }
  86.  
  87.     User &operator--()
  88.     {
  89.         --friends;
  90.         return *this;
  91.     }
  92.  
  93.     static void setLimit(int _maxFriends)
  94.     {
  95.         maxFriends = _maxFriends;
  96.     }
  97.  
  98.     friend class SocialNetwork;
  99.  
  100.     ~User() { }
  101. };
  102.  
  103. class SocialNetwork
  104. {
  105. private:
  106.     User *users;
  107.     int n;
  108. public:
  109.     SocialNetwork()
  110.     {
  111.         n = 0;
  112.         users = new User[n];
  113.     }
  114.  
  115.     SocialNetwork &operator+=(User &u)
  116.     {
  117.         for(int i = 0; i < n; i++)
  118.         {
  119.             if(strcmp(u.username, users[i].username) == 0)
  120.                 throw UserAlreadyExistsException(u.username);
  121.         }
  122.  
  123.         User *tmp = new User[n + 1];
  124.         for (int i = 0; i < n; i++)
  125.         {
  126.             tmp[i] = users[i];
  127.         }
  128.         tmp[n++] = u;
  129.         delete[] users;
  130.         users = tmp;
  131.         return *this;
  132.     }
  133.  
  134.     void friendRequest(char *firstUsername, char *secondUsername)
  135.     {
  136.         bool b = false;
  137.         for (int i = 0; i < n; i++)
  138.         {
  139.             if (strcmp(users[i].username, secondUsername) == 0)
  140.             {
  141.                 b = true;
  142.             }
  143.         }
  144.  
  145.         if(b =- false)
  146.         {
  147.             throw UserNotFoundException(secondUsername);
  148.         }
  149.  
  150.         for (int i = 0; i < n; i++)
  151.         {
  152.             if (strcmp(users[i].username, firstUsername) == 0)
  153.             {
  154.  
  155.                 for (int j = 0; j < n; j++)
  156.                 {
  157.                     if (strcmp(users[j].username, secondUsername) == 0)
  158.                     {
  159.                         try
  160.                         {
  161.                             ++users[i];
  162.                         }
  163.                         catch(FriendsLimitExceededException& e)
  164.                         {
  165.                             e.message();
  166.                             return;
  167.                         }
  168.  
  169.                         try
  170.                         {
  171.                             ++users[j];
  172.                         }
  173.                         catch(FriendsLimitExceededException& e)
  174.                         {
  175.                             e.message();
  176.                             --users[i];
  177.                         }
  178.  
  179.                         return;
  180.                     }
  181.                 }
  182.  
  183.             }
  184.         }
  185.  
  186.         throw UserNotFoundException(firstUsername);
  187.     }
  188.  
  189.     friend ostream &operator<<(ostream &os, const SocialNetwork &network)
  190.     {
  191.         os << "Users: " << endl;
  192.         for (int i=0; i<network.n; i++)
  193.         {
  194.             os << network. users[i] << endl;
  195.         }
  196.         return os;
  197.     }
  198. };
  199.  
  200. int User::maxFriends = 3;
  201.  
  202. int main()
  203. {
  204.     SocialNetwork sn;
  205.     int n;
  206.     cin >> n;
  207.     for (int i=0; i<n; i++)
  208.     {
  209.         char username[50];
  210.         int age;
  211.         cin >> username >> age;
  212.         User u(username, age);
  213.  
  214.         try
  215.         {
  216.             sn += u;
  217.         }
  218.         catch(UserAlreadyExistsException& e)
  219.         {
  220.             e.message();
  221.         }
  222.  
  223.     }
  224.  
  225.     cout << "Registration of users " << endl;
  226.     cout << sn << endl;
  227.     cout << "Friend requests " << endl;
  228.  
  229.     int friendships;
  230.     cin >> friendships;
  231.     for (int i=0; i<friendships; i++)
  232.     {
  233.         char username1[50], username2[50];
  234.         cin >> username1 >> username2;
  235.  
  236.         try
  237.         {
  238.             sn.friendRequest(username1, username2);
  239.         }
  240.         catch(UserNotFoundException& e)
  241.         {
  242.             e.message();
  243.         }
  244.  
  245.     }
  246.  
  247.     cout << sn << endl;
  248.  
  249.     cout << "CHANGE STATIC VALUE" << endl;
  250.  
  251.     int maxFriends;
  252.     cin >> maxFriends;
  253.     cin >> friendships;
  254.     User::setLimit(maxFriends);
  255.     for (int i=0; i<friendships; i++)
  256.     {
  257.         char username1[50], username2[50];
  258.         cin >> username1 >> username2;
  259.  
  260.         try
  261.         {
  262.             sn.friendRequest(username1, username2);
  263.         }
  264.         catch(UserNotFoundException& e)
  265.         {
  266.             e.message();
  267.         }
  268.     }
  269.     cout << sn;
  270.     return 0;
  271. }
  272. ---------------------------------------------------------------------------------------------------------------------------------------
  273. #include<iostream>
  274. #include<cstring>
  275. using namespace std;
  276.  
  277. class UserAlreadyExistsException {
  278.     char username[50];
  279. public:
  280.     UserAlreadyExistsException(char *user=" ")
  281.     {
  282.         strcpy (username,user);
  283.     }
  284.     void showMessage()
  285.     {
  286.         cout<<"User with username " <<username<<" already exists!"<<endl;
  287.     }
  288. //TODO
  289. };
  290.  
  291. class UserNotFoundException {
  292.     char username[50];
  293. public:
  294.     UserNotFoundException(char *name=" ")
  295.     {
  296.         strcpy (username,name);
  297.     }
  298.     void showMessage()
  299.     {
  300.         cout<<"User with username "<<username<<" was not found!"<<endl;
  301.     }
  302. };
  303.  
  304. class FriendsLimitExceededException {
  305.     int number;
  306. //TODO
  307. public:
  308.     FriendsLimitExceededException(int broj=0)
  309.     {
  310.         number=broj;
  311.     }
  312.    
  313.     void showMessage()
  314.     {
  315.         cout<<"Can't have more than "<<number<<" friends."<<endl;
  316.     }
  317. };
  318.  
  319. class User {
  320. private:
  321.     char username[50];
  322.     int age;
  323.     int friends;
  324.  
  325.  
  326. public:
  327.     static int LIMIT;
  328.     User(char *username = "", int age = 18) : age(age) {
  329.         strcpy(this->username, username);
  330.         friends = 0;
  331.     }
  332.  
  333.     friend ostream &operator<<(ostream &os, const User &user) {
  334.         os << "Username: " << user.username << " Age: " << user.age << " # of friends: " << user.friends;
  335.         return os;
  336.     }
  337.  
  338.     User &operator++() {
  339.         ++friends;
  340.         return *this;
  341.     }
  342.     static void setLimit(int a)
  343.     {
  344.         LIMIT=a;
  345.     }
  346.  
  347.     friend class SocialNetwork;
  348.  
  349.  
  350. };
  351. int User::LIMIT=3;
  352. class SocialNetwork {
  353. private:
  354.     User *users;
  355.     int n;
  356. public:
  357.     SocialNetwork() {
  358.         n = 0;
  359.         users = new User[n];
  360.     }
  361.  
  362.     SocialNetwork &operator+=(User &u) {
  363.        
  364.         for (int i=0;i<n;i++)
  365.         {
  366.             if (strcmp(users[i].username,u.username)==0)
  367.             {
  368.                 throw UserAlreadyExistsException(u.username);
  369.             }
  370.         }
  371.         User *tmp = new User[n + 1];
  372.         for (int i = 0; i < n; i++) {
  373.             tmp[i] = users[i];
  374.         }
  375.         tmp[n++] = u;
  376.         delete[] users;
  377.         users = tmp;
  378.         return *this;
  379.     }
  380.  
  381.     void friendRequest(char *firstUsername, char *secondUsername) {
  382.         int flag1=0,flag2=0;
  383.         for (int i = 0; i < n; i++) {
  384.             if (strcmp(users[i].username, firstUsername) == 0) {
  385.                 flag1=1;
  386.                 for (int j = 0; j < n; j++) {
  387.                     if (strcmp(users[j].username, secondUsername) == 0) {
  388.                         flag2=1;
  389.                         if (users[i].friends<User::LIMIT && users[j].friends<User::LIMIT){
  390.                         ++users[i];
  391.                         ++users[j];
  392.                         }
  393.                         else
  394.                         throw FriendsLimitExceededException(User::LIMIT);
  395.                         return;
  396.                     }
  397.                 }
  398.             }
  399.         }
  400.         if (flag2==0)
  401.         {
  402.             throw UserNotFoundException(secondUsername);
  403.         }
  404.         else if (flag1==0)
  405.         {
  406.             throw UserNotFoundException(firstUsername);
  407.         }
  408.     }
  409.  
  410.     friend ostream &operator<<(ostream &os, const SocialNetwork &network) {
  411.         os << "Users: " << endl;
  412.         for (int i=0;i<network.n;i++) {
  413.             os << network. users[i] << endl;
  414.         }
  415.         return os;
  416.     }
  417. };
  418.  
  419.  
  420. int main() {
  421.     SocialNetwork sn;
  422.     int n;
  423.     cin >> n;
  424.     for (int i=0;i<n;i++){
  425.         char username[50]; int age;
  426.         cin >> username >> age;
  427.         try{ User u(username, age);
  428.             sn += u;
  429.         }
  430.         catch (UserAlreadyExistsException e)
  431.         {
  432.             e.showMessage();
  433.         }
  434.  
  435.     }
  436.  
  437.     cout << "Registration of users " << endl;
  438.     cout << sn << endl;
  439.     cout << "Friend requests " << endl;
  440.  
  441.     int friendships;
  442.     cin >> friendships;
  443.     for (int i=0;i<friendships;i++){
  444.         char username1[50], username2[50];
  445.         cin >> username1 >> username2;
  446.         try{
  447.             sn.friendRequest(username1, username2);
  448.  
  449.         }
  450.         catch (UserNotFoundException e)
  451.         {
  452.             e.showMessage();
  453.         }
  454.         catch (FriendsLimitExceededException e)
  455.         {
  456.             e.showMessage();
  457.         }
  458.     }
  459.  
  460.     cout << sn << endl;
  461.  
  462.     cout << "CHANGE STATIC VALUE" << endl;
  463.  
  464.     int maxFriends;
  465.     cin >> maxFriends;
  466.     cin >> friendships;
  467.     User::setLimit(maxFriends);
  468.     for (int i=0;i<friendships;i++){
  469.         char username1[50], username2[50];
  470.         cin >> username1 >> username2;
  471.         try{
  472.             sn.friendRequest(username1, username2);
  473.  
  474.         }
  475.         catch (UserNotFoundException e)
  476.         {
  477.             e.showMessage();
  478.         }
  479.         catch (FriendsLimitExceededException e)
  480.         {
  481.             e.showMessage();
  482.         }
  483.  
  484.     }
  485.     cout << sn;
  486.     return 0;
  487. }
  488.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement