implement an address book;
Contacts can be used to store 1000 of people's information, and each person's information includes:
Name, gender, age, telephone, address
Available methods:
1. Add contact information
2. Delete the specified contact information
3. Find the specified contact information
4. Modify the specified contact information
5. Show All contact information
6. Clear All contacts
Defining header Files
<span style= "FONT-SIZE:18PX;" > #ifndef __contact_h__#define __contact_h__#define name_max 20#define sex_max 5#define tele_max 11#define ADDR_ MAX 20#define peo_max 1000#include <stdio.h> #include <string.h> #include <stdlib.h>typedef struct people{ char Name[name_max]; Char Sex[sex_max]; int age; Char Tele[tele_max]; Char Addr[addr_max];} *ppeople;typedef struct contact{ int count; struct people Dhb[peo_max];} *pcontact;void Add_peo (pcontact pcon); void Del_peo (Pcontact pcon); void Search_peo (Pcontact pcon); void Modify_peo ( Pcontact pcon); void Show_peo (Pcontact pcon); void Clear_peo (Pcontact pcon); #endif </span>
part of the function implementation
<span style= "FONT-SIZE:18PX;" > #include "contact.h" int find (pcontact pcon,char* name) {int i = 0; while (I<pcon->count) {if (strcmp (name,pcon->dhb[i].name) = = 0) return i; i++; } return-1;} void Add_peo (Pcontact pcon)//Add Contact {if (Pcon->count = = Peo_max) {printf ("Phone book capacity up to \ n"); Return } printf ("name:>"); scanf ("%s", (Pcon->dhb[pcon->count]). Name); printf ("sex:>"); scanf ("%s", (Pcon->dhb[pcon->count]). Sex); printf ("age:>"); scanf ("%d",& ((Pcon->dhb[pcon->count]). age)); printf ("tele:>"); scanf ("%s", (Pcon->dhb[pcon->count]). Tele); printf ("addr:>"); scanf ("%s", (Pcon->dhb[pcon->count]). addr); pcon->count++;} void Del_peo (Pcontact pcon)//delete contact {char Tmpname[name_max]; int ret=0; printf ("Enter the name of the contact to be deleted:>"); scanf ("%s", tmpname); if ( -1! = (Ret=find (pcon,tmpname))) {int j=ret; for (; j<pcon->count;j++) {Pcon->dhb[j] = pcon->dhb[j+1]; } Pcon->count-= 1; } else printf ("The contact does not exist \ n");} void Search_peo (Pcontact pcon)//Find contact {char Tmpname[name_max]; int ret = 0; printf ("Please enter the contact you want to find"); scanf ("%s", tmpname); ret = find (pcon,tmpname); if (ret! =-1) {printf ("%s\t%s\t%d\t%s\t%s\n", Pcon->dhb[ret].name, pcon->dhb[ Ret].sex, Pcon->dhb[ret].age, Pcon->dhb[ret].tele, PCON->DHB[RET].ADDR) ; }}void Modify_peo (pcontact pcon)//Modify Contact {char Tmpname[name_max]; int ret = 0; printf ("Please enter the contact you want to modify"); scanf ("%s", tmpname); ret = find (pcon,tmpname); if ( -1! = ret) {printf ("name:>"); scanf ("%s", (Pcon->dhb[ret]). Name); printf ("sex:>"); scanf ("%s", (Pcon->dhb[ret]). Sex); printf ("age:>"); scanf ("%d",& ((Pcon->dhb[ret]). age)); PrinTF ("tele:>"); scanf ("%s", (Pcon->dhb[ret]). Tele); printf ("addr:>"); scanf ("%s", (Pcon->dhb[ret]). addr); }}void Show_peo (pcontact pcon)//Show all contacts {int i = 0; for (i=0;i<pcon->count;i++) {printf ("%s\t%s\t%d\t%s\t%s\n", Pcon->dhb[i].name, Pcon->dhb[i].sex, Pcon->dhb[i].age, Pcon->dhb[i].tele, PCON->DHB [I].ADDR]; }}void Clear_peo (pcontact pcon)//Empty all contacts {pcon->count = 0;} </span>
Test function
<span style= "FONT-SIZE:18PX;" > #include "contact.h" void Show_menu () {printf ("************************\n"); printf ("* 1>add 2>del *\n"); printf ("* 3>search 4>modify *\n"); printf ("* 5>show 6>clear *\n"); printf ("************************\n");} int main () {struct contact my_contact; My_contact.count = 0; int input = 1; while (input) {show_menu (); printf ("Please input:>"); scanf ("%d", &input); switch (input) {case 1:ADD_PEO (&my_contact); System ("CLS"); Clear screen break; Case 2:del_peo (&my_contact); System ("CLS"); Break Case 3:search_peo (&my_contact); Break Case 4:modify_peo (&my_contact); System ("CLS"); Break Case 5:show_peO (&my_contact); Break Case 6:clear_peo (&my_contact); System ("CLS"); Break Default:break; }} return 0;} </span>
C Language Address Book