Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.38 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. Account* newAcc = newAccount(newAccountId(),newAccountName(),1);
  180. addAccount(newAcc);
  181. }
  182. for (int i = 0; i < num_fabricators - 1; i++)
  183. {
  184. Account* newAcc = newAccount(newAccountId(),newAccountName(),2);
  185. addAccount(newAcc);
  186. }
  187. for (int i = 0; i < num_forwarders - 1; i++)
  188. {
  189. Account* newAcc = newAccount(newAccountId(),newAccountName(),3);
  190. addAccount(newAcc);
  191. }
  192. for (int i = 0; i < num_sinks - 1; i++)
  193. {
  194. Account* newAcc = newAccount(newAccountId(),newAccountName(),4);
  195. addAccount(newAcc);
  196. }
  197. for (int i = 0; i < num_reporters - 1; i++)
  198. {
  199. Account* newAcc = newAccount(newAccountId(),newAccountName(),5);
  200. addAccount(newAcc);
  201. }
  202. for (int i = 0; i < NumberAccounts - 1; i++)
  203. {
  204. int randAcc = (rand() % NumberAccounts);
  205. AllAccounts[i]->numFriends = num_friends;
  206. AllAccounts[randAcc]->numFriends = num_friends;
  207. for (int a = 0; a < num_friends - 1; a++)
  208. {
  209. if (i != randAcc)
  210. {
  211. if (isFriend(AllAccounts[i], AllAccounts[randAcc]) == 0)
  212. addFriend(AllAccounts[i], AllAccounts[randAcc]);
  213. }
  214. }
  215. }
  216.  
  217. }
  218.  
  219. // creates a new fake story and returns a pointer to the newly created string
  220. // 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
  221. // where C is one of the consonants in the Consonants array and V is one of the vowels in the Vowels array
  222. // my implementation is 10 lines
  223. char* aFakeStory ()
  224. {
  225. char *newFakeStory;
  226. int size = 7;
  227. newFakeStory = (char *)malloc(sizeof(char)*size);
  228. *(newFakeStory+0) = aConsonant();
  229. *(newFakeStory+1) = aVowel();
  230. *(newFakeStory+2) = aConsonant();
  231. *(newFakeStory+3) = ' ';
  232. *(newFakeStory+4) = aConsonant();
  233. *(newFakeStory+5) = aVowel();
  234. *(newFakeStory+6) = aConsonant();
  235. return newFakeStory;
  236. }
  237.  
  238. // returns 1 if story is a fake story
  239. // otherwise retunrs 0
  240. // my implementation is 10 lines
  241. int isFakeStory (char* story)
  242. {
  243.  
  244. if (isConsonant(*(story+0)))
  245. {
  246. if (isVowel(*(story+1)))
  247. {
  248. if (isConsonant(*(story+2)))
  249. {
  250. if (*(story+3) == ' ')
  251. {
  252. if (isConsonant(*(story+4)))
  253. {
  254. if (isVowel(*(story+5)))
  255. {
  256. if (isConsonant(*(story+6)))
  257. return 1;
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. return 0;
  265. }
  266.  
  267. // creates a new real story.
  268. // A real story is comprised of two DIFFERENT words randomly taken from the TrueStoryWords array
  269. // Hence, if the first randomly picked word is MAYOR the second one cannot be MAYOR
  270. // Exmaples of correctly formed true stories: BREXIT UK, MINISTER DATE, UK WIN, UK MINISTER, DATE WIN, ...
  271. // Exmaples of incorrectly formed stories include: DATE DATE (same word), BREXIT (one word), UK WIN MAYOR (more than 2 words)
  272. // the function returns a pointer to the newly created string
  273. // my implementation is 9 lines
  274. char* aRealStory ()
  275. {
  276. /*int rand1 = (rand() % (NumberTrueStoryWords-1));
  277. int rand2;
  278. do rand2 = (rand() % (NumberTrueStoryWords-1))
  279. while (rand1 != rand2);
  280. char *newRealStory;
  281. int size = NumberTrueStoryWords;
  282. newRealStory = (char *)malloc(sizeof(char)*size);
  283. for (i = 0; i < sizeof(TrueStoryWords[rand1]) - 1; i++)
  284. *(newRealStory+i) = *()*/
  285. return NULL;
  286.  
  287. }
  288.  
  289.  
  290. // create a new message to encapsulate a story to be sent from sender to recipient
  291. // and returns the newly created message
  292. // my implementation is 6 lines
  293. Message createMessage (char* a_story, Account* sender, Account* recipient)
  294. {
  295. Message* newMessage = (Message *)malloc(sizeof(Message));
  296. strcpy(newMessage->theStory, a_story);
  297. newMessage->theSender = sender;
  298. newMessage->theRecipient = recipient;
  299. return *newMessage;
  300. }
  301.  
  302. // upload a message to the server, i.e. add the message to the server's pendingMessages array
  303. // returns -1 if the pendingMessages array is full
  304. // otherwise returns 1
  305. // my implementation is 5 lines
  306. int uploadMessage (Message a_message)
  307. {
  308. if (theServer.numberPendingMessages == MaxNumberPendingMessages - 1)
  309. return -1;
  310. theServer.pendingMessages[theServer.numberPendingMessages] = a_message;
  311. (theServer.numberPendingMessages)++;
  312. return 1;
  313. }
  314.  
  315. // push a message to the recipient's inbox, i.e. add the message to the recipient's receivedM array
  316. // returns -1 if the recipient's receivedM array is full
  317. // otherwise returns 1
  318. // my implementation is 5 lines
  319. int pushMessage (Message a_message)
  320. {
  321. if (a_message.theRecipient->numberReceivedM == MaxMessageArraySize - 1)
  322. return -1;
  323. a_message.theRecipient->receivedM[a_message.theRecipient->numberReceivedM] = a_message;
  324. (a_message.theRecipient->numberReceivedM)++;
  325. return 1;
  326. }
  327.  
  328. // send a story from sender to recipient by:
  329. // 1. creaing a message for the story
  330. // 2. uploading the message to the server
  331. // 3. adding the message to the sentM array of the sender so that the sender knows that it has been sent
  332. // returns -1 if something goes wrong, e.g. upload message to server failed or the sender's sentM array is full
  333. // otherwise returns 1
  334. // my implementation is 9 lines
  335. int sendStory (char* a_story, Account* sender, Account* recipient)
  336. {
  337. Message newMessage = createMessage(a_story, sender, recipient);
  338. if (theServer.numberPendingMessages == MaxNumberPendingMessages - 1)
  339. return -1;
  340. if (sender->numberSentM == MaxMessageArraySize - 1)
  341. return -1;
  342. uploadMessage(newMessage);
  343. sender->sentM[sender->numberSentM] = newMessage;
  344. (sender->numberSentM)++;
  345. return 1;
  346. }
  347.  
  348. // returns 1 (true) if the story has been reported, i.e. it exists in the server's reportedFakeStories array
  349. // otherwise it returns 0
  350. // my implementation is 4 lines
  351. int isReported (char * story)
  352. {
  353. /*
  354. for (int i = 0; i < theServer.numberReportedFakeSt - 1; i++)
  355. printf(theServer.reportedFakeStories[i]);
  356. //if (strcmp(theServer.reportedFakeStories[i], story) == 0)
  357. return 1;
  358. for (int i = 0; i < 10; i++)
  359. printf(theServer.reportedFakeStories[i]);
  360. return 0;*/
  361. }
  362.  
  363.  
  364. // report a fake story, i.e. add the story to the server's reportedFakeStories array
  365. // if the story has already been reported (it does exist in the reportedFakeStories array) the function should not
  366. // add it again
  367. // returns -1 if the reportedFakeStories array is full
  368. // otherwise it returns 1
  369. // my implementation is 8 lines
  370. int ReportFakeStory(char* story)
  371. {
  372. if (isReported(story) == 1)
  373. return 1;
  374. if (theServer.numberReportedFakeSt == 999)
  375. return -1;
  376. theServer.reportedFakeStories[theServer.numberReportedFakeSt] = story;
  377. (theServer.numberReportedFakeSt)++;
  378. }
  379.  
  380. // transmit all the messages in the server's pendingMessages array to their respetive recipient's
  381. // by adding them to the recipient's inbox
  382. // if a message has been reported as fake (it exists in the reportedFakeStories array), it should not be transmitted
  383. // my implementation is 9 lines
  384. int transmitAllMessages ()
  385. {
  386. return -99;
  387. }
  388.  
  389. // seraches for a story in the account's sentM array
  390. // returns 1 if found
  391. // 0 if not found
  392. // my implementation is 4 lines
  393. int isSent (Account* account, char* story)
  394. {
  395. return -99;
  396. }
  397.  
  398.  
  399. // for RealNewsPublisher and FakeNewsFabricator, this function allows the account to create a new story
  400. // (unless the maximum number of new stories is reach) and send the story to all friends
  401. // for all other account types the function will
  402. // go through all messages in the inbox of account and for each message that has not been read:
  403. // set the isRead flag is 1
  404. // decide if the story must be forwarded to all friends or not (the decision depends on the account type)
  405. // decide if the story must be reportd as fake (the decision depends on the account type) and report it
  406. // note that if a story has already been sent to friends (because it exists in the sentM array) it should not be sent again
  407. // the function should return the number of sent messages
  408. // my implementation is 55 lines
  409. int originateOrforwardNews (Account* account)
  410. {
  411. return -99;
  412. }
  413.  
  414. // this is the function that simulates the propagation of news throughout the network
  415. // the pseudocode of the function is in the problem description
  416. // always returns 1
  417. // my implementation is 13 lines
  418. int simulate ()
  419. {
  420. return -99;
  421. }
  422.  
  423. // The following functions have already been implmented
  424. // you may wish
  425.  
  426. // this function counts the number of fake news stories in every inbox of every account in the newtwork
  427. int CountFakeNews ()
  428. {
  429. int countfake = 0;
  430. for (int i = 0; i < NumberAccounts ; i++)
  431. for (int j = 0; j < AllAccounts[i]->numberReceivedM; j++)
  432. if (isFakeStory( AllAccounts[i]->receivedM[j].theStory))
  433. countfake++;
  434. return countfake;
  435. }
  436.  
  437. // this function counts the number of real news stories in every inbox of every account in the newtwork
  438. int CountRealNews ()
  439. {
  440. int count = 0;
  441. for (int i = 0; i < NumberAccounts ; i++)
  442. for (int j = 0; j < AllAccounts[i]->numberReceivedM; j++)
  443. if (isFakeStory( AllAccounts[i]->receivedM[j].theStory) == 0)
  444. count++;
  445. return count;
  446. }
  447.  
  448.  
  449. // displays all the accounts on the terminal
  450. // always returns 1
  451. int lisAllAccounts ()
  452. {
  453. printf ("ID\tName\tClass.\tNo.Friends\tFriends\n");
  454. for (int i=0; i < NumberAccounts ; i++)
  455. {
  456. printf ("%i\t", AllAccounts[i]->accId);
  457. printf ("%s\t", AllAccounts[i]->accName);
  458. printf ("%i\t", AllAccounts[i]->accType);
  459. printf ("%i\t", AllAccounts[i]->numFriends);
  460. for (int j =0; j <AllAccounts[i]->numFriends ; j++)
  461. printf ("%s,", AllAccounts[i]->Friends[j]->accName);
  462. printf ("\n");
  463. }
  464. return 1;
  465. }
  466.  
  467. // displays the social network as a friendhsip matrix
  468. // always returns 1
  469. int showFriendshipMatrix()
  470. {
  471. printf ("The friendship matrix\n");
  472. printf (" ");
  473. for (int i=0; i < NumberAccounts ; i++)
  474. {
  475. Account* this_account = AllAccounts[i];
  476. printf ("%s ", this_account->accName);
  477. }
  478. printf ("\n");
  479. for (int i=0; i < NumberAccounts ; i++)
  480. {
  481. Account* this_account = AllAccounts[i];
  482. printf ("%s ", this_account->accName);
  483. for (int f =0 ; f < NumberAccounts ; f++)
  484. {
  485. if (isFriend (this_account , AllAccounts[f]))
  486. printf (" f ");
  487. else
  488. printf (" n ");
  489. }
  490. printf ("\n");
  491. }
  492. return 1;
  493. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement