Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ******************************* VERY IMPORTANT *************************************************
- // THE FOLLOWING DECLARATIONS SHOULD NOT BE DELETED OR CHANGED
- // REMOVING OR CHANGING ANY OF THEM WILL STOP YOUR PROGRAM FROM
- // COMPILING SUCCESSFULLY WITH THE TEST HARNESS
- // PLEASE USE THEM FOR THE PURPOSE FOR WHICH THEY WERE CREATED
- //*************************************************************************************************
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include<time.h>
- #include "news.h"
- int NumberAccounts; // the actual number of accounts in the social network
- int AccountIdCounter; // a counter for creating unique account IDs
- char Consonants[NumberConsonants] = {'B', 'C', 'D' , 'F' , 'G' , 'J' , 'K' , 'L', 'M' , 'N', 'P', 'R', 'S', 'T' , 'V' ,'Z' };
- char Vowels[NumberVowels] = {'A', 'I', 'O', 'U' };
- char* TrueStoryWords[NumberTrueStoryWords] = {"ELECTIONS" , "BREXIT" , "UK" , "MINISTER" , "DATE", "EDUCATION" , "RESIGN", "MAYOR", "NEWS" , "WIN"};
- Account* AllAccounts[MaxNumberAccounts]; // the array of all accounts in the network
- Server theServer; // this is the server that receives messages from senders and transmit them forward to recipients
- int MaxNewFakeStory; // the maximum number of times fabricators can create new fake stories
- int MaxNewRealStory; // the maximum number of times real news publishers can create new real stories
- int numberNewFakeStories; // the number of fake news stories fabricated so far
- int numberNewRealStories; // the number of new real stories published so far
- // Inititialise all variables to their initial values
- int initAll ()
- {
- NumberAccounts = 0;
- AccountIdCounter = 1;
- theServer.numberPendingMessages =0;
- theServer.numberReportedFakeSt = 0;
- MaxNewFakeStory = 5;
- MaxNewRealStory = 5;
- numberNewFakeStories = 0;
- numberNewRealStories = 0;
- srand ((unsigned int) time(0)); // seed the random number generator with the current time
- return 1;
- }
- // returns a new account id
- // first time returns 1
- // if no more IDs can be generated returns -1
- int newAccountId ()
- {
- if (AccountIdCounter < MaxAccountId)
- return AccountIdCounter++;
- return -1;
- }
- // returns a random consonant from the Consonants array
- // my implementation is 2 lines
- char aConsonant ()
- {
- int index = (rand() % NumberConsonants);
- return Consonants[index];
- }
- // returns a random vowel from the Vowels array
- // my implementation is 2 lines
- char aVowel ()
- {
- int index = (rand() % NumberVowels);
- return Vowels[index];
- }
- // returns 1 if the letter is a consonant, i.e. is one of the letters in the Consonants array
- // otherwise returns 0
- // my implementation is 4 lines
- int isConsonant (char letter)
- {
- for (int i = 0; i < NumberConsonants; i++)
- if (Consonants[i] == letter)
- return 1;
- return 0;
- }
- // returns 1 if the letter is a vowel, i.e. is one of the letters in the Vowels array
- // otherwise returns 0
- // my implementation is 4 lines
- int isVowel (char letter)
- {
- for (int i = 0; i < NumberVowels; i++)
- if (Vowels[i] == letter)
- return 1;
- return 0;
- }
- // creates a new account name (i.e. a new string initialised to the name)
- // returns a pointer to this string
- // account names are exactly four letters long and have the following pattern CVCV
- // where C is any consonant from the Consonant array, and V is any vowel from the Vowels array
- // examples of valid account names TITO, SAMI, KILO, LOVE, NOBU ...
- // examples of invalid account names KLMI (second letter is not a vowel), KUKUA (more than 4 letters), LAL (less than 4 letters)
- // my implementation is 7 lines
- char* newAccountName ()
- {
- char *newName;
- int size = 4;
- newName = (char *)malloc(sizeof(char)*size);
- *(newName+0) = aConsonant();
- *(newName+1) = aVowel();
- *(newName+2) = aConsonant();
- *(newName+3) = aVowel();
- return newName;
- }
- // creates a new account 'object' in the heap and initialises all its fields to the correct values
- // returns a pointer to the newly created account
- // the account type is an integer between 1-5 corresponding to the five account types:
- // my implementation is 8 lines
- Account* newAccount (int account_number, char accout_name[], int account_type)
- {
- Account *newAcc = (Account*)malloc(sizeof(Account));
- newAcc->accId = account_number;
- strcpy(newAcc->accName, accout_name);
- newAcc->accType = account_type;
- return newAcc;
- }
- // adds an account (more precisely a pointer to the account) to the AllAccounts array
- // if the array is full and cannot accept any more accounts the function returns -1
- // otherwise the function returns 1
- // my implementation is 5 lines
- int addAccount (Account* an_account)
- {
- if (NumberAccounts == (MaxNumberAccounts - 1 )) return -1;
- AllAccounts[NumberAccounts] = an_account;
- NumberAccounts++;
- return 1;
- }
- // Makes an account (the first parameter) friend with another account (the second parameter)
- // notice that friendship is mutual hence if LILI is friend with TALA then TALA is also friend with LILI
- // if either account cannot accept any more friends (because the Friends array is full) the function returns -1
- // otherwise the function returns 1
- // my implementation is 7 lines
- int addFriend (Account* an_account, Account* a_friend)
- {
- if ((an_account->numFriends == MaxFriends -1) || (a_friend->numFriends == MaxFriends -1)) return -1;
- an_account->Friends[an_account->numFriends] = a_friend;
- a_friend->Friends[a_friend->numFriends] = an_account;
- an_account->numFriends++;
- a_friend->numFriends++;
- return 1;
- }
- // returns 1 (true) if account a is friend with account b otherwise it returns 0 (false)
- // my implementation is 4 lines
- int isFriend (Account *a , Account *b)
- {
- for (int i =0; i < a->numFriends ;i++)
- if (a->Friends[i] == b) return 1;
- return 0;
- }
- // create a social network having
- // num_publishers real news publishers
- // num_fabricators fake news fabricators
- // num_forwarders naive forwarders
- // num_sinks fake story sinks
- // num_reporters fake news reporters (people who report fake news to the server)
- // each account then should be made a friend with another num_friends friends
- // the friends are randomly picked
- // make sure an account is not made a friend with itself, or made a friend with another account more than once
- // my implementation is 44 lines
- int createSocialNetwork (int num_publishers, int num_fabricators, int num_forwarders, int num_sinks, int num_reporters, int num_friends)
- {
- for (int i = 0; i < num_publishers - 1; i++)
- {
- newAcc* = newAccount(newAccountId(),newAccountName(),1);
- newAcc->numFriends = num_friends;
- newAcc->
- }
- NumberAccounts++;
- return -99;
- }
- // creates a new fake story and returns a pointer to the newly created string
- // 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
- // where C is one of the consonants in the Consonants array and V is one of the vowels in the Vowels array
- // my implementation is 10 lines
- char* aFakeStory ()
- {
- return NULL;
- }
- // returns 1 if story is a fake story
- // otherwise retunrs 0
- // my implementation is 10 lines
- int isFakeStory (char* story)
- {
- return -99;
- }
- // creates a new real story.
- // A real story is comprised of two DIFFERENT words randomly taken from the TrueStoryWords array
- // Hence, if the first randomly picked word is MAYOR the second one cannot be MAYOR
- // Exmaples of correctly formed true stories: BREXIT UK, MINISTER DATE, UK WIN, UK MINISTER, DATE WIN, ...
- // Exmaples of incorrectly formed stories include: DATE DATE (same word), BREXIT (one word), UK WIN MAYOR (more than 2 words)
- // the function returns a pointer to the newly created string
- // my implementation is 9 lines
- char* aRealStory ()
- {
- return NULL;
- }
- // create a new message to encapsulate a story to be sent from sender to recipient
- // and returns the newly created message
- // my implementation is 6 lines
- Message createMessage (char* a_story, Account* sender, Account* recipient)
- {
- }
- // upload a message to the server, i.e. add the message to the server's pendingMessages array
- // returns -1 if the pendingMessages array is full
- // otherwise returns 1
- // my implementation is 5 lines
- int uploadMessage (Message a_message)
- {
- return -99;
- }
- // push a message to the recipient's inbox, i.e. add the message to the recipient's receivedM array
- // returns -1 if the recipient's receivedM array is full
- // otherwise returns 1
- // my implementation is 5 lines
- int pushMessage (Message a_message)
- {
- return -99;
- }
- // send a story from sender to recipient by:
- // 1. creaing a message for the story
- // 2. uploading the message to the server
- // 3. adding the message to the sentM array of the sender so that the sender knows that it has been sent
- // returns -1 if something goes wrong, e.g. upload message to server failed or the sender's sentM array is full
- // otherwise returns 1
- // my implementation is 9 lines
- int sendStory (char* a_story, Account* sender, Account* recipient)
- {
- return -99;
- }
- // returns 1 (true) if the story has been reported, i.e. it exists in the server's reportedFakeStories array
- // otherwise it returns 0
- // my implementation is 4 lines
- int isReported (char * story)
- {
- return -99;
- }
- // report a fake story, i.e. add the story to the server's reportedFakeStories array
- // if the story has already been reported (it does exist in the reportedFakeStories array) the function should not
- // add it again
- // returns -1 if the reportedFakeStories array is full
- // otherwise it returns 1
- // my implementation is 8 lines
- int ReportFakeStory(char* story)
- {
- return -99;
- }
- // transmit all the messages in the server's pendingMessages array to their respetive recipient's
- // by adding them to the recipient's inbox
- // if a message has been reported as fake (it exists in the reportedFakeStories array), it should not be transmitted
- // my implementation is 9 lines
- int transmitAllMessages ()
- {
- return -99;
- }
- // seraches for a story in the account's sentM array
- // returns 1 if found
- // 0 if not found
- // my implementation is 4 lines
- int isSent (Account* account, char* story)
- {
- return -99;
- }
- // for RealNewsPublisher and FakeNewsFabricator, this function allows the account to create a new story
- // (unless the maximum number of new stories is reach) and send the story to all friends
- // for all other account types the function will
- // go through all messages in the inbox of account and for each message that has not been read:
- // set the isRead flag is 1
- // decide if the story must be forwarded to all friends or not (the decision depends on the account type)
- // decide if the story must be reportd as fake (the decision depends on the account type) and report it
- // note that if a story has already been sent to friends (because it exists in the sentM array) it should not be sent again
- // the function should return the number of sent messages
- // my implementation is 55 lines
- int originateOrforwardNews (Account* account)
- {
- return -99;
- }
- // this is the function that simulates the propagation of news throughout the network
- // the pseudocode of the function is in the problem description
- // always returns 1
- // my implementation is 13 lines
- int simulate ()
- {
- return -99;
- }
- // The following functions have already been implmented
- // you may wish
- // this function counts the number of fake news stories in every inbox of every account in the newtwork
- int CountFakeNews ()
- {
- int countfake = 0;
- for (int i = 0; i < NumberAccounts ; i++)
- for (int j = 0; j < AllAccounts[i]->numberReceivedM; j++)
- if (isFakeStory( AllAccounts[i]->receivedM[j].theStory))
- countfake++;
- return countfake;
- }
- // this function counts the number of real news stories in every inbox of every account in the newtwork
- int CountRealNews ()
- {
- int count = 0;
- for (int i = 0; i < NumberAccounts ; i++)
- for (int j = 0; j < AllAccounts[i]->numberReceivedM; j++)
- if (isFakeStory( AllAccounts[i]->receivedM[j].theStory) == 0)
- count++;
- return count;
- }
- // displays all the accounts on the terminal
- // always returns 1
- int lisAllAccounts ()
- {
- printf ("ID\tName\tClass.\tNo.Friends\tFriends\n");
- for (int i=0; i < NumberAccounts ; i++)
- {
- printf ("%i\t", AllAccounts[i]->accId);
- printf ("%s\t", AllAccounts[i]->accName);
- printf ("%i\t", AllAccounts[i]->accType);
- printf ("%i\t", AllAccounts[i]->numFriends);
- for (int j =0; j <AllAccounts[i]->numFriends ; j++)
- printf ("%s,", AllAccounts[i]->Friends[j]->accName);
- printf ("\n");
- }
- return 1;
- }
- // displays the social network as a friendhsip matrix
- // always returns 1
- int showFriendshipMatrix()
- {
- printf ("The friendship matrix\n");
- printf (" ");
- for (int i=0; i < NumberAccounts ; i++)
- {
- Account* this_account = AllAccounts[i];
- printf ("%s ", this_account->accName);
- }
- printf ("\n");
- for (int i=0; i < NumberAccounts ; i++)
- {
- Account* this_account = AllAccounts[i];
- printf ("%s ", this_account->accName);
- for (int f =0 ; f < NumberAccounts ; f++)
- {
- if (isFriend (this_account , AllAccounts[f]))
- printf (" f ");
- else
- printf (" n ");
- }
- printf ("\n");
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement