Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define LNAME_SIZE 20
#define FNAME_SIZE 15
#define TEL_NUM_SIZE 12
#define TEL_BOOK_SIZE 75
int num, target1;
struct tel_book_element
{
char name[LNAME_SIZE];
char name1[FNAME_SIZE];
char tel_num[TEL_NUM_SIZE];
};
typedef struct tel_book_element Tel_Book_Element;
struct tel_book
{
Tel_Book_Element entry[TEL_BOOK_SIZE];
int n;
};
typedef struct tel_book Tel_Book;
/* The Functions */
void modify_entry(Tel_Book *);
void delete_entry(Tel_Book *);
void input_new_book(Tel_Book *);
void look_up_name(Tel_Book *);
void view_phone_book(Tel_Book *);
int find_entry(const Tel_Book *, const char []);
int insert_entry(Tel_Book *, const Tel_Book_Element *);
int compare_entries(const Tel_Book_Element *, const Tel_Book_Element *);
int get_tel_book(const char [], Tel_Book *);
int main()
{
FILE *fp =fopen("list.txt","r");
Tel_Book *tbp,tb;
int i,stop;
char choice[80]={'\0'};
tbp = &tb;
i = stop = 0;
if(!(get_tel_book("list.txt", &tb)))
{
printf("Error: list.txt not found!\n");
return 0;
}
while(!stop)
{
printf("Menu of Operations for Automated Telephone Book\n"
"a) Add New Record\n"
"b) Delete Record\n"
"c) Edit Record\n"
"d) Search Phone Book\n"
"e) View Phone Book\n"
"f) Quit\n"
"Enter your selection (a - f) > ");
gets(choice);
switch(choice[0])
{
case 'a': modify_entry(tbp); break;
case 'b': delete_entry(tbp); break;
case 'c': modify_entry(tbp); break;
case 'd': look_up_name(tbp); break;
case 'e': view_phone_book(tbp); break;
case 'f': stop = 1; break;
default : printf("That option hasn't been written yet.\n\n\n");
break;
}
}
if((fp = fopen("list.txt", "w")) == NULL)
{
printf("Unable to write changes to list.txt!\n");
return 1;
}
for(i=0; i < tb.n; i++)
{
fputs(tb.entry[i].name, fp);
fputs(tb.entry[i].name1, fp);
fputs(tb.entry[i].tel_num, fp);
}
return 1;
}
void look_up_name(Tel_Book *tbp)
{
char target[LNAME_SIZE] = {'\0'};
int i,j,found;
i = j = found = 0;
printf("\nEnter name of person to look up: ");
fgets(target, LNAME_SIZE, stdin);
while(target[i] != '\0')
{
target[i] = toupper(target[i]);
i++;
}
for(j=0; j < (tbp->n); j++)
{
if(!(strcmp(target, tbp->entry[j].name)))
{
found = 1;
break;
}
}
if(found)
printf("%s%s%s\n\n\n", tbp->entry[j].name, tbp->entry[j].name1, tbp->entry[j].tel_num);
else
printf("Name not Found\n\n");
return;
}
void view_phone_book(Tel_Book *tbp)
{
printf("\nPrint phone book\n");
return;
}
void delete_entry(Tel_Book *tb)
{
char target[LNAME_SIZE] = {'\0'};
int i,j;
i = j = 0;
printf("\nEnter name to delete from phone book: \n");
fgets(target, LNAME_SIZE, stdin);
while(target[i] != '\0')
{
target[i] = toupper(target[i]);
i++;
}
i = find_entry(tb, target);
if(i == -1)
{
printf("Name not Found - No Entry Deleted\n\n");
return;
}
for(j = (tb->n) - 2; j >= i; j--)
{
tb->entry[j] = tb->entry[j+1];
}
tb->n -= 1;
printf("Entry Deleted\n\n");
return;
}
void modify_entry(Tel_Book *tb)
{
char target[LNAME_SIZE]={'\0'}, char target1[FNAME_SIZE]={'\0'}, num[TEL_NUM_SIZE]={'\0'};
Tel_Book_Element e;
int i=0;
printf("\nEnter Last name to add or modify: ");
fgets(target, LNAME_SIZE, stdin);
printf("\nEnter the person's first name ");
fgets(target1, FNAME_SIZE, stdin);
printf("Enter person's phone number: ");
fgets(num, TEL_NUM_SIZE, stdin);
while(target[i] != '\0')
{
target[i] = toupper(target[i]);
i++;
}
if(find_entry(tb, target) == -1)
{
strcpy(e.name, target);
strcpy(e.name1, target1);
strcpy(e.tel_num, num);
insert_entry(tb, &e);
printf("New Entry Inserted\n\n");
return;
}
strcpy(tb->entry[i].name1, target1);
strcpy(tb->entry[i].tel_num, num);
printf("Modified Entry Inserted\n\n");
return;
}
void input_new_book(Tel_Book *old_tb)
{
Tel_Book new_unsorted_tb, new_tb;
char file_name[13] = "list.txt";
int i=0, j=0;
new_tb.n = new_unsorted_tb.n = 0;
printf("\nEnter file name with new phone book info: ");
gets(file_name);
if(!get_tel_book(file_name, &new_unsorted_tb))
{
printf("File %s not found - phone book unaltered\n\n", file_name);
return;
}
for(i = 0; i < (new_unsorted_tb.n) ; i++)
{
while(new_unsorted_tb.entry[i].name[j] != '\0')
{
new_unsorted_tb.entry[i].name[j] =
toupper(new_unsorted_tb.entry[i].name[j]);
j++;
}
insert_entry(&new_tb, &new_unsorted_tb.entry[i]);
j=0;
}
*old_tb = new_tb;
printf("Old telephone book overwritten by new one\n\n");
return;
}
int find_entry(const Tel_Book *tb, const char target[])
{
int i=0, found=0;
for(i=0; i<(tb->n); i++)
{
if(!strcmp(target, tb->entry[i].name))
{
found = 1; break;
}
}
if(found) return i;
return -1;
}
int insert_entry(Tel_Book *tb, const Tel_Book_Element *e)
{
int i,r,added;
i = r = added = 0;
if(!tb->n)
{
tb->entry[0] = *e;
tb->n = 1;
return 0;
}
for(i = (tb->n - 1); i >= 0; i--)
{
r = compare_entries(&tb->entry[i], e);
if(r > 0)
{
tb->entry[i+1] = tb->entry[i];
}
else if(r < 0)
{
tb->entry[i+1] = *e;
added = 1;
break;
}
}
tb->n += 1;
if(added) return i+1;
tb->entry[0] = *e;
return 0;
}
int compare_entries(const Tel_Book_Element *e1, const Tel_Book_Element *e2)
{
return strcmp(e1->name, e2->name);
}
int get_tel_book(const char fileName[], Tel_Book *tbp)
{
FILE *fp;
int i = 0;
if((fp = fopen(fileName, "r")) == NULL)
return 0;
while(fgets(tbp->entry[i].name, LNAME_SIZE, fp) != NULL)
{
fgets(tbp->entry[i].name1, FNAME_SIZE, fp); fgets(tbp->entry[i].tel_num, TEL_NUM_SIZE, fp);
i++;
}
tbp->n = i;
return 1;
}