(c language) self-write address Book

Source: Internet
Author: User

Problem Description:
Implement an address book;
The Address Book can be used to store personal information, and everyone's information includes:

Name, sex, age, telephone number, address. Realize increase, delete, check, find, empty these several functions.

Program Analysis:

(1) Basic idea: After learning the structure, we must through continuous application to truly grasp. This address book is a small project built on two structures as a framework. As shown below:

typedef struct PEO
{
Char Name[name_len];
int age;
Char Sex[sex_len];
Char Tele[tele_len];
Char Addr[addr_len];
}PEO,*PPEO;

This type of struct is used to store information about a contact as a model. Only a model has been defined, and no specific contact information has been defined.

typedef struct CONTACT
{
PEO DHB[MAX_PEO]; Store person's information
int count; Record number of valid
}con,*pcon;

The above structure type is a structure that stores each contact. With both types of structures, the next step is to access or assign the structure to complete the various functions of the Address Book.

(2) Functional function: After knowing the planning idea of this project, the main task is to write some functions, such as adding, deleting, checking, changing, emptying and displaying, so as to realize the writing of this address book. The following are some of the things you need to be aware of in these functions:

A. When increasing the number of contacts, we must not forget the count++ of effective contacts;

B. The contact you want to delete is based on finding the contact, starting with the contact, and assigning the latter to the previous person in turn. Finally, will count-1;

C. Modifying the information of the contact person is also based on finding the contact person;
D. When you write out all of your contacts this function, the easiest thing to think about is to assign the number of valid contacts to the count of 0;

(3) Establishment of the project: in the establishment of the project should be noted that the function of the declaration and some macro definitions written in the header file contact.h, the definition of these functions in a single. C (. cpp) file. Create a. C (. cpp) file to call these functions. So this address book can be successfully completed.


The code is as follows:

/***************************************************************************************/

/* header file <span style= "Background-color:rgb (255, 0, 0);" >contact.h</span>*/#ifndef __contact_h__ #define __CONTACT_H__ #include <stdio.h> #define Name_len 20 # Define Sex_len 5 #define Tele_len #define Addr_len #define MAX_PEO 1000/** define a struct type store everyone's information **/typedef struct PE		  
	o {char Name[name_len];
	int age;
	Char Sex[sex_len];
	Char Tele[tele_len];
Char Addr[addr_len];	   }PEO,*PPEO;
	PEO represents the type referred to above, PPEO refers to this type of struct pointer/* Defines a telephone book that allows all persons (no more than 1000) to be able to deposit the information inside/typedef struct CONTACT {PEO DHB[MAX_PEO];//store person's information			int count;

Record valid number}con,*pcon;	/** declares the following several functions **/void initcontact (Pcon contact);	Initialize the number of valid contacts bool Addcontact (Pcon contact);	 Increase contact person void Showcontact (Pcon contacts);				 Show all contacts void Print_menu ();   Print menu int searchcontact (Pcon contact);	Find contacts void DeleteContact (Pcon contact);   Delete contact void Clearcontact (Pcon contacts);  Empty all contacts bool Modifycontact (Pcon contact); Modify the contact information #endif/* 
********************************************************************************************** In this <span style= "Background-color:rgb (255, 0, 0);" The >contact.c</span> file will define these functions **/#include "contact.h" #include <string.h> void Initcontact (Pcon contact    ) {contact->count = 0;  <span style= "font-family:arial, Helvetica, Sans-serif;" >/* the number of effective contacts into 0, can also be directly defined as 0,</span><span style= "font-family:arial, Helvetica, Sans-serif;" > without having to encapsulate one such function to achieve the purpose of the contact initialization */</span> 
BOOL Addcontact (Pcon Contact) {printf ("Name:");/prompt for the name to be added scanf ("%s", contact->dhb[contact->count].name); /arrows access to the space where the name is stored in the structure, and assigns the content to name printf ("Age:"); <span style= "font-family:arial, Helvetica, Sans-serif;" >//prompts for the age to be added </span> scanf ("%d",& (contact->dhb[contact->count].age));//<span style= " Font-family:arial, Helvetica, Sans-serif; " > Use arrows to access the space where the name resides in the structure and assign the content to age</span> printf ("Sex:"); <span style= "font-family:arial, Helvetica, Sans-serif; " >//prompts for the gender </span> scanf ("%s", contact->dhb[contact->count].sex) to be added;//<span style= "font-family: Arial, Helvetica, Sans-serif; > Use arrows to access the space where the name is stored in the structure and assign the content to sex</span> printf ("Tele:"); <span style= "font-family:arial, Helvetica, Sans-serif;" >//prompts to enter the name to add </span><span style= "font-family:arial, Helvetica, Sans-serif;" > </span> scanf ("%s", Contact->dhb[contact->count].tele);//<span style= "font-family:arial, Helvetica, Sans-serif;"> Access the space of the name in the structure by arrows, and assign the content to tele</span> printf (" addr: "); <span style=" font-family:arial, Helvetica, Sans-serif; " >//Prompt for the name you want to add </span> scanf ("%s", contact->dhb[contact->count].addr);//<span style= "font-family: Arial, Helvetica, Sans-serif;
> Use arrows to access the space in the structure to hold the name, and assign the content to addr</span> contact->count++;//so that the number of valid contacts is added return true; } void Showcontact (Pcon contact) {int i = 0;//defines a loop variable and initializes it to 0 for (i = 0;i<contact->count;i++)//Use a For loop to put all the letters of all contacts All print out {printf ("%12s", contact->dhb[i].name);/on the right 12-bit alignment, output contact name printf ("%12d", Contact->dhb[i].age);< Span style= "font-family:arial, Helvetica, Sans-serif;" >//is aligned on the right 12-bit, outputting the age of the contact </span> printf ("%12s", contact->dhb[i].sex); <span style= "font-family:arial, Helvetica, Sans-serif; " >//is aligned on the right 12-bit, outputting the contact's sex </span> printf ("%12s", Contact->dhb[i].tele); <span style= "font-family:arial, Helvetica, Sans-serif; " >//is aligned to the right 12-bit, outputting the contact's phone number </span> printf ("%12s\n", contact-&GT;DHB[I].ADDR); <span style= "font-family:arial, Helvetica, Sans-serif;" >//right 12-bit alignment, output contact's address </span>} int searchcontact (Pcon contacts) {Char name[name_len];//defines an array of strings printf ("Name:
	\ n "); scanf ("%s", name);/Enter the name of the contact you want to find int ret = 0;//define the loop variable for (ret = 0;ret<contact->count;ret++)//To judge one by one {if strcmp ( name,contact->dhb[ret].name) = = 0)//To determine whether the contact name of the query is consistent with the contact name in the phone book {return ret;//if consistent, returns the subscript}} return-1 of the contact; If the lookup contact does not exist, return-1} void DeleteContact (Pcon contacts) {int ret = searchcontact (contact);/call "Find a contact" function and use RET to receive the connection found The person's subscript while (ret<contact->count-1) {Contact->dhb[ret] = contact->dhb[ret+1];//This idea: to cover the position of the RET position with the ret+1 location,
	Again speak ret++, so that RET refers to the next, the same idea, will be a forward one to cover the previous position, ret back, from behind, so that it reached the original RET position of the contact to delete the function ret++; (contact->count)--;//deleted, the effective contact number -1} void Clearcontact (Pcon contact) {Contact->count = 0;//empty Contact, imminent number of effective contacts 0} bool Modifycontact (Pcon contacts) {int ret = searchcontact (contact); <span style= "font-fAmily:arial, Helvetica, Sans-serif; "
	  >//invokes the "Find contact" function and receives the subscript </span> printf ("Name:") of the contact found by the RET; scanf ("%s", contact->dhb[ret].name); <span style= "font-family:arial, Helvetica, Sans-serif;"
	  >//enter the name to be modified </span> printf ("Age:"); scanf ("%d",& (contact->dhb[ret].age)); <span style= "font-family:arial, Helvetica, Sans-serif;"
	  >//Enter the age to be modified </span> printf ("Sex:"); scanf ("%s", contact->dhb[ret].sex); <span style= "font-family:arial, Helvetica, Sans-serif;"  
	  >//enter the gender to be modified </span> printf ("Tele:"); scanf ("%s", Contact->dhb[ret].tele); <span style= "font-family:arial, Helvetica, Sans-serif;"
	  >//Enter the phone number </span> printf ("addr:") to be modified; scanf ("%s", contact->dhb[ret].addr); <span style= "font-family:arial, Helvetica, Sans-serif;" >//Enter the address to be modified </span><span style= "font-family:arial, Helvetica, Sans-serif;"
> </span> return true; } void Print_menu () {printf ("***************************\n")//Print This menu, contains some hint information printf ("*[1]add: [2]show: *\n");
	printf ("*[3]search: [4]delete:*\n");
	printf ("*[5]clear: [6]modify:*\n");
printf ("***************************\n");

 }
/************************************************************************** <span style= " Background-color:rgb (255, 0, 0);  >main.c</span> function call/#include "contact.h" int main () {Con contact;
	Define the structure body contact initcontact (&contact);//make the number of effective contacts initialized int input = 1;

	int ret;
		while (input)//use while loop allows the program to loop through the test {print_menu ();
		printf ("Please chose input:\n");
		scanf ("%d", &input);  switch (input) {case 1:addcontact (&contact);
		Increase contact person break;
		Case 2:showcontact (&contact);//show contact break; 
			Case 3:ret = searchcontact (&contact);//Find contact if (ret!=-1) {printf ("found it!\n");//ret did not return 1, description found}
		else {printf ("not exist!\n");//Otherwise not found} break;
		Case 4:deletecontact (&contact);//delete contact person break;
		Case 5:clearcontact (&contact);//empty contact break;		
		Case 6:modifycontact (&contact);//modify contact break;	
		Default:break;
} return 0; }






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.