Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help with these additions to the following code. It needs to STRICTLY be in C coding only. I also need you to post
I need help with these additions to the following code.
It needs to STRICTLY be in C coding only.
I also need you to post a screen shot of the output along with the solution.
Programming assignment (100 pts): You have worked very hard on your phonebook program for many wecks 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 phonebook program from lab# 7 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 expicity specify a path to the fie, o defaut location of your chooing should be used #include
#include
#include
#include
struct data
{
char FirstName[50];
char LastName[50];
char PhoneNumber[10];
struct data *next;
};
struct data *head = NULL;
void insert()
{
struct data *NewData = (struct data *)malloc(sizeof(struct data));
struct data *temp = head;
printf(" First name: ");
scanf("%s", &NewData->FirstName);
printf("Last name: ");
scanf("%s", &NewData->LastName);
printf("Enter Phonenumber: ");
scanf("%s", &NewData->PhoneNumber);
NewData->next = NULL;
if (head == NULL)
{
head = NewData;
printf("Contact added! ");
}
else
{
while (temp->next != NULL)
temp = temp->next;
temp->next = NewData;
printf("Contact added! ");
}
}
void del()
{
char val[50];
printf(" Enter first name of contact:");
scanf("%s", &val);
struct data *temp = head;
struct data *prev;
if (temp != NULL && strcmp(temp->FirstName, val) == 0)
{
head = head->next;
free(temp);
printf(" Contact Deleted! ");
}
else
{
while (temp != NULL && strcmp(temp->FirstName, val) == 1)
{
prev = temp;
temp = temp->next;
}
if (temp == NULL)
printf(" Contact doesn't exist ");
else
{
prev->next = temp->next;
free(temp);
printf(" Record deleted.");
}
}
}
void show()
{
if (head == NULL)
printf(" Phonebook doesnt have any contacts ");
else
{
printf(" Phonebook contacts:");
struct data *temp = head;
while (temp != NULL)
{
if (temp->PhoneNumber != NULL && temp->PhoneNumber[3] == '-' && strlen(temp->PhoneNumber) == 8)
{
printf(" %s %s %s", temp->FirstName, temp->LastName, temp->PhoneNumber);
}
temp = temp->next;
}
printf(" ");
}
}
void sort()
{
struct data *t = head, *t1 = NULL, *prev = NULL;
while(t!=NULL){
t1 = t->next;
while(t1!=NULL){
if(strcmp(t1->FirstName, t->FirstName)
char FirstName[50], LastName[50], phoneNumber[10];
strcpy(FirstName, t->FirstName);
strcpy(LastName, t->LastName);
strcpy(phoneNumber, t->PhoneNumber);
strcpy(t->FirstName, t1->FirstName);
strcpy(t->LastName, t1->LastName);
strcpy(t->PhoneNumber, t1->PhoneNumber);
strcpy(t1->FirstName, FirstName);
strcpy(t1->LastName, LastName);
strcpy(t1->PhoneNumber, phoneNumber);
}
prev = t1;
t1 = t1->next;
}
t=t->next;
}
show();
}
void search()
{
char name[50];
printf("Enter name to search: ");
scanf("%s", name);
struct data *temp = head;
while(temp!=NULL){
if(strcmp(temp->FirstName, name)==0){
printf("Name of Contact: %s %s %s ", temp->FirstName, temp->LastName, temp->PhoneNumber);
}
temp = temp->next;
}
}
void Call()
{
srand((int)time(0));
int B = 0;
struct data *t=head;
while(t!=NULL){
B++;
t= t->next;
}
int r = rand()%B;
t = head;
B=0;
while(t!=NULL){
if(B==r){
printf("Now calling \"%s %s\" %s ", t->FirstName, t->LastName, t->PhoneNumber);
}
B++;
t = t->next;
}
}
void Obliterate()
{
struct data *temp = head;
struct data *prev;
while (temp != NULL)
{
head = head->next;
free(temp);
temp = temp->next;
printf(" Contact Deleted! ");
}
}
void main()
{
int change;
while (1)
{
printf(" Phone Book application.");
printf(" \t1) Add contact");
printf(" \t2) Delete contact");
printf(" \t3) Show contacts");
printf(" \t4) Alphabeticaly sort contacts");
printf(" \t5) Find a phone number for a given name");
printf(" \t6) Randomly select contacts");
printf(" \t7) Delete entire contact list");
printf(" Please choose an option: ");
scanf("%d", &change);
switch (change)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
show();
break;
case 4:
sort();
break;
case 5:
search();
break;
case 6:
Call();
break;
case 7:
Obliterate();
break;
default:
printf("ADIOS");
exit(0);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started