Use C language to implement an address book (add, delete, find, modify, display, empty function)

Source: Internet
Author: User

Source Program header File Contact.h

#ifndef _contact_h__#define _contact_h__#define name_max 20#define sex_max 5#define tele_max 15#define ADDR_MAX 50# 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{struct people dhb[peo_max];    int count;} *pcon;void Add_peo (PCon pCon); void Del_peo (PCon pCon); void Search_peo (PCon pCon); void Modify_peo (PCon pCon); void Show_ PEO (PCon pCon); void Clear_peo (PCon pCon); #endif
</pre> source program function segment implementation <p></p><p></p><p></p><pre name= "code" class= "CPP" > #include "contact.h" int Find (pCon Pcon,char * name) {int I=0;while (i<pcon->count) {if (strcmp (name,pcon->dhb[i ].name) ==0)//Find the matching name on the output Ireturn i;i++;} return-1;} void Add_peo (PCon pCon)//Add store man function {//judgment if (Pcon->count==peo_max) {printf ("storage space is full \ n"); return;} printf ("name:>"); scanf ("%s", (Pcon->dhb[pcon->count]). Name);p rintf ("sex:>"), scanf ("%s", pcon-> Dhb[pcon->count]). Sex);p rintf ("age:>"), scanf ("%d",& ((Pcon->dhb[pcon->count]). Age));p rintf (" Tele:> "), scanf ("%s ", (Pcon->dhb[pcon->count]). Tele);p rintf (" addr:> "); scanf ("%s ", pcon->dhb[pcon- >count]). addr);p con->count++;} void Del_peo (PCon pCon)//delete function {char tmpname[name_max];int ret=0;printf ("Please enter the name you want to delete:>"); scanf ("%s", tmpname); ret= Find (Pcon,tmpname), if ( -1! = ret) {int j=ret;for (; j<pcon->count-1;j++) {pcon->dhb[j]=pcon->dhb[j+1];// Overwrite the data you want to delete with the following data}pcon->count--;//save one}ELSE{PRINTF ("Could not find the information to delete \ n");}} void Search_peo (PCon pCon)//Lookup function {char tmpname[name_max];int ret=0;printf ("Please enter the name you want to find:>"); scanf ("%s", tmpname); ret = Find (Pcon,tmpname), if (ret! =-1) {printf ("%s\t%s\t%d\t%s\t%s\t\n", Pcon->dhb[ret].name,pcon->dhb[ret]. SEX,PCON-&GT;DHB[RET].AGE,PCON-&GT;DHB[RET].TELE,PCON-&GT;DHB[RET].ADDR);} else{printf ("There is no information to look for \ n");}} void Modify_peo (PCon pCon)//Modify data function {char tmpname[name_max];int ret=0;printf ("Please enter the name you want to modify:>"); scanf ("%s", Tmpname) ; ret = Find (Pcon,tmpname), if ( -1! = ret)//Find modified name after re-assignment {printf ("name:>"); scanf ("%s", (Pcon->dhb[ret]). Name); printf ("sex:>"); scanf ("%s", (Pcon->dhb[ret]). Sex);p rintf ("age:>"); scanf ("%d",& ((Pcon->dhb[ret)) . Age);p rintf ("tele:>"), scanf ("%s", (Pcon->dhb[ret]). Tele);p rintf ("addr:>"); scanf ("%s", pcon->dhb[ RET]). addr);}} void Show_peo (PCon pCon)//display function {int i;for (i=0;i<pcon->count;i++)//display and print said contact information {printf ("%s\t%s\t%d\t%s\t%s\t\ N ", Pcon->dhb[i].name,pcon->dhb[i].sex,pcon->dhb[i].age,pcon-&GT;DHB[I].TELE,PCON-&GT;DHB[I].ADDR);}} void Clear_peo (PCon pCon)//Clear all contact information {pcon->count = 0;}
</pre><p></p><p> source program main function call segment </p><p></p><p></p><pre name= " Code "class=" CPP "> #include" contact.h "void Show_menu () {printf (" ***************************\n ");p rintf (" *   1 >add       2>del     *\n ");p rintf (" *   3>search    4>modify  *\n ");p rintf (" *   5>show      6>clear   *\n ");p rintf (" ***************************\n ");} int main () {struct contact My_contact;int input=1;my_contact.count = 0;while (input) {show_menu ();p rintf ("Please input: > "), scanf ("%d ", &input), switch (input)//To determine and invoke the function {case 1:{ADD_PEO (&my_contact); System (" CLS ");//Invoke clear-screen function Break    ;} Case 2:{del_peo (&my_contact); System ("CLS");//Invoke clear-screen function break;} Case 3:search_peo (&my_contact); Break;case 4:{modify_peo (&my_contact); System ("CLS");//Invoke clear-screen function break;} Case 5:show_peo (&my_contact); Break;case 6:clear_peo (&my_contact); break;default:break;}} return 0;}

Add a contact function.

void Add_peo (PCon pCon)//Add store man function {//judgment if (Pcon->count==peo_max) {printf ("storage space is full \ n"); return;} printf ("name:>"); scanf ("%s", (Pcon->dhb[pcon->count]). Name);p rintf ("sex:>"), scanf ("%s", pcon-> Dhb[pcon->count]). Sex);p rintf ("age:>"), scanf ("%d",& ((Pcon->dhb[pcon->count]). Age));p rintf (" Tele:> "), scanf ("%s ", (Pcon->dhb[pcon->count]). Tele);p rintf (" addr:> "); scanf ("%s ", pcon->dhb[pcon- >count]). addr);p con->count++;}



Delete a contact function

void Del_peo (PCon pCon)//delete function {char tmpname[name_max];int ret=0;printf ("Please enter the name you want to delete:>"); scanf ("%s", tmpname); ret= Find (Pcon,tmpname), if ( -1! = ret) {int j=ret;for (; j<pcon->count-1;j++) {pcon->dhb[j]=pcon->dhb[j+1];// Overwrite the data you want to delete with subsequent data}pcon->count--;//save one bit}else{printf ("The information you want to delete is not found \ n");}}



Find a Contact function segment


void Search_peo (PCon pCon)//Lookup function {char tmpname[name_max];int ret=0;printf ("Please enter the name you want to find:>"); scanf ("%s", tmpname); ret = Find (Pcon,tmpname), if (ret! =-1) {printf ("%s\t%s\t%d\t%s\t%s\t\n", Pcon->dhb[ret].name,pcon->dhb[ret]. SEX,PCON->DHB[RET].AGE,PCON->DHB[RET].TELE,PCON->DHB[RET].ADDR);} else{printf ("There is no information to look for \ n");}}



Modify a Contact function segment


void Modify_peo (PCon pCon)//Modify data function {char tmpname[name_max];int ret=0;printf ("Please enter the name you want to modify:>"); scanf ("%s", Tmpname) ; ret = Find (Pcon,tmpname), if ( -1! = ret)//Find modified name after re-assignment {printf ("name:>"); scanf ("%s", (Pcon->dhb[ret]). Name); printf ("sex:>"); scanf ("%s", (Pcon->dhb[ret]). Sex);p rintf ("age:>"); scanf ("%d",& ((Pcon->dhb[ret)) . Age);p rintf ("tele:>"), scanf ("%s", (Pcon->dhb[ret]). Tele);p rintf ("addr:>"); scanf ("%s", pcon->dhb[ RET]). addr);}}




Show Contact function segment


void Show_peo (PCon pCon)//display function {int i;for (i=0;i<pcon->count;i++)//display and print said contact information {printf ("%s\t%s\t%d\t%s\t%s\t\ n ", pcon->dhb[i].name,pcon->dhb[i].sex,pcon->dhb[i].age,pcon->dhb[i].tele,pcon->dhb[i].addr);}}


Clear the Contact function segment


void Clear_peo (PCon pCon)//Clear all contact information {pcon->count = 0;}



Use C language to implement an address book (add, delete, find, modify, display, empty function)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.