Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include<time.h> stru

ID: 3732289 • 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 fron 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 User be used.

Explanation / Answer

#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 import()

{

char file[255];

printf("Enter filename to import: ");

scanf("%s", file);

FILE *fp;

fp = fopen(file, "r");

if (fp == NULL)

{

printf("Unable to open file ");

}

else

{

int count = 0;

struct data *NewData = (struct data *)malloc(sizeof(struct data));

while (fscanf(fp, "%s %s %s", NewData->FirstName, NewData->LastName, NewData->PhoneNumber) != -1)

{

NewData->next = NULL;

if (head == NULL)

{

head = NewData;

}

else

{

struct data *temp = head;

while (temp->next != NULL)

temp = temp->next;

temp->next = NewData;

}

NewData = (struct data *)malloc(sizeof(struct data));

count++;

}

printf("%d contacts imported ", count);

}

fclose(fp);

}

void export()

{

if(head!=NULL){

char file[255];

printf("Enter filename to export: ");

scanf("%s", file);

FILE *fp;

fp = fopen(file, "w");

int c = 0;

struct data *temp = head;

while (temp != NULL)

{

if (temp->PhoneNumber != NULL && temp->PhoneNumber[3] == '-' && strlen(temp->PhoneNumber) == 8)

{

fprintf(fp, "%s %s %s ", temp->FirstName, temp->LastName, temp->PhoneNumber);

c++;

}

temp = temp->next;

}

fclose(fp);

printf("Successfully %d exported ", c);

}

}

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(" 8) Import contacts from file");

printf(" 9) Export contacts to file");

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;

case 8:

import();

break;

case 9:

export();

break;

default:

printf("ADIOS");

exit(0);

}

}

}