Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class InvalidPassword {
- public:
- void print()
- {
- cout<<"Password is too weak."<<endl;
- }
- };
- class InvalidEmail {
- public:
- void print()
- {
- cout<<"Mail is not valid."<<endl;
- }
- };
- class MaximumSizeLimit {
- private:
- int max;
- public:
- MaximumSizeLimit (int m)
- {
- maxx=m;
- }
- void print()
- {
- cout<<"You can't add more than "<<max1<<" users."<<endl;
- }
- };
- bool validenpassword(const char *p)
- {
- int golema=0, mala=0, broj=0;
- for (int i=0; i<strlen(p); i++)
- {
- if (isupper(p[i]))
- golema++;
- if (islower(p[i]))
- mala++;
- if (isdigit(p[i]))
- broj++;
- }
- if (broj!=0&&golema!=0 && mala!=0)
- return true;
- else
- return false;
- }
- bool validenemail(const char *e)
- {
- int brojac=0;
- for (int i=0; i<strlen(e); i++)
- {
- if(e[i]=='@')
- brojac++;
- }
- if (brojac==1)
- return true;
- else
- return false;
- }
- class User {
- protected:
- char username[50];
- char password[50];
- char email[50];
- public:
- User(const char *u="", const char *p="", const char *e="")
- {
- strcpy(username,u);
- if (validenpassword(p))
- strcpy(password,p);
- else
- throw new InvalidPassword();
- if (validenemail(e))
- strcpy(email,e);
- else
- throw new InvalidEmail();
- }
- virtual double popularity() = 0;
- ~User();
- };
- class FacebookUser : public User {
- private:
- int friends;
- int likes;
- int comments;
- static double like_koef;
- static double comment_koef;
- public:
- FacebookUser(const char *u="", const char *p="", const char *e="", int fr=0, int li=0, int co=0) : User(u,p,e)
- {
- friends = fr;
- likes = li;
- comments = co;
- }
- double popularity()
- {
- return friends + like_koef*likes + comment_koef*comments;
- }
- };
- class TwitterUser : public User{
- private:
- int followers;
- int tweets;
- static double tweet_koef;
- public:
- TwitterUser(const char *u="", const char *p="", const char *e="", int fol=0, int twe=0) : User(u,p,e)
- {
- followers = fol;
- tweets = twe;
- }
- double popularity()
- {
- return tweets*tweet_koef + followers;
- }
- };
- class SocialNetwork{
- private:
- User **niza;
- int tekovni_korisnici;
- static int max;
- public:
- SocialNetwork()
- {
- niza = new User*[0];
- tekovni_korisnici=0;
- }
- SocialNetwork &operator+=(User *u)
- {
- if (tekovni_korisnici==max)
- throw new MaximumSizeLimit(max);
- else
- {
- User **tmp = new User*[tekovni_korisnici+1];
- for (int i=0; i<tekovni_korisnici; i++)
- {
- tmp[i] = niza[i];
- }
- tmp[tekovni_korisnici++] = u;
- /*for (int i=0; i<tekovni_korisnici-1; i++)
- {
- delete [] niza[i];
- }
- delete [] niza;*/
- niza = tmp;
- }
- return *this;
- }
- double avgPopularity()
- {
- double vkupno=0;
- for (int i=0; i<tekovni_korisnici; i++)
- {
- vkupno+=niza[i]->popularity();
- }
- return vkupno/tekovni_korisnici;
- }
- static void changeMaximumSize(int br)
- {
- max=br;
- //cout<<"CHANGED to "<<max<<endl;
- }
- ~SocialNetwork() { delete [] niza; }
- };
- double FacebookUser::like_koef=0.1;
- double FacebookUser::comment_koef=0.5;
- double TwitterUser::tweet_koef=0.5;
- int SocialNetwork::max=5;
- int main() {
- SocialNetwork network = SocialNetwork();
- int n;
- cin >> n;
- char username[50];
- char password[50];
- char email[50];
- int userType;
- for (int i=0; i < n; ++i) {
- cin >> username;
- cin >> password;
- cin >> email;
- cin >> userType;
- if (userType == 1) {
- int friends;
- int likes;
- int comments;
- cin >> friends >> likes >> comments;
- // TODO: Try-catch
- try
- {
- User * u = new FacebookUser(username,password,email,friends,likes,comments);
- network += u;
- }
- catch(InvalidPassword *ip) { ip->print(); }
- catch(InvalidEmail *ie) { ie->print(); }
- catch(MaximumSizeLimit *isl) { isl->print(); }
- }
- else {
- int followers;
- int tweets;
- cin >> followers >> tweets;
- // TODO: Try-catch
- try
- {
- User * u= new TwitterUser(username,password,email,followers,tweets);
- network += u;
- }
- catch(InvalidPassword *ip) { ip->print(); }
- catch(InvalidEmail *ie) { ie->print(); }
- catch(MaximumSizeLimit *isl) { isl->print(); }
- }
- }
- network.changeMaximumSize(6);
- cin >> username;
- cin >> password;
- cin >> email;
- int followers;
- int tweets;
- cin >> followers >> tweets;
- // TODO: Try-catch
- try
- {
- User * u= new TwitterUser(username,password,email,followers,tweets);
- network += u;
- }
- catch(InvalidPassword *ip) { ip->print(); }
- catch(InvalidEmail *ie) { ie->print(); }
- catch(MaximumSizeLimit *isl) { isl->print(); }
- cout << network.avgPopularity();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement