Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.12 KB | None | 0 0
  1.  
  2. // ******************************* VERY IMPORTANT *************************************************
  3. // THE FOLLOWING DECLARATIONS SHOULD NOT BE DELETED OR CHANGED
  4. // REMOVING OR CHANGING ANY OF THEM WILL STOP YOUR PROGRAM FROM
  5. // COMPILING SUCCESSFULLY WITH THE TEST HARNESS
  6. // PLEASE USE THEM FOR THE PURPOSE FOR WHICH THEY WERE CREATED
  7. //*************************************************************************************************
  8.  
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include<time.h>
  13. #include "news.h"
  14.  
  15. int NumberAccounts; // the actual number of accounts in the social network
  16. int AccountIdCounter; // a counter for creating unique account IDs
  17.  
  18.  
  19. char Consonants[NumberConsonants] = {'B', 'C', 'D' , 'F' , 'G' , 'J' , 'K' , 'L', 'M' , 'N', 'P', 'R', 'S', 'T' , 'V' ,'Z' };
  20. char Vowels[NumberVowels] = {'A', 'I', 'O', 'U' };
  21. char* TrueStoryWords[NumberTrueStoryWords] = {"ELECTIONS" , "BREXIT" , "UK" , "MINISTER" , "DATE", "EDUCATION" , "RESIGN", "MAYOR", "NEWS" , "WIN"};
  22.  
  23. Account* AllAccounts[MaxNumberAccounts]; // the array of all accounts in the network
  24. Server theServer; // this is the server that receives messages from senders and transmit them forward to recipients
  25.  
  26. int MaxNewFakeStory; // the maximum number of times fabricators can create new fake stories
  27. int MaxNewRealStory; // the maximum number of times real news publishers can create new real stories
  28. int numberNewFakeStories; // the number of fake news stories fabricated so far
  29. int numberNewRealStories; // the number of new real stories published so far
  30.  
  31. // Inititialise all variables to their initial values
  32. int initAll ()
  33. {
  34. NumberAccounts = 0;
  35. AccountIdCounter = 1;
  36. theServer.numberPendingMessages =0;
  37. theServer.numberReportedFakeSt = 0;
  38. MaxNewFakeStory = 5;
  39. MaxNewRealStory = 5;
  40. numberNewFakeStories = 0;
  41. numberNewRealStories = 0;
  42. srand ((unsigned int) time(0)); // seed the random number generator with the current time
  43. return 1;
  44. }
  45.  
  46. // returns a new account id
  47. // first time returns 1
  48. // if no more IDs can be generated returns -1
  49. int newAccountId ()
  50. {
  51. if (AccountIdCounter < MaxAccountId)
  52. return AccountIdCounter++;
  53. return -1;
  54. }
  55.  
  56. // returns a random consonant from the Consonants array
  57. // my implementation is 2 lines
  58. char aConsonant ()
  59. {
  60. int index = (rand() % NumberConsonants);
  61. return Consonants[index];
  62. }
  63.  
  64. // returns a random vowel from the Vowels array
  65. // my implementation is 2 lines
  66. char aVowel ()
  67. {
  68. int index = (rand() % NumberVowels);
  69. return Vowels[index];
  70. }
  71.  
  72. // returns 1 if the letter is a consonant, i.e. is one of the letters in the Consonants array
  73. // otherwise returns 0
  74. // my implementation is 4 lines
  75. int isConsonant (char letter)
  76. {
  77. for (int i = 0; i < NumberConsonants; i++)
  78. if (Consonants[i] == letter)
  79. return 1;
  80. return 0;
  81. }
  82.  
  83. // returns 1 if the letter is a vowel, i.e. is one of the letters in the Vowels array
  84. // otherwise returns 0
  85. // my implementation is 4 lines
  86. int isVowel (char letter)
  87. {
  88. for (int i = 0; i < NumberVowels; i++)
  89. if (Vowels[i] == letter)
  90. return 1;
  91. return 0;
  92. }
  93.  
  94. // creates a new account name (i.e. a new string initialised to the name)
  95. // returns a pointer to this string
  96. // account names are exactly four letters long and have the following pattern CVCV
  97. // where C is any consonant from the Consonant array, and V is any vowel from the Vowels array
  98. // examples of valid account names TITO, SAMI, KILO, LOVE, NOBU ...
  99. // examples of invalid account names KLMI (second letter is not a vowel), KUKUA (more than 4 letters), LAL (less than 4 letters)
  100. // my implementation is 7 lines
  101. char* newAccountName ()
  102. {
  103. char *newName;
  104. int size = 4;
  105. newName = (char *)malloc(sizeof(char)*size);
  106. *(newName+0) = aConsonant();
  107. *(newName+1) = aVowel();
  108. *(newName+2) = aConsonant();
  109. *(newName+3) = aVowel();
  110. return newName;
  111.  
  112. }
  113.  
  114. // creates a new account 'object' in the heap and initialises all its fields to the correct values
  115. // returns a pointer to the newly created account
  116. // the account type is an integer between 1-5 corresponding to the five account types:
  117. // my implementation is 8 lines
  118. Account* newAccount (int account_number, char accout_name[], int account_type)
  119. {
  120. Account *newAcc = (Account*)malloc(sizeof(Account));
  121. newAcc->accId = account_number;
  122. strcpy(newAcc->accName, accout_name);
  123. newAcc->accType = account_type;
  124. return newAcc;
  125. }
  126.  
  127. // adds an account (more precisely a pointer to the account) to the AllAccounts array
  128. // if the array is full and cannot accept any more accounts the function returns -1
  129. // otherwise the function returns 1
  130. // my implementation is 5 lines
  131. int addAccount (Account* an_account)
  132. {
  133. if (NumberAccounts == (MaxNumberAccounts - 1 )) return -1;
  134. AllAccounts[NumberAccounts] = an_account;
  135. NumberAccounts++;
  136. return 1;
  137. }
  138.  
  139.  
  140. // Makes an account (the first parameter) friend with another account (the second parameter)
  141. // notice that friendship is mutual hence if LILI is friend with TALA then TALA is also friend with LILI
  142. // if either account cannot accept any more friends (because the Friends array is full) the function returns -1
  143. // otherwise the function returns 1
  144. // my implementation is 7 lines
  145. int addFriend (Account* an_account, Account* a_friend)
  146. {
  147. if ((an_account->numFriends == MaxFriends -1) || (a_friend->numFriends == MaxFriends -1)) return -1;
  148. an_account->Friends[an_account->numFriends] = a_friend;
  149. a_friend->Friends[a_friend->numFriends] = an_account;
  150. an_account->numFriends++;
  151. a_friend->numFriends++;
  152. return 1;
  153. }
  154.  
  155.  
  156. // returns 1 (true) if account a is friend with account b otherwise it returns 0 (false)
  157. // my implementation is 4 lines
  158. int isFriend (Account *a , Account *b)
  159. {
  160. for (int i =0; i < a->numFriends ;i++)
  161. if (a->Friends[i] == b) return 1;
  162. return 0;
  163. }
  164.  
  165. // create a social network having
  166. // num_publishers real news publishers
  167. // num_fabricators fake news fabricators
  168. // num_forwarders naive forwarders
  169. // num_sinks fake story sinks
  170. // num_reporters fake news reporters (people who report fake news to the server)
  171. // each account then should be made a friend with another num_friends friends
  172. // the friends are randomly picked
  173. // make sure an account is not made a friend with itself, or made a friend with another account more than once
  174. // my implementation is 44 lines
  175. int createSocialNetwork (int num_publishers, int num_fabricators, int num_forwarders, int num_sinks, int num_reporters, int num_friends)
  176. {
  177. for (int i = 0; i < num_publishers - 1; i++)
  178. {
  179. newAcc* = newAccount(newAccountId(),newAccountName(),1);
  180. newAcc->numFriends = num_friends;
  181. newAcc->
  182. }
  183. NumberAccounts++;
  184. return -99;
  185. }
  186.  
  187. // creates a new fake story and returns a pointer to the newly created string
  188. // a fake story is comprised of execty two 3-letter words separated by single white space and has the following pattern CVC CVC, for example RIS PUZ or DAR MUC
  189. // where C is one of the consonants in the Consonants array and V is one of the vowels in the Vowels array
  190. // my implementation is 10 lines
  191. char* aFakeStory ()
  192. {
  193. return NULL;
  194. }
  195.  
  196. // returns 1 if story is a fake story
  197. // otherwise retunrs 0
  198. // my implementation is 10 lines
  199. int isFakeStory (char* story)
  200. {
  201. return -99;
  202. }
  203.  
  204. // creates a new real story.
  205. // A real story is comprised of two DIFFERENT words randomly taken from the TrueStoryWords array
  206. // Hence, if the first randomly picked word is MAYOR the second one cannot be MAYOR
  207. // Exmaples of correctly formed true stories: BREXIT UK, MINISTER DATE, UK WIN, UK MINISTER, DATE WIN, ...
  208. // Exmaples of incorrectly formed stories include: DATE DATE (same word), BREXIT (one word), UK WIN MAYOR (more than 2 words)
  209. // the function returns a pointer to the newly created string
  210. // my implementation is 9 lines
  211. char* aRealStory ()
  212. {
  213. return NULL;
  214. }
  215.  
  216.  
  217. // create a new message to encapsulate a story to be sent from sender to recipient
  218. // and returns the newly created message
  219. // my implementation is 6 lines
  220. Message createMessage (char* a_story, Account* sender, Account* recipient)
  221. {
  222.  
  223. }
  224.  
  225. // upload a message to the server, i.e. add the message to the server's pendingMessages array
  226. // returns -1 if the pendingMessages array is full
  227. // otherwise returns 1
  228. // my implementation is 5 lines
  229. int uploadMessage (Message a_message)
  230. {
  231. return -99;
  232. }
  233.  
  234. // push a message to the recipient's inbox, i.e. add the message to the recipient's receivedM array
  235. // returns -1 if the recipient's receivedM array is full
  236. // otherwise returns 1
  237. // my implementation is 5 lines
  238. int pushMessage (Message a_message)
  239. {
  240. return -99;
  241. }
  242.  
  243. // send a story from sender to recipient by:
  244. // 1. creaing a message for the story
  245. // 2. uploading the message to the server
  246. // 3. adding the message to the sentM array of the sender so that the sender knows that it has been sent
  247. // returns -1 if something goes wrong, e.g. upload message to server failed or the sender's sentM array is full
  248. // otherwise returns 1
  249. // my implementation is 9 lines
  250. int sendStory (char* a_story, Account* sender, Account* recipient)
  251. {
  252. return -99;
  253. }
  254.  
  255. // returns 1 (true) if the story has been reported, i.e. it exists in the server's reportedFakeStories array
  256. // otherwise it returns 0
  257. // my implementation is 4 lines
  258. int isReported (char * story)
  259. {
  260. return -99;
  261. }
  262.  
  263.  
  264. // report a fake story, i.e. add the story to the server's reportedFakeStories array
  265. // if the story has already been reported (it does exist in the reportedFakeStories array) the function should not
  266. // add it again
  267. // returns -1 if the reportedFakeStories array is full
  268. // otherwise it returns 1
  269. // my implementation is 8 lines
  270. int ReportFakeStory(char* story)
  271. {
  272. return -99;
  273. }
  274.  
  275. // transmit all the messages in the server's pendingMessages array to their respetive recipient's
  276. // by adding them to the recipient's inbox
  277. // if a message has been reported as fake (it exists in the reportedFakeStories array), it should not be transmitted
  278. // my implementation is 9 lines
  279. int transmitAllMessages ()
  280. {
  281. return -99;
  282. }
  283.  
  284. // seraches for a story in the account's sentM array
  285. // returns 1 if found
  286. // 0 if not found
  287. // my implementation is 4 lines
  288. int isSent (Account* account, char* story)
  289. {
  290. return -99;
  291. }
  292.  
  293.  
  294. // for RealNewsPublisher and FakeNewsFabricator, this function allows the account to create a new story
  295. // (unless the maximum number of new stories is reach) and send the story to all friends
  296. // for all other account types the function will
  297. // go through all messages in the inbox of account and for each message that has not been read:
  298. // set the isRead flag is 1
  299. // decide if the story must be forwarded to all friends or not (the decision depends on the account type)
  300. // decide if the story must be reportd as fake (the decision depends on the account type) and report it
  301. // note that if a story has already been sent to friends (because it exists in the sentM array) it should not be sent again
  302. // the function should return the number of sent messages
  303. // my implementation is 55 lines
  304. int originateOrforwardNews (Account* account)
  305. {
  306. return -99;
  307. }
  308.  
  309. // this is the function that simulates the propagation of news throughout the network
  310. // the pseudocode of the function is in the problem description
  311. // always returns 1
  312. // my implementation is 13 lines
  313. int simulate ()
  314. {
  315. return -99;
  316. }
  317.  
  318. // The following functions have already been implmented
  319. // you may wish
  320.  
  321. // this function counts the number of fake news stories in every inbox of every account in the newtwork
  322. int CountFakeNews ()
  323. {
  324. int countfake = 0;
  325. for (int i = 0; i < NumberAccounts ; i++)
  326. for (int j = 0; j < AllAccounts[i]->numberReceivedM; j++)
  327. if (isFakeStory( AllAccounts[i]->receivedM[j].theStory))
  328. countfake++;
  329. return countfake;
  330. }
  331.  
  332. // this function counts the number of real news stories in every inbox of every account in the newtwork
  333. int CountRealNews ()
  334. {
  335. int count = 0;
  336. for (int i = 0; i < NumberAccounts ; i++)
  337. for (int j = 0; j < AllAccounts[i]->numberReceivedM; j++)
  338. if (isFakeStory( AllAccounts[i]->receivedM[j].theStory) == 0)
  339. count++;
  340. return count;
  341. }
  342.  
  343.  
  344. // displays all the accounts on the terminal
  345. // always returns 1
  346. int lisAllAccounts ()
  347. {
  348. printf ("ID\tName\tClass.\tNo.Friends\tFriends\n");
  349. for (int i=0; i < NumberAccounts ; i++)
  350. {
  351. printf ("%i\t", AllAccounts[i]->accId);
  352. printf ("%s\t", AllAccounts[i]->accName);
  353. printf ("%i\t", AllAccounts[i]->accType);
  354. printf ("%i\t", AllAccounts[i]->numFriends);
  355. for (int j =0; j <AllAccounts[i]->numFriends ; j++)
  356. printf ("%s,", AllAccounts[i]->Friends[j]->accName);
  357. printf ("\n");
  358. }
  359. return 1;
  360. }
  361.  
  362. // displays the social network as a friendhsip matrix
  363. // always returns 1
  364. int showFriendshipMatrix()
  365. {
  366. printf ("The friendship matrix\n");
  367. printf (" ");
  368. for (int i=0; i < NumberAccounts ; i++)
  369. {
  370. Account* this_account = AllAccounts[i];
  371. printf ("%s ", this_account->accName);
  372. }
  373. printf ("\n");
  374. for (int i=0; i < NumberAccounts ; i++)
  375. {
  376. Account* this_account = AllAccounts[i];
  377. printf ("%s ", this_account->accName);
  378. for (int f =0 ; f < NumberAccounts ; f++)
  379. {
  380. if (isFriend (this_account , AllAccounts[f]))
  381. printf (" f ");
  382. else
  383. printf (" n ");
  384. }
  385. printf ("\n");
  386. }
  387. return 1;
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement