当前位置:网站首页>[C language] Address book "Static Memory Version"

[C language] Address book "Static Memory Version"

2022-08-10 01:32:00 Genze

Hello謓泽多多指教

HY点赞收藏️留言​

通讯录目录   

前言  

模块化编程 

Logic realization of address book 

实现通讯录の步骤

创建颜色函数color() 

菜单界面函数menu()

Definition of address book

枚举类型

初始化通讯录

Add address book information 

打印通讯录的信息 

删除通讯录的信息 

Find information from contacts 

Modify the information of the specified address book person

Check the information of people in the address book

模块化代码实现

test.c

address_book.c 

address_book.h 

最后



前言  

This blog will show you how to implement a program code for an address book by yourself,Before talking about the code, let's talk about what modular programming is.


模块化编程 

传统方式编程→所有的函数均放在main.c里,若使用的模块比较多,则一个文件内会有很多的代码,不利于代码的组织和管理,而且很影响编程者的思路.

模块化编程:把各个模块的代码放在不同的.c文件里,在.h文件里提供外部可调用函数的声明,其它.c文件想使用其中的代码时,只需要#include "XXX.h"文件即可.使用模块化编程可极大的提高代码的可阅读性、可维护性、可移植性等.

传统方式编程:所有的函数均放在main.c里,若使用的模块比较多,则一个文件内会有很多的代码,不利于代码的组织和管理,而且很影响编程者的思路.

模块化编程:把各个模块的代码放在不同的.c文件里,在.h文件里提供外部可调用函数的声明,其它.c文件想使用其中的代码时,只需要#include "XXX.h"文件即可.使用模块化编程可极大的提高代码的可阅读性、可维护性、可移植性等!

总的来说就是:当你代码比较多的时候,就可以采用模块化编程来完成这个程序


Logic realization of address book 

We stipulated in total in the address book can hold1000个人的信息.

Each person's information has→名字、年龄、性别、电话、地址.

增加人的信息,put it in the address book.

Delete the information specified in the address book.

Modify the information of the specified address book person.

Find the information of a specified address book person.

Check the information of people in the address book.

Print the information of the people in the address book.

Sign out when I don't want to use this address book.


实现通讯录の步骤

Then the next step is to implement an implementation of the address book program.;


创建颜色函数color() 

前景色颜色的对应值↓

0=黑色                8=灰色  
1=蓝色                9=淡蓝色        十六进制                                  
2=绿色                10=淡绿色        A          
3=湖蓝色              11=淡浅绿色      B 
4=红色                12=淡红色        C  
5=紫色                13=淡紫色        D          
6=黄色                14=淡黄色        E         
7=白色                15=亮白色        F

color()创建颜色函数如下↓ 

void color(short x)	
{
	if (x >= 0 && x <= 15)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);	 
	else
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}

使用颜色函数的好处实际上无非就是让程序运行看上去更加美观、鲜明,实际上没什么实际作用,这点是我们要知道的. 

这里的STD_OUTPUT_HANDLE需要引头文件#include<Windows.h>,才可以进行使用.


菜单界面函数menu()

菜单界面函数实际上就像是我们的一个界面,就好比是游戏的界面目录,餐馆当中的菜单.一样的道理.这个是库函数就有的我们只需要直接引用下即可.示例代码如下↓

void menu()
{
	color(0);//黑色
	system("cls");//清屏
	color(7);//白色
	printf("■■■■■■■■→通讯录v1.0←■■■■■■■■\n");
	printf("*****→1.increase ■ 2.delete←*****\n");
	printf("*****→3.find     ■ 4.Revise←*****\n");
	printf("*****→5.Check    ■ 6.Print ←*****\n");
	printf("*****→0.Exit     ■         ←*****\n");
	printf("■■■■■■■■→通讯录v1.0←■■■■■■■■\n");
}

The functions corresponding to the numbers on the interface\(@^0^@)/

1.increase → 增加人的信息,put it in the address book.

2.delete → Delete the information specified in the address book.

3.find → Find the information of a specified address book person.

4.Revise → Modify the information of the specified address book person.

5.Check → Check the information of people in the address book.

6.Print → Print the information of the people in the address book.

0.Exit → Sign out when I don't want to use this address book.

注→在这里用到了一个system("cls"); 达到了一个清屏的效果,只有加了这个,你才可以让cmd中的界面全部为黑色.因为我们还在这个清屏指令上+color(0); 这个代表的是,黑色.


Definition of address book

在这里我们要实现↓

We stipulated in total in the address book can hold1000个人的信息.

Each person's information has→名字、年龄、性别、电话、地址.

So we can create a directory structure types of information to be defined.

#define Max_name 5
#define Max_age 100
#define Max_genger 3
#define Max_address 20
#define Max_telephone 20

typedef struct information
{
	//名字、年龄、性别、电话、地址.
	char name[Max_name];
	int age[Max_age];
	char genger[Max_genger];
	int telephone[Max_telephone];
	char address[Max_address];
}information;

This is each person's information separately→名字、年龄、性别、电话、地址. 

#define num 1000
typedef struct Contact
{
	//Here is a structure nested
	information date[num];//Contains the information added
	int sz;//记录当前通讯录有效信息的个数
}Conta;

We stipulated in total in the address book can hold1000个人的信息. 

In the above code we use a lot of #define 宏定义在这里的好处:

  1. 方便程序的修改,不用对整个程序进行修改,只需对宏定义上进行修改.
  2. 提高程序的运行效率,更加方便模块化.
  3. We just need to change the value of the macro definition,It can achieve the effect of different numbers of people and is easy to modify.

枚举类型

Here we can use enumeration type constants to switch case use in sentences.Because the default value of an enumeration is0的,It can correspond one-to-one with the values ​​in the above interface,放在caseThe code in the constant expression will be more readable.

enum Number
{
	Exit,
	Increase,
	DeLete,
	Find,
	Revise,
	Check,
	Print,
};

It is recommended to capitalize values ​​in enum constants. 那么此时我们的caseStatement of the value of the constant expression can put enumerated constants of these values.就像下面这样,示例代码如下↓

switch (input)
{
case Exit:printf("退出通讯录v1.0\n");
	break;
case Increase:
	break;
case DeLete:
	break;
case Find:
	break;
case Revise:
	break;
case Check:
	break;
case Print:
	break;
default:printf("The number you entered was not found,请重新输入~");
}


初始化通讯录

Before adding address book information,We first need to initialize the information of the address book.

void InitContact(Contact* pc)
{
	pc->sz = 0;//sz初始化
	//memest() - 内存设置
	memset(pc->date, 0, sizeof(pc->date)); 
}

如果不知道memset()What does this memory function mean?,可以去查下. 

有的人可能会有疑问,Here we can directly initialize the structure type of our address book0,Why do you need this.因为这样的话,Can face more complex problems,It's possible because we usememset()this memory function.


Add address book information 

After initializing the address book,Then we can realize our increase in the information of the address book..ヽ(*゚▽゚)ノ

void Add_Contact(Contact* pc)
{
	if (pc->sz == num)
	{
		printf("Contacts are full\n");
		return;
	}
	//增加一个人的信息
	printf("\n");
	printf("Please enter the name of the adder->:");
	scanf("%s", pc->date[pc->sz].name);//注意→数组名是首元素地址,So no need to take the address.

	printf("Please enter the age of the adder->:");
	scanf("%s", pc->date[pc->sz].age);

	printf("Please enter the increase a person's sex->:");
	scanf("%s", pc->date[pc->sz].genger);

	printf("Please enter the phone number of the person to be added->:");
	scanf("%s", pc->date[pc->sz].telephone);

	printf("Please enter the address of the adder->:");
	scanf("%s", pc->date[pc->sz].address);
	//成功~
	pc->sz++;
	printf("*恭喜你~添加信息成功*\n");
	printf("\n");
}

There are two situations here,One is in our address book1000People are already full,Another is that there is no full,Can continue to add.If it is full, directlyreturn;返回即可,no input.

示例→添加信息如下图所示↓


打印通讯录的信息 

Then suppose we add the information of the address book and want to print it out,This time will have to print our address book information.如下代码所示↓

void Print_Contact(const Contact* pc)
{
	int i = 0;
	//print title bar information
	printf("|-------------------------------------------------------|\n");
	printf("|%-3s\t%-3s\t%-3s\t%-11s\t%-10s\t|\n", "名字", "年龄", "性别", "电话", "地址");

	for (i = 0; i < pc->sz; i++)
	{
		printf("|%-3s\t%-3s\t%-3s\t%-11s\t%-10s\t|\n", pc->date[i].name,
			pc->date[i].age,
			pc->date[i].genger,
			pc->date[i].telephone,
			pc->date[i].address);
	}
}

 - 是左对齐,%3s的数字3是打印空3grid position.

In this case, suppose we want to enter the information of a group of contacts,Then let's print it out and see if it works.

successfully printed(^∀^●)ノシ 


删除通讯录的信息 

Deleting the information of the address book is actually assuming that we have entered a group of address book information,If we don't want it, then we can delete it,In this way, when we print the information in the address book, there will be no longer the group of information we entered..

void DeLete_Contact(Contact* pc)
{
	char name[Max_name] = { 0 };
	//When the address book is empty
	if (pc->sz == 0)
	{
		printf("There is no information in the address book\n");
		return;
	}
	printf("请输入你要删除的名字->:\n");
	scanf("%s", name);
	//查找要删除的人:有/没有
	int ret = FindByname(pc, name);
	if (ret == -1)
	{
		printf("No information found for the current person\n");
		return;
	}
	//删除
	int i = 0;
	for (i = ret; i < pc->sz - 1; i++)
	{
		pc->date[i] = pc->date[i + 1];
	}
	pc->sz--;//Because we delete the successful subscript to reduce1
	printf("*恭喜你~删除信息成功*\n");
	printf("\n");
}

Here we also create a function FindByname(),Its function is actually to help us find out whether the information of the people in the address book exists or not.,In the form of input name to find address book information.

static int FindByname(Contact* pc, char name[])
{
	int i = 0;
	//用for循环进行遍历
	for (i = 0; i < pc->sz; i++)
	{
		//strcmp()比较字符串
		if (strcmp(pc->date[i].name, name) == 0)
		{
			return i;//返回下标
		}
	}
	return -1;
}

Here we use static local variables static 修饰函数↓

function is static (static) Decorated functions are also not available in other source files,Can only be used in source files !

Then we run the program to see if it achieves an effect we want to achieve.

The result of running the program proves that the program is successfulヽ(*゚▽゚)ノ


Find information from contacts 

Query the address book and what we implemented above FindByname() 都是很类似的.

void Find_Contact(Contact* pc)
{
	char name[Max_name] = { 0 };
	printf("请输入你要删除的名字->:");
	scanf("%s", name);
	int ret = FindByname(pc, name);
	if (ret == -1)
	{
		printf("The current person's name was not found\n");
		return;
	}
	else
	{
		printf("|-------------------------------------------------------|\n");
		printf("|%-3s\t%-3s\t%-3s\t%-11s\t%-10s\t|\n", "名字", "年龄", "性别", "电话", "地址");

		printf("|%-3s\t%-3s\t%-3s\t%-11s\t%-10s\t|\n", 
			pc->date[ret].name,
			pc->date[ret].age,
			pc->date[ret].genger,
			pc->date[ret].telephone,
			pc->date[ret].address);
	}
}

let's find out"张三"的信息. 

Did the search succeed?(^∀^●)ノシ 


Modify the information of the specified address book person

So suppose I enter a person's information,But what if I want to change it to another person's information later?.如下代码所示↓ 

void Revise_Contact(Contact* pc)
{
	char name[Max_name] = { 0 };
	printf("Please enter the name of the person you want to modify->:");
	scanf("%s", name);
	int ret = FindByname(pc, name);
	if (ret == -1)
	{
		printf("Information about current modifier does not exist\n");
		return;
	}
	else
	{
		printf("请输入修改人的名字->:");
		scanf("%s", pc->date[ret].name);//注意→数组名是首元素地址,So no need to take the address.

		printf("请输入修改人的年龄->:");
		scanf("%s", pc->date[ret].age);

		printf("Please enter the gender of the person modifying->:");
		scanf("%s", pc->date[ret].genger);

		printf("请输入修改人的电话->:");
		scanf("%s", pc->date[ret].telephone);

		printf("请输入修改人的地址->:");
		scanf("%s", pc->date[ret].address);
		printf("恭喜你,修改成功~\n");
	}
}

So let's take a look at the modified results. 

The modification result was successfulヽ(*゚▽゚)ノ 


Check the information of people in the address book

Here we check the information of the people in the address book is actually sorted by name,用qsort()The function sorts by comparing the first letter.

int sort_name_max(const void* e1, const void* e2)
{
	return (strcmp(((struct Contact*)e1)->date->name, ((struct Contact*)e2)->date->name));
}

void Check_Contact(Contact* pc)
{
	//qosrt()Function first letter sorting
	qsort(pc->date, pc->sz, sizeof(pc->date[0]), sort_name_max);
	printf("|%-3s\t%-3s\t%-3s\t%-11s\t%-10s\t|\n", "名字", "年龄", "性别", "电话", "地址");
	int i;
	for (i = 0; i < pc->sz; i++)
	{
		printf("|%-3s\t%-3s\t%-3s\t%-11s\t%-10s\t|\n", pc->date[i].name,
			pc->date[i].age,
			pc->date[i].genger,
			pc->date[i].telephone,
			pc->date[i].address);
	}
}

Let's enter three sets of information to see,Is it possible to checkInformation about people in the address book. 

从上述的运行结果可以看出,The compiler checked.根据ASCllCode values ​​are sorted from smallest to largest.


模块化代码实现

test.c

示例代码如下↓

#include"address_book.h"
//颜色函数
void color(short x)
{
	if (x >= 0 && x <= 15)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
	else
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
//创建菜单函数
void menu()
{
	color(0);//黑色
	system("cls");//清屏
	color(12);//白色
	printf("¤--------------------------------------------¤\n");
	printf("|■■■■■■■■→通讯录v1.0←■■■■■■■■|\n");
	printf("|———————————————————————|\n");
	printf("|*****→1.increase ■ 2.deLete←*****|\n");
	printf("|*****→3.find     ■ 4.Revise←*****|\n");
	printf("|*****→5.Check    ■ 6.Print ←*****|\n");
	printf("|*****→0.Exit     ■         ←*****|\n");
	printf("|———————————————————————|\n");
	printf("|■■■■■■■■→通讯录v1.0←■■■■■■■■|\n");
	printf("¤--------------------------------------------¤\n");
}
enum Number
{
	Exit,
	Increase,
	DeLete,
	Find,
	Revise,
	Check,
	Print,
};
int main(void)
{
	menu();
	int input = 0;
	//Of course initialize all0:Contact con = {0};也是可以的,The initialization is not here, of course, there's a reason for this can deal with more complex problems.
	Contact con;//通讯录
	//初始化通讯录
	InitContact(&con);
	do
	{
		color(5);
		printf("¤----------------¤\n");
		printf("|Please enter the number on the interface|:");
		scanf("%d", &input);
		printf("¤----------------¤\n");
		color(1);
		switch (input)
		{
		case Exit:
			printf("══════════════@\n");
			printf("退出通讯录[email protected]\n");
			printf("══════════════@\n");
			break;
		case Increase:
			//增加人的信息,put it in the address book.
			Add_Contact(&con);
			break;
		case DeLete:
			//Delete the information of the correspondent
			DeLete_Contact(&con);
			break;
		case Find:
			//Find the information of a specified address book person
			Find_Contact(&con);
			break;
		case Revise:
			//Modify the information of the specified address book person
			Revise_Contact(&con);
			break;
		case Check:
			//Check the information of people in the address book
			Check_Contact(&con);
			break;
		case Print:
			//Print the information of the people in the address book.
			Print_Contact(&con);
			break;
		default:printf("The number you entered was not found,请重新输入~\n");
		}
	} while (input);
	return 0;
}

address_book.c 

示例代码如下↓

#define _CRT_SECURE_NO_WARNINGS 1
#include"address_book.h"

void InitContact(Contact* pc)
{
	pc->sz = 0;
	//memest() - 内存设置
	memset(pc->date, 0, sizeof(pc->date)); 
}

void Add_Contact(Contact* pc)
{
	if (pc->sz == num)
	{
		printf("Contacts are full\n");
		return;
	}
	//增加一个人的信息
	printf("\n");
	printf("Please enter the name of the adder->:");
	scanf("%s", pc->date[pc->sz].name);//注意→数组名是首元素地址,So no need to take the address.

	printf("Please enter the age of the adder->:");
	scanf("%s", pc->date[pc->sz].age);

	printf("Please enter the increase a person's sex->:");
	scanf("%s", pc->date[pc->sz].genger);

	printf("Please enter the phone number of the person to be added->:");
	scanf("%s", pc->date[pc->sz].telephone);

	printf("Please enter the address of the adder->:");
	scanf("%s", pc->date[pc->sz].address);
	//成功~
	pc->sz++;
	printf("*恭喜你~添加信息成功*\n");
	printf("\n");
}

void Print_Contact(const Contact* pc)
{
	int i = 0;
	//print title bar information
	printf("|-------------------------------------------------------|\n");
	printf("|%-3s\t%-3s\t%-3s\t%-11s\t%-10s\t|\n", "名字", "年龄", "性别", "电话", "地址");

	for (i = 0; i < pc->sz; i++)
	{
		printf("|%-3s\t%-3s\t%-3s\t%-11s\t%-10s\t|\n", pc->date[i].name,
			pc->date[i].age,
			pc->date[i].genger,
			pc->date[i].telephone,
			pc->date[i].address);
	}
}

static int FindByname(Contact* pc, char name[])
{
	int i = 0;
	//用for循环进行遍历
	for (i = 0; i < pc->sz; i++)
	{
		//strcmp()比较字符串
		if (strcmp(pc->date[i].name, name) == 0)
		{
			return i;//返回下标
		}
	}
	return -1;
}

void DeLete_Contact(Contact* pc)
{
	char name[Max_name] = { 0 };
	//When the address book is empty
	if (pc->sz == 0)
	{
		printf("There is no information in the address book\n");
		return;
	}
	printf("请输入你要删除的名字->:");
	scanf("%s", name);
	//查找要删除的人:有/没有
	int ret = FindByname(pc, name);
	if (ret == -1)
	{
		printf("No information found for the current person\n");
		return;
	}
	//删除
	int i = 0;
	for (i = ret; i < pc->sz - 1; i++)
	{
		pc->date[i] = pc->date[i + 1];
	}
	pc->sz--;//Because we delete the successful subscript to reduce1
	printf("*恭喜你~删除信息成功*\n");
	printf("\n");
}

void Find_Contact(Contact* pc)
{
	char name[Max_name] = { 0 };
	printf("请输入你要查找的名字->:");
	scanf("%s", name);
	int ret = FindByname(pc, name);
	if (ret == -1)
	{
		printf("The current person's name was not found\n");
		return;
	}
	else
	{
		printf("|-------------------------------------------------------|\n");
		printf("|%-3s\t%-3s\t%-3s\t%-11s\t%-10s\t|\n", "名字", "年龄", "性别", "电话", "地址");

		printf("|%-3s\t%-3s\t%-3s\t%-11s\t%-10s\t|\n", 
			pc->date[ret].name,
			pc->date[ret].age,
			pc->date[ret].genger,
			pc->date[ret].telephone,
			pc->date[ret].address);
		printf("查找%sの信息成功~\n", pc->date[ret].name);
	}
}

void Revise_Contact(Contact* pc)
{
	char name[Max_name] = { 0 };
	printf("Please enter the name of the person you want to modify->:");
	scanf("%s", name);
	int ret = FindByname(pc, name);
	if (ret == -1)
	{
		printf("Information about current modifier does not exist\n");
		return;
	}
	else
	{
		printf("请输入修改人的名字->:");
		scanf("%s", pc->date[ret].name);//注意→数组名是首元素地址,So no need to take the address.

		printf("请输入修改人的年龄->:");
		scanf("%s", pc->date[ret].age);

		printf("Please enter the gender of the person modifying->:");
		scanf("%s", pc->date[ret].genger);

		printf("请输入修改人的电话->:");
		scanf("%s", pc->date[ret].telephone);

		printf("请输入修改人的地址->:");
		scanf("%s", pc->date[ret].address);
		printf("恭喜你,修改成功~\n");
	}
}

int sort_name_max(const void* e1, const void* e2)
{
	return (strcmp(((struct Contact*)e1)->date->name, ((struct Contact*)e2)->date->name));
}

void Check_Contact(Contact* pc)
{
	//qosrt()Function first letter sorting
	qsort(pc->date, pc->sz, sizeof(pc->date[0]), sort_name_max);
	printf("|%-3s\t%-3s\t%-3s\t%-11s\t%-10s\t|\n", "名字", "年龄", "性别", "电话", "地址");
	int i;
	for (i = 0; i < pc->sz; i++)
	{
		printf("|%-3s\t%-3s\t%-3s\t%-11s\t%-10s\t|\n", pc->date[i].name,
			pc->date[i].age,
			pc->date[i].genger,
			pc->date[i].telephone,
			pc->date[i].address);
	}
}

address_book.h 

示例代码如下↓

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h> 
#include<stdlib.h>
#include<Windows.h>
#include<string.h>

#define Max_name 5
#define Max_age 100
#define Max_genger 3
#define Max_address 20
#define Max_telephone 20

#define num 1000

//类型的定义
typedef struct information
{
	//名字、年龄、性别、电话、地址.
	char name[Max_name];
	char age[Max_age];
	char genger[Max_genger];
	char telephone[Max_telephone];
	char address[Max_address];
}information;
//通讯录
typedef struct Contact
{
	//Here is a structure nested
	information date[num];//Contains the information added
	int sz;//记录当前通讯录有效信息的个数
}Contact;
//初始化通讯录
void InitContact(Contact* pc);
//增加通讯录信息
void Add_Contact(Contact* pc);
//打印通讯录的信息
void Print_Contact(const Contact* pc);
//Delete the information of the correspondent
void DeLete_Contact(Contact* pc);
//Find the information of a specified address book person
void Find_Contact(Contact* pc);
//Modify the information of the specified address book person
void Revise_Contact(Contact* pc);
//Check the information of people in the address book
void Check_Contact(Contact* pc);

最后

Of course, the follow-up will still use the method of dynamic memory development to realize a program of this address book,Because the above program actually uses a fixed way of opening up memory space,是不太合适的.

原网站

版权声明
本文为[Genze]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208092308161592.html