Advertisement
Guest User

Elena_Papazova

a guest
May 6th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class User {
  6. private:
  7.     char username[50], password[50], email[50];
  8. public:
  9.     User(char *username=" ", char *password=" ", char *email=" ") {
  10.         strcpy(this->username,username);
  11.         strcpy(this->password,password);
  12.         strcpy(this->email,email);
  13.     }
  14.     virtual double popularity()=0;
  15.     const static double vrednostL;  //staticni clenovi
  16.     const static double vrednostK;
  17.     const static double vrednostT;
  18.     virtual User & operator =(const User &us) {
  19.         if(this==&us) return *this;
  20.         strcpy(username,us.username);
  21.         strcpy(password,us.password);
  22.         strcpy(email,us.email);
  23.         return *this;
  24.     }
  25.     User(const User &us) {
  26.         strcpy(username,us.username);
  27.         strcpy(password,us.password);
  28.         strcpy(email,us.email);
  29.     }
  30.  
  31. };
  32.  
  33. const double User :: vrednostL=0.1;
  34. const double User :: vrednostK=0.5;  //inicijalizacija
  35. const double User :: vrednostT=0.5;
  36.  
  37.  
  38. class FacebookUser:public User {
  39. private:
  40.     int brP, brL, brK;
  41. public:
  42.     FacebookUser(char *username=" ", char* password=" ", char * email=" ",int brP=0, int brL=0, int brK=0):User(username,password,email) {
  43.         this->brP=brP;
  44.         this->brL=brL;
  45.         this->brK=brK;
  46.     }
  47.     FacebookUser(const FacebookUser &f) {
  48.         brP=f.brP;
  49.         brL=f.brL;
  50.         brK=f.brK;
  51.     }
  52.     double popularity() {
  53.         return brP+brL*vrednostL+brK*vrednostK;
  54.     }
  55.     FacebookUser &operator =(const FacebookUser &f) {
  56.         if(this==&f) return *this;
  57.         this->User::operator=(f);
  58.         brP=f.brP;
  59.         brL=f.brL;
  60.         brK=f.brK;
  61.         return *this;
  62.     }
  63.  
  64. };
  65. class TwitterUser: public User {
  66. private:
  67.     int brF, brTw;
  68. public:
  69.     TwitterUser(char *username="", char* password="", char* email="", int brF=0, int brTw=0):User(username,password,email) {
  70.         this->brF=brF;
  71.         this->brTw=brTw;
  72.     }
  73.     TwitterUser(const TwitterUser &tw) {
  74.         brF=tw.brF;
  75.         brTw=tw.brTw;
  76.     }
  77.     double popularity() {
  78.         return brF+ brTw*vrednostT;
  79.     }
  80.     TwitterUser &operator =(const TwitterUser &t) {
  81.         if(this==&t) return *this;
  82.         this->User::operator=(t);
  83.         brF=t.brF;
  84.         brTw=t.brTw;
  85.         return *this;
  86.     }
  87.  
  88. };
  89. class SocialNetwork {
  90. private:
  91.     User **u;
  92.     int korisnici;
  93. public:
  94.     const static int max=5;
  95.     SocialNetwork() {
  96.         u = new User*[0];
  97.         korisnici=0;
  98.     }
  99.     ~SocialNetwork() {
  100.         /*for(int i=0; i<korisnici; i++)
  101.             delete u[i];
  102.         delete [] u;*/
  103.     }
  104.     SocialNetwork(const SocialNetwork &sn) {
  105.         korisnici=sn.korisnici;
  106.         u=new User *[korisnici];
  107.         for(int i=0; i<korisnici; i++)u[i]=sn.u[i];
  108.     }
  109.     SocialNetwork &operator =(const SocialNetwork &SN) {
  110.         if(this==&SN) return *this;
  111.         /*for(int i=0; i<korisnici; i++)
  112.             delete u[i];*/
  113.         //delete [] u;
  114.         korisnici=SN.korisnici;
  115.         u= new User*[korisnici];
  116.         for(int i=0; i<korisnici; i++)
  117.             u[i]=SN.u[i];
  118.         return *this;
  119.     }
  120.     SocialNetwork &operator +=(User *nov) {
  121.         User **tmp= new User *[korisnici+1];
  122.         for(int i=0; i<korisnici; i++)
  123.             tmp[i]=u[i];
  124.         tmp[korisnici]=nov;                    //* help here?? *
  125.  
  126.        /* for(int i=0; i<korisnici; i++)
  127.             delete [] u;*/
  128.         korisnici++;
  129.         //u= new User *[korisnici];
  130.         delete [] u;
  131.         u=tmp;
  132.         /*for(int i=0; i<korisnici; i++)
  133.             delete [] tmp;*/
  134.         return *this;
  135.  
  136.     }
  137.     int getMaxK() {
  138.         return max;
  139.     }
  140.     int getK() {
  141.         return korisnici;
  142.     }
  143.     float avgPopularity() {
  144.         float avg;
  145.         for(int i=0; i<korisnici; i++)
  146.             avg+=u[i]->popularity();
  147.         return avg/korisnici;
  148.     }
  149.     void changeMaximumSize(int number) {
  150.         User **tmp=new User *[number];
  151.         for(int i=0; i<korisnici; i++)
  152.             tmp[i]=u[i];
  153.         /*for(int i=0; i<korisnici; i++)
  154.             delete [] u;*/
  155.         korisnici=number;
  156.         u= new User *[number];
  157.         for(int i=0; i<korisnici; i++)
  158.             u[i]=tmp[i];
  159.     }
  160.  
  161. };
  162.  
  163. class InvalidPassword {
  164. private:
  165.     char text[50];
  166. public:
  167.     InvalidPassword(char *text="") {
  168.         strcpy(this->text,text);
  169.     }
  170.     void message() {
  171.         cout<<text;
  172.     }
  173.  
  174. };
  175. class InvalidEmail {
  176. private:
  177.     char text[50];
  178. public:
  179.     InvalidEmail(char * text="") {
  180.         strcpy(this->text,text);
  181.     }
  182.     void message() {
  183.         cout<<text;   //void message(char *text)
  184.     }
  185. };
  186. class MaximumSizeLimit {
  187. private:
  188.     int number;
  189. public:
  190.     MaximumSizeLimit(int number=0) {
  191.         this->number=number;
  192.     }
  193.     void message() {
  194.         cout<<"You can't add more than "<<number<<" users"<<endl;
  195.     }
  196. };
  197.  
  198.  
  199. int main() {
  200.  
  201.     SocialNetwork network = SocialNetwork();
  202.     int n;
  203.     cin >> n;
  204.     char username[50];
  205.     char password[50];
  206.     char email[50];
  207.     int userType;
  208.     for (int i=0; i < n; ++i) {
  209.         cin >> username;
  210.         cin >> password;
  211.         cin >> email;
  212.         cin >> userType;
  213.         if (userType == 1) {
  214.             int friends;
  215.             int likes;
  216.             int comments;
  217.             cin >> friends >> likes >> comments;
  218.  
  219.             try { // TODO: Try-catch
  220.                 User * u = new FacebookUser(username,password,email,friends,likes,comments);
  221.                 network += u;
  222.                 int GB, MB,BROJ;
  223.                 for(int i=0; i<strlen(password); i++) {
  224.                     if(isupper(password[i]))
  225.                         GB++;
  226.                     if(islower(password[i]))
  227.                         MB++;
  228.                     if(isalnum(password[i]))
  229.                         BROJ++;
  230.                 }
  231.                 if(GB<1 || MB<1 || BROJ<1)
  232.                     throw 1;
  233.                 int flag=1;
  234.                 for(int i=0; i<strlen(email); i++) {
  235.                     if(email[i]=='@') {
  236.                         flag=0;
  237.                         break;
  238.                     }
  239.  
  240.                 }
  241.                 if (flag) throw 2;
  242.  
  243.             } catch(int n) {
  244.                 if(n==1) InvalidPassword("Password is too weak"); //InvalidPassword::message();
  245.                 if(n==2) InvalidEmail("Mail is not valid"); //InvalidEmail::message();
  246.             }
  247.         } else {
  248.             int followers;
  249.             int tweets;
  250.             cin >> followers >> tweets;
  251.  
  252.             try { // TODO: Try-catch
  253.                 User * u= new TwitterUser(username,password,email,followers,tweets);
  254.                 network += u;
  255.                 int GB, MB,BROJ;
  256.                 for(int i=0; i<strlen(password); i++) {
  257.                     if(isupper(password[i]))
  258.                         GB++;
  259.                     if(islower(password[i]))
  260.                         MB++;
  261.                     if(isalnum(password[i]))
  262.                         BROJ++;
  263.                 }
  264.                 if(GB<1 || MB<1 || BROJ<1)
  265.                     throw 1;
  266.                 int flag=1;
  267.                 for(int i=0; i<strlen(email); i++) {
  268.                     if(email[i]=='@') {
  269.                         flag=0;
  270.                         break;
  271.                     }
  272.  
  273.                 }
  274.                 if (flag) throw 2;
  275.             } catch(int n) {
  276.                 if(n==1) InvalidPassword("Password is too weak"); //InvalidPassword::message("Password is too weak");
  277.                 if(n==2) InvalidEmail("Mail is not valid"); //InvalidEmail::message("Mail is not valid");
  278.             }                                                // ~ fix later ~
  279.         }
  280.  
  281.     }
  282.     network.changeMaximumSize(6);
  283.     cin >> username;
  284.     cin >> password;
  285.     cin >> email;
  286.     int followers;
  287.     int tweets;
  288.     cin >> followers >> tweets;
  289.  
  290.     try { // TODO: Try-catch
  291.         User * u= new TwitterUser(username,password,email,followers,tweets);
  292.         network += u;
  293.         if(network.getMaxK()<network.getK()) {
  294.             int newK= network.getK();
  295.             throw 3;
  296.         }
  297.     } catch (int n) {
  298.         if (n==3) MaximumSizeLimit(newK);   //MaximumSizeLimit::message(newK);
  299.     }
  300.     cout << network.avgPopularity();
  301.  
  302. }
  303. //im tired
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement