Advertisement
Guest User

SCNet

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