Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <cctype>
- using namespace std;
- class InvalidPassword{
- char text[100];
- public:
- InvalidPassword(const char *text){
- strcpy(this->text,text);
- }
- void message()
- {
- cout<<text<<endl;
- }
- };
- class InvalidEmail{
- char text[100];
- public:
- InvalidEmail(const char *text){
- strcpy(this->text,text);
- }
- void message()
- {
- cout<<text<<endl;
- }
- };
- class MaximumSizeLimit{
- int n;
- public:
- MaximumSizeLimit(int n)
- {
- this->n=n;
- }
- void message()
- {
- cout<<"You can't add more than "<<n<<" users."<<endl;
- }
- };
- bool checkPassword(const char *password)
- {
- int br1=0,br2=0,br3=0;
- for(int i=0;i<strlen(password);i++)
- {
- if(isupper(password[i])) br1++;
- if(islower(password[i])) br2++;
- if(isdigit(password[i])) br3++;
- }
- if(br1&&br2&&br3) return true;
- return false;
- }
- bool checkEmail(const char *email)
- {
- int br=0;
- for(int i=0;i<strlen(email);i++)
- {
- if(email[i]=='@') br++;;
- }
- if(br==1) return true;
- 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="")
- {
- if(checkEmail(e)==false) throw InvalidEmail("Mail is not valid.");
- if(checkPassword(p)==false) throw InvalidPassword("Password is too weak.");
- else{
- strcpy(username,u);
- strcpy(password,p);
- strcpy(email,e);
- }
- }
- virtual double popularity()=0;
- };
- class FacebookUser:public User{
- private:
- int bp,bl,bk;
- public:
- FacebookUser(const char *u="",const char *p="",const char *e="",int bp=0,int bl=0,int bk=0):User(u,p,e)
- {
- this->bp=bp;
- this->bl=bl;
- this->bk=bk;
- }
- double popularity()
- {
- return bp+(bl*0.1)+(bk*0.5);
- }
- };
- class TwitterUser:public User{
- private:
- int bs,bt;
- public:
- TwitterUser(const char *u="",const char *p="",const char *e="",int bs=0,int bt=0):User(u,p,e)
- {
- this->bs=bs;
- this->bt=bt;
- }
- double popularity()
- {
- return bs+(bt*0.5);
- }
- };
- class SocialNetwork{
- private:
- User **niza;
- int broj;
- static int max;
- public:
- SocialNetwork(){
- broj=0;
- niza=new User*[max];
- }
- SocialNetwork &operator+=(User *u)
- {
- if(broj==max) throw MaximumSizeLimit(5);
- niza[broj++]=u;
- return *this;
- }
- double avgPopularity()
- {
- double av=0;
- for(int i=0;i<broj;i++)
- {
- av+=niza[i]->popularity();
- }
- return av/broj;
- }
- void changeMaximumSize(int n)
- {
- max=n;
- }
- };
- 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 obj)
- {
- obj.message();
- }
- catch(MaximumSizeLimit obj)
- {
- obj.message();
- }
- catch(InvalidEmail obj)
- {
- obj.message();
- }
- }
- 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 obj)
- {
- obj.message();
- }
- catch(MaximumSizeLimit obj)
- {
- obj.message();
- }
- catch(InvalidEmail obj)
- {
- obj.message();
- }
- }
- }
- 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 obj)
- {
- obj.message();
- }
- catch(MaximumSizeLimit obj)
- {
- obj.message();
- }
- catch(InvalidEmail obj)
- {
- obj.message();
- }
- cout << network.avgPopularity();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement