#include <stdio.h> #include <stdlib.h> #include <string.h> #include<time.h> stru
ID: 3732290 • Letter: #
Question
#include <stdio.h> #include <stdlib.h> #include <string.h> #include<time.h> 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)<1){ 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(" 1) Add contact"); printf(" 2) Delete contact"); printf(" 3) Show contacts"); printf(" 4) Alphabeticaly sort contacts"); printf(" 5) Find a phone number for a given name"); printf(" 6) Randomly select contacts"); printf(" 7) 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); } } }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include<time.h> 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)<1){ 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(" 1) Add contact"); printf(" 2) Delete contact"); printf(" 3) Show contacts"); printf(" 4) Alphabeticaly sort contacts"); printf(" 5) Find a phone number for a given name"); printf(" 6) Randomly select contacts"); printf(" 7) 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); } } }
Programming assignment (100 pts): 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 phonebook program from lab# 7, Make it possible for users to: 1) Store all entries in the phonebook into a location/ile-name specified by the user. 2) Retrieve entries from the location/file-name specified by the user. If the user does not explicifly specity a path to the tie, o default location of your choosing should be used
Explanation / Answer
Given below is the modified code for the question. On choosing any other option excluding 1-7, the program saves the existing contact list to a file named contacts.txt and then loads it when the program runs again.
Please do rate the answer if it was helpful. Thank you
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<time.h>
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)
{
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)<0){
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 Load(char *filename)
{
FILE *fp = fopen(filename, "r");
struct data *NewData = (struct data *)malloc(sizeof(struct data));
struct data *tail = head;
char fname[50];
if(fp == NULL)
return;
fscanf(fp, "%s", fname);
int count = 0;
while(!feof(fp))
{
count++;
NewData = (struct data *)malloc(sizeof(struct data));
strcpy(NewData->FirstName, fname);
fscanf(fp, "%s", NewData->LastName);
fscanf(fp, "%s", NewData->PhoneNumber);
NewData->next = NULL;
if (head == NULL)
{
head = tail = NewData;
}
else
{
tail->next = NewData;
tail = NewData;
}
fscanf(fp, "%s", fname);
}
fclose(fp);
if(count != 0)
printf("Loaded %d contacts from file ", count);
}
void Save(char *filename)
{
int count = 0;
struct data *ptr = head;
FILE *fp = fopen(filename, "w");
if(fp == NULL)
printf("ERROR: Could not open file %s for writing ", filename);
while(ptr != NULL)
{
fprintf(fp, "%15s %15s %15s ", ptr->FirstName, ptr->LastName, ptr->PhoneNumber);
ptr = ptr->next;
count++;
}
fclose(fp);
if(count != 0)
printf("Saved %d contacts to file %s ", count, filename);
}
int main()
{
int change;
char filename[] = "contacts.txt";
Load(filename);
while (1)
{
printf(" Phone Book application.");
printf(" 1) Add contact");
printf(" 2) Delete contact");
printf(" 3) Show contacts");
printf(" 4) Alphabeticaly sort contacts");
printf(" 5) Find a phone number for a given name");
printf(" 6) Randomly select contacts");
printf(" 7) 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 ");
Save(filename);
exit(0);
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.