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 specified contact information
4. Modify the specified contact information
5. Show all contact information
6. Clear All contacts
Main.c
#include "contact.h"//Find contact, return subscript, int find (PCon Pcon,char *name) {int I=0;while (I < pcon->num) {if (strcmp (name, pcon->telbook[i].name) = = 0) {return i;} i++;} return-1;} /**************** Add Contact ****************/void add_peo (PCon pCon) {if (Pcon->num==num_max) {printf ("Phone book is full, unable to store! "); return;} printf ("Name:"); scanf ("%s", Pcon->telbook[pcon->num].name);p rintf ("Gender:"); scanf ("%s", pcon->telbook[pcon- >num].sex);p rintf ("Age:"), scanf ("%d",& (pcon->telbook[pcon->num].age));p rintf ("Phone:"); scanf ("%s", Pcon->telbook[pcon->num].tel);p rintf ("Address:"); scanf ("%s", pcon->telbook[pcon->num].addr);p Con->num + +;p rintf ("Add success, press any key to continue ..."); Getch ();} /**************** Delete Contact ****************/void del_peo (PCon pCon) {char delname[name_max];int ret = 0;printf (" Please enter the name of the contact you want to delete: scanf ("%s", delname), ret = Find (Pcon,delname), if (0 = = ret) {int j;for (j=ret;j<pcon->num-1;j++) { PCON->TELBOOK[J]=PCON->TELBOOK[J+1];} pcon->num--;p rintf ("Delete succeeded! \ n ");} else{printf ("This contact is not found! \ n ");} printf ("Press any key to continue ..."); Getch ();} /**************** Find Contact ****************/void serach_peo (PCon pCon) {char sername[name_max];int ret = 0;printf (" Please enter the name of the contact you want to find: "); scanf ("%s ", sername), ret = Find (Pcon,sername), if (0 = = ret) {printf ("%s\t%s\t%d\t%s\t%s\n ",pcon-> Telbook[ret].name,pcon->telbook[ret].sex,pcon->telbook[ret].age,pcon->telbook[ret].tel,pcon-> TELBOOK[RET].ADDR);p rintf ("Find done! ");} else{printf ("This contact is not found! \ n ");} printf ("Press any key to continue ..."); Getch (); /**************** Modify Contact ****************/void modify_peo (PCon pCon) {char modname[name_max];int ret = 0;printf (" Please enter the name of the contact you want to modify: "); scanf ("%s ", modname), ret = Find (Pcon,modname), if (0 = = ret) {printf (" Name: "); scanf ("%s ",pcon-> Telbook[ret].name);p rintf ("Gender:"); scanf ("%s", Pcon->telbook[ret].sex);p rintf ("Age:"); scanf ("%d",& (pcon- >telbook[ret].age));p rintf ("Phone:"), scanf ("%s", Pcon->telbook[ret].tel);p rintf ("Address:"); scanf ("%s",pcon-> TELBOOK[RET].ADDR);p rintf ("modified successfully! ");} else{printf ("No This contact information, cannot be modified.") \ n ");} printf ("Press any key to continue ..."); Getch (); /**************** Show Contact person ****************/void Show_peo (PCon pCon) {int i;if (Pcon->num = = 0) {printf ("Phone book is empty! ");} Else{for (i=0;i<pcon->num;i++) {printf ("%s\t%s\t%d\t%s\t%s\n", Pcon->telbook[i].name,pcon->telbook[i] . sex,pcon->telbook[i].age,pcon->telbook[i].tel,pcon->telbook[i].addr);}} printf ("Press any key to continue ..."); Getch (); /**************** Empty Contacts ****************/void Clear_peo (PCon pCon) {pcon->num = 0;printf ("Erase complete, press any key to continue ..."); Getch ( );}
TEST.c
#include "contact.h" char Show_menu () {printf ("***************************************\n");p rintf ("* menu *\ n ");p rintf (" * 1. Add Contact 2. Delete contact *\n ");p rintf (" * 3. Find Contacts 4. Modify Contact *\n ");p rintf (" * 5. Show Contacts 6. Clear contact *\n ");p rintf (" * 0. Exit *\n ");p rintf (" ***************************************\n " ); return 0;} int main () {struct contact My_contact;int input;my_contact.num=0;do{system ("CLS"); Show_menu ();p rintf ("Select the action you want:"); X:SCANF ("%d", &input), if (input>=0 && input <=6) {switch (input) {case 0:return 0;case 1: {Add_peo (& My_contact); break;} Case 2: {del_peo (&my_contact); Case 3: {serach_peo (&my_contact); Case 4: {modify_peo (&my_contact); Case 5: {show_peo (&my_contact); Case 6: {clear_peo (&my_contact); Default:break;}} else{printf ("Input error, please re-enter:"); goto x;}} while (input>=0 && input<=6); return 0;}
Contact.h
#ifndef __contact_h__#define __contact_h__#include <stdio.h> #include <string.h> #include <stdlib.h > #include <conio.h> #define NAME_MAX 20#define sex_max 5#define tel_max 12#define addr_max 20#define Num_max 1000 typedef struct PEOPLE{CHAR Name[name_max];char sex[sex_max];int age;char tel[tel_max];char Addr[addr_max];} ppeople;typedef struct Contact{int num;struct people Telbook[num_max];} *pcon;void Add_peo (PCon pCon); void Del_peo (PCon pCon); void Serach_peo (PCon pCon); void Modify_peo (PCon pCon); void Show_ PEO (PCon pCon); void Clear_peo (PCon pCon); #endif
Program Run Result:
C Language Address Book