Advanced C language (implementing a simple address book)

content

1. Problem description

2. Function introduction

Second, the realization process

1. Create an address book

1. Create the header file contact.h

2. Define the structure type (struct peoInfo)

3. Define the address book structure type (struct contact)

4. #define defines identifier constants

5. Declaration of function implementation function

2. Create the source file test.c

1. Print menu

2. Enumeration method definition number

3. Implementation of the main file

3. Create the source file contact.c

1. Initialize the address book to 0 (implemented by the Initcontact function)

 2. Add address book contact information (AddContact function implementation)

4. Delete address book contact information (implemented by DelContact function)

5. Find address book contact information (SearchContact function implementation)

6. Modify contact information (ModifyContact function implementation)


1. Problem description

Write a simple address book in C language

2. Function introduction

Address book
1. The address book can store the information of 1000 people Information of
each person:
name, age, gender, phone number, address
2. Add person's information 3.
Delete person's
information 6. Sorting
of people's information

Second, the realization process

1. Create an address book

1. Create the header file contact.h

The header file contact.h is used to implement type definitions and function declarations.

2. Define the structure type (struct peoInfo)

A person's information includes name, age, gender, phone number, and address, so we need to define a structure type to express it completely.

Write a structure type struct peoInfo to store a person's information

code show as below:

/类型的定义
typedef struct peoInfo
{
	char name[MAX_NAME];//名字
	char sex[MAX_SEX];//性别
	int age;//年龄
	char tele[MAX_TELE];//电话
	char addr[MAX_ADDR];//地址
}peoInfo;

typedef (type rename), struct peoInfo renamed to peoInfo.

3. Define the address book structure type (struct contact)

Because we want to store the information of 1000 such people, we create an array data of structure type. Then create a variable sz to represent the subscript of this array. When the first person's information is stored in the data array, we put it in the position of the subscript sz. At this time, let our sz equal to 0, and store the information of the second person. At the same time, let our sz equal to 1, so that it is convenient for us to find the information of each person deposited by subscripting.

code show as below:

peoInfo data[MAX];//存放添加进来的人的信息
	int sz;//记录的是通讯录中有效信息的个数

The above code adds up to form our address book, which is obviously also a structure.

code show as below:

//通讯录
typedef struct contact
{
	peoInfo data[MAX];//存放添加进来的人的信息
	int sz;//记录的是通讯录中有效信息的个数
}contact;

4. #define defines identifier constants

In order to facilitate the subsequent adjustment of the address book size at any time, we define the identifier constants with #define for the numbers of the array one by one.

code show as below:

#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30
#define MAX 1000

5. Declaration of function implementation function

/初始化通讯录
void Initcontact(contact* cp);

//增加联系人信息
void AddContact(contact* cp);

//打印联系人信息
void PintContact(const contact* cp);

//删除联系人信息
void DelContact(contact* cp);

//查找联系人信息
void SearchContact(contact* cp);

//修改联系人信息
void ModifyContact(contact* cp);

2. Create the source file test.c

The source file test.c tests the module of the address book

After we have the address book, we need to write out the basic operation logic of the address book, choose to add contacts, choose to delete contacts, choose to modify contacts... choose the wrong one, choose again, choose to start over, and so on.

1. Print menu

code show as below:


void menu()
{
	printf("******************************\n");
	printf("*****1.add     2.del *********\n");
	printf("*****3.search  4.modify*******\n");
	printf("*****5.sort    6.print *******\n");
	printf("*****0.exit    ***************\n");
	printf("******************************\n");
}

2. Enumeration method definition number

code show as below:

enum Option
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SORT,
	PRINT
};

3. Implementation of the main file

code show as below

int main()
{
	int input = 0;
	//创建通讯录
	contact con;//通讯录
	//初始化通讯录
	Initcontact(&con);
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			//增加人的信息
			AddContact(&con);
			break;
		case DEL:
			//删除人的信息
			DelContact(&con);
			break;
		case SEARCH:
			//查找联系人的信息
			SearchContact(&con);
			break;
		case MODIFY:
			//修改联系人信息
			ModifyContact(&con);
			break;
		case SORT:
			break;
		case PRINT:
			//打印人的信息
			PintContact(&con);
			break;
		case EXIT:
			printf("退出登录\n");
			break;
		default:
			printf("选择错误,请重新选择\n");
			break;
		}

	} while (input);
	return 0;
}

Write a menu for users to choose the functions they want, and use the enumeration method to let programmers see the code at a glance, and at a glance, you can see what functions are to be implemented here.

3. Create the source file contact.c

Source file contact.c to achieve the function we want (function implementation)

1. Initialize the address book to 0 (implemented by the Initcontact function)

//初始化通讯录为0
void Initcontact(contact* cp)
{
	cp->sz = 0;//通讯录中人的数量先初始化为0
	memset(cp->data, 0, sizeof(cp->data));//数组内存中每个字节初始化为0

 2. Add address book contact information (AddContact function implementation)

First, determine whether the number of contacts in the address book is full. If it is full, it cannot be added. If it is not full, add contact information.

code show as below:

//增加联系人的信息
void AddContact(contact* cp)
{
	if (cp->sz == MAX)
	{
		printf("通讯录已满,无法添加\n");
		return;
	}
	//增加一个人的信息
	printf("请输入名字:>");
	scanf("%s", cp->data[cp->sz].name);
	printf("请输入年龄:>");
	scanf("%d", &(cp->data[cp->sz].age));
	printf("请输入性别:>");
	scanf("%s", cp->data[cp->sz].sex);
	printf("请输入电话:>");
	scanf("%s", cp->data[cp->sz].tele);
	printf("请输入地址:>");
	scanf("%s", cp->data[cp->sz].addr);
	cp->sz++;
	printf("增加成功\n");

}

3. Print address book contact information (PintContact function implementation)

code show as below:

//打印联系人的信息
void PintContact(const contact* cp)
{
	//打印标题
	int i = 0;
	printf("%-5s\t %-5s\t %-5s\t %-12s\t %-20s\n", "名字", "年龄", "性别", "电话", "地址");
	for (i = 0; i < cp->sz; i++)
	{
		printf("%-5s\t %-5d\t %-5s\t %-12s\t %-20s\n", 
			cp ->data[i].name,
			cp -> data[i].age,
			cp -> data[i].sex,
			cp -> data[i].tele,
			cp -> data[i].addr);
	}
}

After adding the contact information, we can print it out and take a look.

4. Delete address book contact information (implemented by DelContact function)

First, determine whether there is a contact in the address book. If not, there is no need to delete it. If there is, search for the name of the address book member according to the search function to delete the corresponding information.

code show as below:

static int  FindByName(contact* cp, char name[])
{
	int i = 0;
	for (i = 0; i < cp->sz; i++)
	{
		if (strcmp(cp->data[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}

//删除联系人信息
void DelContact(contact* cp)
{
	char name[MAX_NAME] = { 0 };
	if (cp->sz == 0)
	{
		printf("通讯录为空,无需删除\n");
		return;
	}
	//1.查找要删除的人
	//有没有
	printf("请输入要删除人的名字\n");
	scanf("%s", name);
	
	int pos = FindByName(cp, name);
	
		if (pos == -1)
		{
			printf("要删除的人不存在\n");
			return;
		}
		//2.删除
		int i = 0;
		for (i = pos; i < cp -> sz - 1; i++)
		{
			cp->data[i] = cp->data[i + 1];
		}
		cp->sz--;
		printf("删除成功\n");
		
}

5. Find address book contact information (SearchContact function implementation)

Search the address book member name according to the search function to delete the corresponding information.

code show as below

/查找联系人信息
void SearchContact(contact* cp)
{
	char name[MAX_NAME] = { 0 };
	printf("请输入要查找人的名字\n");
	scanf("%s", name);
	int pos = FindByName(cp, name);

	if (pos == -1)
	{
		printf("要查找的人不存在\n");
		return;
	}
	else 
	{
		printf("%-5s\t %-5s\t %-5s\t %-12s\t %-20s\n", "名字", "年龄", "性别", "电话", "地址");
		printf("%-5s\t %-5d\t %-5s\t %-12s\t %-20s\n",
			cp->data[pos].name,
			cp->data[pos].age,
			cp->data[pos].sex,
			cp->data[pos].tele,
			cp->data[pos].addr);

	}
}

6. Modify contact information (ModifyContact function implementation)

According to the search function to find the name of the address book member to modify the corresponding information.

code show as below:

//修改联系人的信息
void ModifyContact(contact* cp)
{
	char name[MAX_NAME] = { 0 };
	printf("请输入要修改人的名字\n");
	scanf("%s", name);
	int pos = FindByName(cp, name);

	if (pos == -1)
	{
		printf("要修改的人不存在\n");
		return;
	}
	else 
	{
		printf("请输入名字:>");
		scanf("%s", cp->data[pos].name);
		printf("请输入年龄:>");
		scanf("%d", &(cp->data[pos].age));
		printf("请输入性别:>");
		scanf("%s", cp->data[pos].sex);
		printf("请输入电话:>");
		scanf("%s", cp->data[pos].tele);
		printf("请输入地址:>");
		scanf("%s", cp->data[pos].addr);
		printf("修改成功\n");
		
	}
}

Partial function result graph:

 Summarize:

The above simply implements the function of adding, deleting, modifying and checking the address book, and writing a simple address book. If there are any problems with the above article, you are welcome to ask questions. I will learn and correct with an open mind. The most important thing is to make progress together. Grow up and learn to program well.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324088428&siteId=291194637