Super detailed C language implementation - address book

Table of contents

1. Introduction

2. Source code

test.c:

Contact.c:

Contact.h:

Code running results:

3. Start realizing

1.Basic framework:

2. Add contacts:

3. Display contact information:

4. Delete contact information:

5. View designated contact information:

6. Modify contact information:

Summarize



1. Introduction

I believe that everyone is familiar with the address book. Since you have come to refer to or study it, it means that you have basically mastered the basic syntax and some basic routines of C language. If you have any questions or suggestions later, you are welcome to discuss them in the comment area.

2. Source code

test.c:

#include "contact.h"
//测试通讯录

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

void test()
{
	int input = 0;
	Contact con;
	InitContact(&con);//初始化通讯录结构体
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			AddContact(&con);
			break;
		case 2:
			DelContact(&con);
			break;
		case 3:
			SearchContact(&con);
			break;
		case 4:
			ModifyContact(&con);
			break;
		case 5:
			ShowContact(&con);
			break;
		case 0:
			printf("退出成功!\n");
			break;
		default:
			printf("输入无效,请重新选择!\n");
			break;
		}
	} while (input);
}

int main()
{
	test();
	return 0;
}

Contact.c:

//函数的实现
#include "Contact.h"

//初始化通讯录结构体
void InitContact(Contact* p)
{
	memset(p->data, 0, sizeof(p->data));
	p->sz = 0;
}

//增加联系人信息
void AddContact(Contact* p)
{
	//判断通讯录是否塞满
	if (p->sz == Max)
	{
		printf("通讯录已满,无法添加\n");
		return;
	}
	printf("请输入要添加的联系人的姓名:>");
	scanf("%s", p->data[p->sz].name);
	printf("请输入要添加的联系人的性别:>");
	scanf("%s", p->data[p->sz].sex);
	printf("请输入要添加的联系人的电话号码:>");
	scanf("%s", p->data[p->sz].tala);
	printf("请输入要添加的联系人的地址:>");
	scanf("%s", p->data[p->sz].addr);
	p->sz++;
	printf("添加联系人成功!\n");
}


//展示联系人信息
void ShowContact(const Contact* p)
{
	//显示标题
	printf("%-5s\t%-5s\t%-12s\t%-10s\n", "姓名", "性别", "电话号码", "地址");
	for (int i = 0; i < p->sz; i++)
	{
		printf("%-5s\t%-5s\t%-12s\t%-10s\n",
			p->data[i].name,
			p->data[i].sex,
			p->data[i].tala,
			p->data[i].addr);
	}
}


//查找联系人
int Findname(Contact* p, char name1[])
{
	int i = 0;
	for (i = 0; i < p->sz; i++)
	{
		if (strcmp(p->data[i].name, name1) == 0)
		{
			return i;
		}
	}
	//没找到
	return -1;
}


//删除指定联系人信息
void DelContact(Contact* p)
{
	if (p->sz == 0)
	{
		printf("通讯录为空!\n");
		return;
	}
	char name1[Max_name] = { 0 };
	printf("请输入你要删除的联系人姓名:>");
	scanf("%s", name1);
	int del = Findname(p, name1);
	if (del == -1)
	{
		printf("该通讯录不存在这个人!\n");
		return;
	}
	int i = 0;
	for (i = del; i < p->sz - 1; i++)
	{
		p->data[i] = p->data[i + 1];
	}
	p->sz--;
	printf("删除联系人成功!\n");
}

//查找指定联系人
void SearchContact(const Contact* p)
{
	char name[Max_name] = { 0 };
	printf("请输入你要查看的联系人的姓名:>");
	while (getchar()!= '\n');
	scanf("%s", name);
	int i = Findname(p, name);
	if (i == -1)
	{
		printf("该通讯录不存在该联系人\n");
	}
	else
	{
		//显示标题
		printf("%-5s\t%-5s\t%-12s\t%-10s\n", "姓名", "性别", "电话号码", "地址");
		printf("%-5s\t%-5s\t%-12s\t%-10s\n",
				p->data[i].name,
				p->data[i].sex,
				p->data[i].tala,
				p->data[i].addr);
	}
}

//修改联系人信息
void ModifyContact(Contact* p)
{
	char name[Max_name] = { 0 };
	printf("请输入你要修改的联系人的姓名:>");
	while (getchar() != '\n');
	scanf("%s", name);
	int i = Findname(p, name);
	if (i == -1)
	{
		printf("该通讯录不存在该联系人\n");
	}
	else
	{
		printf("请重新输入该联系人的姓名:>");
		scanf("%s", p->data[i].name);
		printf("请重新输入该联系人的性别:>");
		scanf("%s", p->data[i].sex);
		printf("请重新输入该联系人的电话号码:>");
		scanf("%s", p->data[i].tala);
		printf("请重新输入该联系人的地址:>");
		scanf("%s", p->data[i].addr);
	}
	printf("修改成功!\n");
}

Contact.h:

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#define Max 100
#define Max_name 20
#define Max_sex 5
#define Max_tele 12
#define Max_addr 30


//存放函数的类型和声明
#include<string.h>
#include<stdio.h>

typedef struct PeoInfo
{
	char name[Max_name];
	char sex[Max_sex];
	char tala[Max_tele];
	char addr[Max_addr];

}PeoInfo;

//通讯录结构体
typedef struct Contact
{
	PeoInfo data[Max];
	int sz;
}Contact;


//函数声明

//初始化通讯录结构体
void InitContact(Contact* p);

//增加联系人信息
void AddContact(Contact* p);

//展示联系人信息
void ShowContact(Contact* p);

//删除联系人信息
void DelContact(Contact* p);

//查找指定联系人信息
void SearchContact(const Contact* p);

//修改联系人信息
void ModifyContact(Contact* p);

Code running results:

3. Start realizing

1.Basic framework:

This implementation of the address book uses a multi-file method . Since everyone has learned about the address book, almost all of them should be exposed to multi-file operations. The so-called multi-file operations are:

①: The function declaration, define macro definition, structure declaration, etc. are placed in the .h file. (contact.h)

②: The definition of the function is placed in the .c file. (contact.c)

③: The main function and some basic frameworks can be placed separately in another .c file. (test.c).

Since it is an address book, we need a structure to save the people in the address book and a structure to save the personnel information . The declaration is placed in the contact.h file:

①: We put this personnel information in the structure PerInfo, which includes name, gender, phone number, and address. You can add other information according to your own ideas.

②: Then the second structure Contact is used to save contacts. Here we take the upper limit of 100 contacts as an example, so members have a PerInfo structure array, and then there is an integer variable sz used to record the existing number of people, increase A person, sz+1; delete a person, sz-1.

code show as below:

#pragma once
#define Max 100
#define Max_name 20
#define Max_sex 5
#define Max_tele 12
#define Max_addr 30

typedef struct PeoInfo
{
	char name[Max_name];
	char sex[Max_sex];
	char tala[Max_tele];
	char addr[Max_addr];

}PeoInfo;

//通讯录结构体
typedef struct Contact
{
	PeoInfo data[Max];
	int sz;
}Contact;

Then this framework still uses the do while loop to cover the switch case statement to facilitate the selection of different functions. Here we put it in the test.c file:

#include "contact.h"
//测试通讯录

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

void test()
{
	int input = 0;
	Contact con;
	InitContact(&con);//初始化通讯录结构体
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			break;
		case 2:
			break;
		case 3:
			break;
		case 4:
			break;
		case 5:
			break;
		case 0:
			break;
		default:
			break;
		}
	} while (input);
}

int main()
{
	test();
    return 0;
}

①: Because the relevant header file is in the contact.h file, it does not matter whether it is in the contact.c file or in test. c file, we all need to include our own header file contact.h.

②: Then for the sake of beauty, write a menu function menu, which you can set by yourself.

③: First we must have a Contact structure variable con, and then create the InitContact function to simply initialize its members:

The array here has 100 spaces, and loop initialization is too troublesome, so we use a library function memset . For detailed introduction, please refer to http://t.csdn.cn/wu8iX

④: Most people should have used do while to set switch case, because in this way we can choose according to the menu function, enter different numbers to enter different function functions, and after the function ends, we can input in a loop. When 0 is entered, do while ends , that is, the entire system ends.

2. Add contacts:

After the basic framework is built, we can implement related functions. Here, the editor recommends that when writing similar programs, it is best to implement them one function at a time, so as to facilitate debugging and correction.

First, to implement the add operation, we create the AddContact function to implement the add operation. The function source code is as follows:

//增加联系人信息
void AddContact(Contact* p)
{
	//判断通讯录是否塞满
	if (p->sz == Max)
	{
		printf("通讯录已满,无法添加\n");
		return;
	}
	printf("请输入要添加的联系人的姓名:>");
	scanf("%s", p->data[p->sz].name);
	printf("请输入要添加的联系人的性别:>");
	scanf("%s", p->data[p->sz].sex);
	printf("请输入要添加的联系人的电话号码:>");
	scanf("%s", p->data[p->sz].tala);
	printf("请输入要添加的联系人的地址:>");
	scanf("%s", p->data[p->sz].addr);
	p->sz++;
	printf("添加联系人成功!\n");
}

①: Since we are adding, we first need to determine whether the address book is full. If it is not full, we will continue to add, so we start with an if statement. When sz==Max, it is full.

②: Then you only need to input data into the corresponding positions in sequence, because sz represents the number of existing people in the address book, which is 0 at the beginning, and the subscript of the array also starts from 0, so directly use sz as the array subscript to enter the data. Yes, after the addition is successful, sz will increase by 1, which means the number of people +1, and can also be used as an array subscript when adding for the second time.

③: Here you need to pay attention and understand the corresponding relationship between each structure variable and member as well as the '.' operator and the '->' operator.

3. Display contact information:

In order to facilitate testing whether the code is incorrect, we can implement the display operation first after implementing the adding operation. This way we can check whether the adding operation is incorrect and every time a subsequent function is completed, we can use the display function to check.

We create the function ShowContact function to implement the display function. The source code is as follows:


//展示联系人信息
void ShowContact(Contact* p)
{
	//显示标题
	printf("%-5s\t%-5s\t%-12s\t%-10s\n", "姓名", "性别", "电话号码", "地址");
    //显示信息
	for (int i = 0; i < p->sz; i++)
	{
		printf("%-5s\t%-5s\t%-12s\t%-10s\n",
			p->data[i].name,
			p->data[i].sex,
			p->data[i].tala,
			p->data[i].addr);
	}
}

①: We first print the title for easy viewing.

②: We only need to use a for loop, and then use the sum structure to find the corresponding value and print it out.

③: What is worth noting here is the typesetting issue. Some indentations and typesetting affect the appearance. You can set it yourself, or you can refer to the appeal source code.

4. Delete contact information:

We create the DelContact function to implement the deletion operation. The source code is as follows:

//删除指定联系人信息
void DelContact(Contact* p)
{
	if (p->sz == 0)
	{
		printf("通讯录为空!\n");
		return;
	}
	char name1[Max_name] = { 0 };
	printf("请输入你要删除的联系人姓名:>");
	scanf("%s", name1);
	int del = Findname(p, name1);
	if (del == -1)
	{
		printf("该通讯录不存在这个人!\n");
		return;
	}
	int i = 0;
	for (i = del; i < p->sz - 1; i++)
	{
		p->data[i] = p->data[i + 1];
	}
	p->sz--;
	printf("删除联系人成功!\n");
}

①: Since it is deletion, we should first determine whether the address book is empty;

②: Then create a temporary array char name1[] for the user to enter the name of the contact to be deleted;

③: Then we need to determine whether this person exists in the address book, so here we create the Findname function to find the specified contact. The source code is as follows:
 

//查找联系人
int Findname(Contact* p, char name1[])
{
	int i = 0;
	for (i = 0; i < p->sz; i++)
	{
		if (strcmp(p->data[i].name, name1))
		{
			return i;
		}
	}
	//没找到
	return -1;
}

What is worth noting here is the if statement in the loop. Many people like to use "==" in the judgment condition, but we must pay attention to the fact that the name is a string. Here we are comparing two strings, so we cannot use "==" directly. ", so the string function strcmp is used here. For specific usage, please refer to: http://t.csdn.cn/qD0LQ

④: When not found, return directly after prompting; if found, delete operation is performed;

Because we are using an array, the deletion operation is simple. We only need to find the position del of the contact first, and then overwrite the content behind the position in sequence. Finally, the existing number of people sz can be decremented by one, so here we use to a for loop.

5. View designated contact information:

We create the SearchContact function to implement this operation. The source code is as follows:

//查找指定联系人
void SearchContact(const Contact* p)
{
	char name[Max_name] = { 0 };
	printf("请输入你要查看的联系人的姓名:>");
	while (getchar()!= '\n');
	scanf("%s", name);
	int i = Findname(p, name);
	if (i == -1)
	{
		printf("该通讯录不存在该联系人\n");
	}
	else
	{
		//显示标题
		printf("%-5s\t%-5s\t%-12s\t%-10s\n", "姓名", "性别", "电话号码", "地址");
		printf("%-5s\t%-5s\t%-12s\t%-10s\n",
				p->data[i].name,
				p->data[i].sex,
				p->data[i].tala,
				p->data[i].addr);
	}
}

①: First create a temporary array to facilitate the user to enter the name of the specified contact, and then use a while loop to remove excess content in the buffer so that scanf can read it normally;

②: Then call the Findname function and perform operations based on the return value

If -1 is returned, the contact does not exist and is returned directly;

If it is not -1, it means it is found, and then we can print out the information of the contact.

6. Modify contact information:

We create the ModifyContact function to implement this operation. The source code is as follows:

//修改联系人信息
void ModifyContact(Contact* p)
{
	char name[Max_name] = { 0 };
	printf("请输入你要修改的联系人的姓名:>");
	while (getchar() != '\n');
	scanf("%s", name);
	int i = Findname(p, name);
	if (i == -1)
	{
		printf("该通讯录不存在该联系人\n");
	}
	else
	{
		printf("请重新输入该联系人的姓名:>");
		scanf("%s", p->data[i].name);
		printf("请重新输入该联系人的性别:>");
		scanf("%s", p->data[i].sex);
		printf("请重新输入该联系人的电话号码:>");
		scanf("%s", p->data[i].tala);
		printf("请重新输入该联系人的地址:>");
		scanf("%s", p->data[i].addr);
	}
	printf("修改成功!\n");
}

①: The previous step is the same as searching for contacts. The user needs to first enter the name of the contact to be modified, and then determine whether it exists;

②: If it exists, just return the subscript, and then re-enter the data for the subscript information, which is relatively simple.

Summarize

This time, the editor simply implements an address book to facilitate everyone to find ideas and methods. There are many loopholes and improvements that you can implement by yourself. This knowledge ends here. I hope it will be helpful to everyone!

Guess you like

Origin blog.csdn.net/hffh123/article/details/133252675