In: Computer Science
You have worked very hard on your phonebook program for many weeks and it is becoming a popular application with all your friends. You decide you want to compete with facebook.com and the next upgrade of your phonebook software should make contact data accessible even if the user closes your application and returns to it at a later time. Add additional functionality to your previous phonebook program. Make it possible for users to: 1) Store all entries in the phonebook into a location/file-name specified by the user. 2) Retrieve entries from the location/file-name specified by the user. If the user does not explicitly specify a path to the file, a default location of your choosing should be used. Answer needs to be in C Programming. Please and thank you.
Current code is:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// Creates an array for the input from the user
struct phonebook{
char firstname[100];
char lastname[100];
char phonenumber[100];
};
void addfriend(struct phonebook **, int *);
void deletefriend(struct phonebook **, int);
void showPhonebook(struct phonebook **);
//Listed below are the 4 new functions for this code
void sortByFirstName(struct phonebook **);
void findNumber(struct phonebook **);
void randomSelect(struct phonebook **);
void deleteAll(struct phonebook **);
int main()
{
int i;
// This will create an array of type struct for phonebook
struct phonebook **arr = (struct phonebook **)malloc(50 * sizeof(struct phonebook *));
// This integer will be the index at which a new entry will be added
int index = 0;
// This part of the code will set all entries of arr to NULL
for( i = 0 ; i < 50 ; i++ )
arr[i] = NULL;
while(1)
{
//Prints the menu for the user to select an option
printf("Phone Book Application\n");
printf(" 1) Add friend\n");
printf(" 2) Delete friend\n");
printf(" 3) Show phone book\n");
printf(" 4) Sort by first name\n");
printf(" 5) Find a phone number\n");
printf(" 6) Randomly select a friend\n");
printf(" 7) Delete all\n");
printf(" 8) Exit\n");
printf("\nWhat would you like to do?: ");
int option;
scanf("%d",&option);
switch(option)
//This section of the code states cases based on the users input
{
case 1 : addfriend(arr, &index);
break;
case 2 : deletefriend(arr, index);
break;
case 3 : showPhonebook(arr);
break;
case 4 : sortByFirstName(arr);
break;
case 5 : findNumber(arr);
break;
case 6 : randomSelect(arr);
break;
case 7 : deleteAll(arr);
break;
case 8 : sortByFirstName(arr);
exit(0);
default : printf("Please enter a choice listed \n");
exit(0);
}
}
return 0;
}
void addfriend(struct phonebook **arr, int *index)
{
struct phonebook *temp = (struct phonebook *)malloc(sizeof(struct phonebook));
printf("Please enter the name of the friend\n");
scanf("%s%s", temp->firstname, temp->lastname);
printf("Please enter their phone number\n");
scanf("%s", temp->phonenumber);
arr[*index] = temp;
(*index)++;
}
void deletefriend(struct phonebook **arr, int index)
{
char f[100], l[100];
printf("Please enter the name of the friend\n");
scanf("%s%s", f, l);
int i;
// This part of the code will locate the required contact
for( i = 0 ; i < 50 ; i++ )
// This part of the code will display the contact if found
if(arr[i] && !strcmp(arr[i]->firstname, f) && !strcmp(arr[i]->lastname, l))
// This part of the code sets arr to NULL
arr[i] = NULL;
}
void showPhonebook(struct phonebook **arr)
{
int i;
for( i = 0 ; i < 50 ; i++ )
// This part of the code will display the contact if arr is is not NULL
if(arr[i])
printf("Name : %s %s\nPhone Number : %s\n\n",arr[i]->firstname, arr[i]->lastname, arr[i]->phonenumber);
}
// This part of the code creates a function that will sort the phonebook entries by first name
void sortByFirstName(struct phonebook **arr)
{
int i,j;
// This part of the code helps store the temp data
char *temp1= (char*)malloc(sizeof(char)*100);
char *temp2=(char*)malloc(sizeof(char)*100);
char *temp3=(char*)malloc(sizeof(char)*8);
for(i=0;i<50;i++)
{
if(arr[i])
for(j=i+1;j<50;j++)
{
if(arr[j])
if(strcmp(arr[j]->firstname,arr[i]->firstname)<0)// This part of the code will compare the names found in the phonebook
{
strcpy(temp1,arr[j]->firstname);// This part of the code will copy to the temp
strcpy(temp2,arr[j]->lastname);
strcpy(temp3,arr[j]->phonenumber);
strcpy(arr[j]->firstname,arr[i]->firstname);// This part of the code will change te array index value
strcpy(arr[j]->lastname,arr[i]->lastname);
strcpy(arr[j]->phonenumber,arr[i]->phonenumber);
strcpy(arr[i]->firstname,temp1);
strcpy(arr[i]->lastname,temp2);
strcpy(arr[i]->phonenumber,temp3);
}
}
}
showPhonebook(arr);// This part of the code will print all of the names found in the phonebook
}
void findNumber(struct phonebook **arr)
{
char f[20], l[20];
printf("Please enter the name of the friend\n");
scanf("%s%s", f, l);
int i;
for( i = 0 ; i < 50 ; i++ ){
// This part of the code will attempt to locate the friend's name in the phonebook
if(arr[i] && !strcmp(arr[i]->firstname, f) && !strcmp(arr[i]->lastname, l)){
// This part of the code will print the phone number if the name is located.
printf("Phone number : %s\n",arr[i]->phonenumber);
return;
}
}//if name not found
printf("Sorry, but this name was not found. \n");
}
// This part of the code allows the user to select a random friend from the phonebook
void randomSelect(struct phonebook **arr)
{
int i;
//This part of the code creates a loop
while(1){
// This part of the code will generate a random number between 0 and 50
i = rand() % 50;
if(arr[i])// If the number generated is not between the two prior numbers, it will be NULL
{
printf("Name : %s %s\nPhone Number : %s\n\n",arr[i]->firstname, arr[i]->lastname, arr[i]->phonenumber);
break;
}
}
}
// This part of the code will allow the user to delete all of the entries in the phonebook
void deleteAll(struct phonebook **arr){
int i;
for( i = 0 ; i < 50 ; i++ )
arr[i] = NULL;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//Function trial product
void ADD();
void Delete1();
void Display1();
void Alphabetical1();
void Number1();
void Random1();
void DeleteAll1();
typedef struct Phone_Book_List {
char *FName;
char *LName;
char *PhoneNumber1;
} list;
//delete entry
typedef struct Delete_Entry {
char *FName;
char *LName;
} take;
//Pointers in to the structures
list *lt1;
take *tk1;
//variables are declared in globally
int cnt = 0;
int delCnt = 0;
int main(void)
{
//VARIABLE in declarations
int iSel;
do {
printf("\n\nPHONE BOOK:\n\n");
printf("1) ADD FRIEND\n");
printf("2) DELETE FRIEND\n");
printf("3) DISPLAY PHONE BOOK\n");
printf("4) SORT AND DISPLAY ENTRIES ALPHABETICALLY BY LAST NAME\n");
printf("5) FIND PHONE NUMBER FOR GIVEN NAME\n");
printf("6) RANDOMLY SELECT CONTACT\n");
printf("7) DELETE ALL CONTACTS\n");
printf("8) EXIT\n");
printf("WHAT DO YOU WANT TO DO? ");
scanf("%d", &iSel);
switch (iSel) {
case 1: //Add entrance toward phonebook
ADD();
break;
case 2: //Delete access toward phonebook
Delete1();
break;
case 3: //Display every access toward phonebook
Display1();
break;
case 4: //Sort entries alphabetically in phonebook
Alphabetical1();
break;
case 5: //Find phone number toward phonebook
Number1();
break;
case 6: //Randomly choose contact toward phonebook
Random1();
break;
case 7: //Delete every one contacts toward phonebook
DeleteAll1();
break;
case 8: //CLOSE THE PROGRAM
break;
default: //while user has been entered wrong access
printf("\nYou entered an invalid selection. Try again.\n");
break;
}
} while (iSel != 8); //END DO WHILE LOOP
//RETURN USED MEMORY
free(tk1);
free(lt1);
tk1 = NULL;
lt1 = NULL;
return 0;
} //END MAIN
//FUNCTION DEFINITIONS
//ADD AN ENTRY
void ADD()
{
char *leftName;
if (cnt == 0)
{
lt1 = (list *) malloc ((cnt*25) + 25);
}
else
{
lt1 = (list *) realloc (lt1, (cnt*50) + 50);
}
if (lt1 == NULL)
{
printf("YOU CANNOT ADD MORE MEMORY\n");
}
else
{
lt1[cnt].FName = (char *) malloc(sizeof(char)*15);
lt1[cnt].LName = (char *) malloc(sizeof(char)*15);
lt1[cnt].PhoneNumber1 = (char *) malloc(sizeof(char)*15);
printf("\nENTER THEIR FIRST NAME: ");
scanf("%s", lt1[cnt].FName);
printf("\nENTER THEIR LAST NAME: ");
scanf("%s", lt1[cnt].LName);
printf("\nENTER THEIR PHONE NUMBER: ");
scanf("%s", lt1[cnt].PhoneNumber1);
printf("\nCONTACT ADDED\n");
}
cnt++;
}//END FUNCTION
//DELETE AN ENTRY
void Delete1()
{
int i;
int q = 0;
char *uName;
if (delCnt == 0)
{
tk1 = (take *) malloc ((delCnt*25) + 25);
}
else
{
tk1 = (take *) realloc (tk1, (delCnt*1) + 1);
}
if (tk1 == NULL)
{
printf("This cannot be deleted (out of memory)\n");
}
else
{
tk1[delCnt].FName = (char *) malloc(sizeof(char)*15);
tk1[delCnt].LName = (char *) malloc(sizeof(char)*15);
printf("\nENTER THEIR FIRST NAME: ");
scanf("%s", tk1[delCnt].FName);
printf("\nENTER THEIR LAST NAME: ");
scanf("%s", tk1[delCnt].LName);
}
for (i = 0; i < cnt; i++)
{
if (lt1[i].FName == NULL && lt1[i].LName == NULL) continue;
if (strcmp(lt1[i].FName, tk1[delCnt].FName) == 0 && strcmp(lt1[i].LName, tk1[delCnt].LName) == 0)
{
printf("\n%s %s has been deleted\n", lt1[i].FName, lt1[i].LName);
lt1[i].FName = NULL;
lt1[i].LName = NULL;
lt1[i].PhoneNumber1 = NULL;
q = 1;
break;
}
} //End for loop
if (q != 1)
{
printf("\nThis person is not in the Phonebook\n");
)
delCnt++;
cnt--;
}
void Display1()
{
int i;
printf("\nYOUR CONTACTS:\n");
for (i = 0; i < cnt; i++)
{
if (lt1[i].FName != NULL && lt1[i].LName != NULL)
{
printf("\n%s %s: %s\n", lt1[i].FName, lt1[i].LName, lt1[i].PhoneNumber1);
}
}//End for loop
system("pause");
}
//Sort entries alphabetically by last name.
void Alphabetical1()
{
int i;
int j;
char temp[50][50];
printf("\nYOUR CONTACTS:\n");
for (i = 0; i < cnt; i++)
{
for (j = i + 1; j < cnt; j++)
{
if (strcmp(lt1[i].LName, lt1[j].LName) > 0)
{
strcpy(temp[i], lt1[i].LName);
strcpy(lt1[i].LName, lt1[j].LName);
strcpy(lt1[j].LName, temp[i]);
}
}
}//End for loop
for (i = 0; i < cnt; i++)
printf("\n%s %s: %s\n", lt1[i].FName, lt1[i].LName, lt1[i].PhoneNumber1);
system("pause");
}//End function
void Number1()
{
int i;
int q = 0;
char fName[25];
char lName[25];
printf("\nEnter their First Name: ");
scanf("%s", fName);
printf("\nEnter their Last Name: ");
scanf("%s", lName);
for (i = 0; i < cnt; i++)
{
if (strcmp(lt1[i].FName, fName) == 0 && strcmp(lt1[i].LName, lName) == 0)
{
printf("\n%s %s's PHONE NUMBER IS: %s\n", lt1[i].FName, lt1[i].LName, lt1[i].PhoneNumber1);
q = 1;
break;
}
}
if (q != 1)
{
printf("\nThis person is not in the Phonebook\n");
}
system("pause");
}
void Random1()
{
srand(time(NULL));
int iRandomNum;
iRandomNum = (rand() % cnt) + 1;
printf("%s %s: %s\n",
lt1[iRandomNum].FName, lt1[iRandomNum].LName, lt1[iRandomNum].PhoneNumber1);
system("pause");
}
void DeleteAll1()
{
int i;
for (i = 0; i < cnt; i++)
{
do{
lt1[i].FName = NULL;
lt1[i].LName = NULL;
lt1[i].PhoneNumber1 = NULL;
break;
} while (i <= cnt);
}
printf("\nALL CONTACTS DELETED\n\n");
system("pause");
}//End function