Advertisement
masterm1nd99

e

May 16th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class InvalidPassword {
  6. public:
  7. void print()
  8. {
  9. cout<<"Password is too weak."<<endl;
  10. }
  11. };
  12.  
  13. class InvalidEmail {
  14. public:
  15. void print()
  16. {
  17. cout<<"Mail is not valid."<<endl;
  18. }
  19. };
  20.  
  21. class MaximumSizeLimit {
  22. private:
  23. int max;
  24. public:
  25. MaximumSizeLimit (int m)
  26. {
  27. maxx=m;
  28. }
  29. void print()
  30. {
  31. cout<<"You can't add more than "<<max1<<" users."<<endl;
  32. }
  33. };
  34.  
  35. bool validenpassword(const char *p)
  36. {
  37. int golema=0, mala=0, broj=0;
  38. for (int i=0; i<strlen(p); i++)
  39. {
  40. if (isupper(p[i]))
  41. golema++;
  42. if (islower(p[i]))
  43. mala++;
  44. if (isdigit(p[i]))
  45. broj++;
  46. }
  47. if (broj!=0&&golema!=0 && mala!=0)
  48. return true;
  49. else
  50. return false;
  51.  
  52. }
  53.  
  54. bool validenemail(const char *e)
  55. {
  56. int brojac=0;
  57. for (int i=0; i<strlen(e); i++)
  58. {
  59. if(e[i]=='@')
  60. brojac++;
  61. }
  62. if (brojac==1)
  63. return true;
  64. else
  65. return false;
  66. }
  67.  
  68. class User {
  69. protected:
  70. char username[50];
  71. char password[50];
  72. char email[50];
  73. public:
  74. User(const char *u="", const char *p="", const char *e="")
  75. {
  76. strcpy(username,u);
  77. if (validenpassword(p))
  78. strcpy(password,p);
  79. else
  80. throw new InvalidPassword();
  81.  
  82. if (validenemail(e))
  83. strcpy(email,e);
  84. else
  85. throw new InvalidEmail();
  86. }
  87. virtual double popularity() = 0;
  88. ~User();
  89. };
  90.  
  91. class FacebookUser : public User {
  92. private:
  93. int friends;
  94. int likes;
  95. int comments;
  96. static double like_koef;
  97. static double comment_koef;
  98. public:
  99. FacebookUser(const char *u="", const char *p="", const char *e="", int fr=0, int li=0, int co=0) : User(u,p,e)
  100. {
  101. friends = fr;
  102. likes = li;
  103. comments = co;
  104. }
  105. double popularity()
  106. {
  107. return friends + like_koef*likes + comment_koef*comments;
  108. }
  109. };
  110.  
  111.  
  112. class TwitterUser : public User{
  113. private:
  114. int followers;
  115. int tweets;
  116. static double tweet_koef;
  117. public:
  118. TwitterUser(const char *u="", const char *p="", const char *e="", int fol=0, int twe=0) : User(u,p,e)
  119. {
  120. followers = fol;
  121. tweets = twe;
  122. }
  123. double popularity()
  124. {
  125. return tweets*tweet_koef + followers;
  126. }
  127.  
  128. };
  129.  
  130. class SocialNetwork{
  131. private:
  132. User **niza;
  133. int tekovni_korisnici;
  134. static int max;
  135. public:
  136. SocialNetwork()
  137. {
  138. niza = new User*[0];
  139. tekovni_korisnici=0;
  140. }
  141. SocialNetwork &operator+=(User *u)
  142. {
  143. if (tekovni_korisnici==max)
  144. throw new MaximumSizeLimit(max);
  145.  
  146. else
  147. {
  148. User **tmp = new User*[tekovni_korisnici+1];
  149. for (int i=0; i<tekovni_korisnici; i++)
  150. {
  151. tmp[i] = niza[i];
  152. }
  153. tmp[tekovni_korisnici++] = u;
  154.  
  155. /*for (int i=0; i<tekovni_korisnici-1; i++)
  156. {
  157. delete [] niza[i];
  158. }
  159. delete [] niza;*/
  160. niza = tmp;
  161. }
  162. return *this;
  163. }
  164. double avgPopularity()
  165. {
  166. double vkupno=0;
  167. for (int i=0; i<tekovni_korisnici; i++)
  168. {
  169. vkupno+=niza[i]->popularity();
  170. }
  171. return vkupno/tekovni_korisnici;
  172. }
  173.  
  174. static void changeMaximumSize(int br)
  175. {
  176.  
  177. max=br;
  178. //cout<<"CHANGED to "<<max<<endl;
  179. }
  180. ~SocialNetwork() { delete [] niza; }
  181. };
  182.  
  183. double FacebookUser::like_koef=0.1;
  184. double FacebookUser::comment_koef=0.5;
  185. double TwitterUser::tweet_koef=0.5;
  186. int SocialNetwork::max=5;
  187.  
  188.  
  189. int main() {
  190.  
  191. SocialNetwork network = SocialNetwork();
  192. int n;
  193. cin >> n;
  194. char username[50];
  195. char password[50];
  196. char email[50];
  197. int userType;
  198. for (int i=0; i < n; ++i) {
  199. cin >> username;
  200. cin >> password;
  201. cin >> email;
  202. cin >> userType;
  203. if (userType == 1) {
  204. int friends;
  205. int likes;
  206. int comments;
  207. cin >> friends >> likes >> comments;
  208.  
  209. // TODO: Try-catch
  210. try
  211. {
  212. User * u = new FacebookUser(username,password,email,friends,likes,comments);
  213. network += u;
  214. }
  215. catch(InvalidPassword *ip) { ip->print(); }
  216. catch(InvalidEmail *ie) { ie->print(); }
  217. catch(MaximumSizeLimit *isl) { isl->print(); }
  218.  
  219.  
  220.  
  221. }
  222. else {
  223. int followers;
  224. int tweets;
  225. cin >> followers >> tweets;
  226.  
  227. // TODO: Try-catch
  228. try
  229. {
  230. User * u= new TwitterUser(username,password,email,followers,tweets);
  231. network += u;
  232. }
  233. catch(InvalidPassword *ip) { ip->print(); }
  234. catch(InvalidEmail *ie) { ie->print(); }
  235. catch(MaximumSizeLimit *isl) { isl->print(); }
  236.  
  237. }
  238.  
  239. }
  240. network.changeMaximumSize(6);
  241. cin >> username;
  242. cin >> password;
  243. cin >> email;
  244. int followers;
  245. int tweets;
  246. cin >> followers >> tweets;
  247.  
  248. // TODO: Try-catch
  249. try
  250. {
  251. User * u= new TwitterUser(username,password,email,followers,tweets);
  252. network += u;
  253. }
  254. catch(InvalidPassword *ip) { ip->print(); }
  255. catch(InvalidEmail *ie) { ie->print(); }
  256. catch(MaximumSizeLimit *isl) { isl->print(); }
  257.  
  258. cout << network.avgPopularity();
  259.  
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement