Advertisement
Guest User

Untitled

a guest
Sep 4th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. #include <iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. class Exception
  6. {
  7.  
  8. protected:
  9.  
  10. char *msg;
  11.  
  12. public:
  13.  
  14. Exception(char* msg="")
  15. {
  16. this->msg=new char[strlen(msg)];
  17. strcpy(this->msg,msg);
  18. }
  19.  
  20.  
  21. virtual void message()
  22. {
  23. cout<<msg<<endl;
  24. }
  25.  
  26.  
  27. };
  28.  
  29.  
  30. class InvalidPassword:public Exception
  31. {
  32.  
  33. public:
  34.  
  35. InvalidPassword(char* msg=""):Exception(msg){}
  36.  
  37.  
  38. } ;
  39.  
  40. class InvalidEmail:public Exception
  41. {
  42.  
  43. public:
  44.  
  45. InvalidEmail(char* msg=""):Exception(msg){}
  46.  
  47.  
  48.  
  49. };
  50.  
  51. class MaximumSizeLimit:public Exception
  52. {
  53.  
  54. protected:
  55.  
  56. int maxii;
  57.  
  58. public:
  59.  
  60. MaximumSizeLimit(char* msg="",int maxii=0)
  61. {
  62. this->maxii=maxii;
  63. }
  64.  
  65.  
  66. void message()
  67. {
  68. cout<<"You can't add more than "<<maxii<<" users"<<endl;
  69. }
  70.  
  71.  
  72.  
  73. };
  74.  
  75. class User
  76. {
  77.  
  78. protected:
  79.  
  80. char username[50];
  81. char password[50];
  82. char email[50];
  83.  
  84.  
  85. public:
  86.  
  87. virtual double popularity()=0;
  88.  
  89. User(char* username="",char* password="",char* email="")
  90. {
  91. bool upper=false;
  92. bool lower=false;
  93. bool digit=false;
  94.  
  95. for(int i=0;i<strlen(password);i++)
  96. {
  97.  
  98. if(isupper(password[i]))
  99. upper=true;
  100. if(islower(password[i]))
  101. lower=true;
  102. if(isdigit(password[i]))
  103. digit=true;
  104. }
  105.  
  106. if(!upper || !lower || !digit)
  107. throw InvalidPassword("Password is too weak");
  108.  
  109. int counter=0;
  110. for(int i=0;i<strlen(email);i++)
  111. {
  112. if(email[i]=='@')
  113. counter++;
  114.  
  115. }
  116. if(counter!=1)
  117. throw InvalidEmail("Mail is not valid");
  118.  
  119. strcpy(this->username,username);
  120. strcpy(this->password,password);
  121. strcpy(this->email,email);
  122. }
  123.  
  124.  
  125.  
  126.  
  127. };
  128.  
  129. class FacebookUser:public User
  130. {
  131.  
  132. protected:
  133.  
  134. int friends;
  135. int likes;
  136. int comments;
  137. static double like;
  138. static double comment;
  139.  
  140. public:
  141.  
  142. FacebookUser(char* username="",char* password="",char* email="",int friends=0,int likes=0,int comments=0):User(username,password,email)
  143. {
  144. this->friends=friends;
  145. this->likes=likes;
  146. this->comments=comments;
  147. }
  148.  
  149.  
  150. double popularity()
  151. {
  152. return friends+comments*comment+likes*like;
  153. }
  154.  
  155. };
  156.  
  157. class TwitterUser:public User
  158. {
  159. protected:
  160.  
  161. int followers;
  162. int tweets;
  163. static double tweet;
  164.  
  165. public:
  166.  
  167. TwitterUser(char* username="",char* password="",char* email="",int followers=0,int tweets=0):User(username,password,email)
  168. {
  169. this->followers=followers;
  170. this->tweets=tweets;
  171. }
  172.  
  173.  
  174. double popularity()
  175. {
  176. return followers+tweets*tweet;
  177. }
  178.  
  179. };
  180.  
  181. class SocialNetwork
  182. {
  183. protected:
  184.  
  185. User **users;
  186. int counter;
  187. static int maxi;
  188.  
  189. public:
  190.  
  191. SocialNetwork()
  192. {
  193. counter=0;
  194. users=new User*[counter];
  195. }
  196.  
  197. SocialNetwork& operator+=(User *u)
  198.  
  199. {
  200. if(counter + 1 > maxi)
  201. throw MaximumSizeLimit(maxi);
  202.  
  203. User **temp = new User*[counter+1];
  204. for(int i = 0; i < counter; i++)
  205. temp[i] = users[i];
  206.  
  207. delete []users;
  208. users = temp;
  209. users[counter++] = u;
  210.  
  211.  
  212. return *this;
  213. }
  214.  
  215. float avgPopularity()
  216. {
  217. int suma=0;
  218. for(int i=0;i<counter;i++)
  219. {
  220. suma+=users[i]->popularity();
  221. }
  222.  
  223. return suma/counter;
  224. }
  225.  
  226. static void changeMaximumSize(int number)
  227. {
  228. maxi=number;
  229. }
  230.  
  231. ~SocialNetwork()
  232. {
  233. for(int i=0;i<counter;i++)
  234. delete users[i];
  235. delete[]users;
  236. }
  237.  
  238.  
  239.  
  240. };
  241.  
  242.  
  243. double FacebookUser::like=0.1;
  244. double FacebookUser::comment=0.5;
  245. double TwitterUser::tweet=0.5;
  246. int SocialNetwork::maxi=5;
  247.  
  248.  
  249.  
  250. int main() {
  251.  
  252. SocialNetwork network = SocialNetwork();
  253. int n;
  254. cin >> n;
  255. char username[50];
  256. char password[50];
  257. char email[50];
  258. int userType;
  259. for (int i=0; i < n; ++i) {
  260. cin >> username;
  261. cin >> password;
  262. cin >> email;
  263. cin >> userType;
  264. if (userType == 1) {
  265. int friends;
  266. int likes;
  267. int comments;
  268. cin >> friends >> likes >> comments;
  269.  
  270. // TODO: Try-catch
  271. try
  272. {
  273. User * u = new FacebookUser(username,password,email,friends,likes,comments);
  274. network += u;
  275. }
  276.  
  277. catch(InvalidPassword &i)
  278. {
  279. i.message();
  280. }
  281.  
  282. catch(InvalidEmail &e)
  283. {
  284. e.message();
  285. }
  286.  
  287. catch(MaximumSizeLimit &m)
  288. {
  289. m.message();
  290. }
  291.  
  292.  
  293. }
  294. else {
  295. int followers;
  296. int tweets;
  297. cin >> followers >> tweets;
  298.  
  299. // TODO: Try-catch
  300. try
  301. {
  302. User * u= new TwitterUser(username,password,email,followers,tweets);
  303. network += u;
  304.  
  305.  
  306. }
  307.  
  308. catch(InvalidPassword &i)
  309. {
  310. i.message();
  311. }
  312.  
  313. catch(InvalidEmail &e)
  314. {
  315. e.message();
  316. }
  317.  
  318. catch(MaximumSizeLimit &m)
  319. {
  320. m.message();
  321. }
  322.  
  323.  
  324.  
  325. }
  326.  
  327. }
  328. network.changeMaximumSize(6);
  329. cin >> username;
  330. cin >> password;
  331. cin >> email;
  332. int followers;
  333. int tweets;
  334. cin >> followers >> tweets;
  335.  
  336. // TODO: Try-catch
  337. try
  338. {
  339.  
  340. User * u= new TwitterUser(username,password,email,followers,tweets);
  341. network += u;
  342. }
  343. catch(InvalidPassword &i)
  344. {
  345. i.message();
  346. }
  347.  
  348. catch(InvalidEmail &e)
  349. {
  350. e.message();
  351. }
  352.  
  353. catch(MaximumSizeLimit &m)
  354. {
  355. m.message();
  356. }
  357. cout << network.avgPopularity();
  358.  
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement