[C language] dynamic address book management system


foreword

I wrote a rudimentary communication management system before, in which the capacity of the address book is set in advance. This has a disadvantage that when the capacity reaches the maximum value, no more information can be added. Therefore, in order to solve such problems, you can choose the dynamic memory function for dynamic management, and increase capacity independently when necessary.

If you are not familiar with the address book management system and C language dynamic memory management, you can read these two articles first:

C language address book management system
C language dynamic memory management


1. System framework construction

1. Creation of member information

First of all, the overall framework of member information remains unchanged, and an address book structure and a member information structure are still selected; secondly, a member information structure and a variable indicating the number of members are still nested inside the address book structure. The point of change is that at the beginning, a maximum capacity value should be added to the address book structure, and when the capacity is full, dynamic memory development will be performed, that is, the capacity of the address book will increase dynamically.

struct PeoInfo
{
    
    
	char name[MAX_NAME];
	int age;
	char sex[MAX_SEX];
	char tele[MAX_TELE];
	char addr[MAX_ADDR];
};
//通讯录类型
struct Contact
{
    
    
	struct PeoInfo *data;//存放一个信息
	int size;//记录当前已经有的元素个数
	int capacity;//当前通讯录最大容量
};

2. Menu implementation

void menu()
{
    
    
	printf("\n\n\t\t-------------------------欢迎使用-------------------------\n");
	printf("\t\t☆☆ 1. ADD                           ☆☆\n\n");
	printf("\t\t☆☆ 2. DEL                           ☆☆\n\n");
	printf("\t\t☆☆ 3. SEARCH                        ☆☆\n\n");
	printf("\t\t☆☆ 4. MODIFY                        ☆☆\n\n");
	printf("\t\t☆☆ 5. SHOW                          ☆☆\n\n");
	printf("\t\t☆☆ 6. SORT                          ☆☆\n\n");
	printf("\t\t☆☆ 0. EXIT                          ☆☆\n\n");
	printf("\t\t----------------------------------------------------------\n");
}

3. System Function Statement

There is one more DestroyContact() function than before. Its function is to release (destroy) the previously dynamically opened memory through the free() function.

void InitContcat(struct Contact* ps);//初始化同学录

void AddContact(struct Contact* ps);//增加

void ShowContact(const struct Contact* ps);//展示

void DelContact(struct Contact* ps);//删除

void SearchContact(struct Contact* ps);//查找

void ModifyContact(struct Contact* ps);//修改

void SortContact(struct Contact* ps);//排序

void DestroyContact(struct Contact* ps);//销毁

2. Realization of system functions

1. Initialize the address book

In the initialization function, use the malloc() function to dynamically open up the address capacity of three member information sizes, and assign values ​​to the current number of people in the address book and the maximum capacity of the address book.

void InitContcat(struct Contact* ps)
{
    
    
	ps->data = (struct PeoInfo*)malloc(3 * sizeof(struct PeoInfo));
	if (ps->data == NULL)
	{
    
    
		return;
	}
	ps->size = 0;
	ps->capacity = 3;
}

2. Capacity detection

The function of this function is to compare the current number of people in the address book with the maximum capacity. If the space is not full, the function will not be executed. On the contrary, the realloc() function will be used to expand the capacity, and each expansion will increase the capacity by two.

void CheckCapacity(struct Contact* ps)
{
    
    
	if (ps->size == ps->capacity)
	{
    
    
		struct PeoInfo* ptr = realloc(ps->data, (ps->capacity + 2)*sizeof(struct PeoInfo));
		if (ptr != NULL)
		{
    
    
			ps->data = ptr;
			ps->capacity += 2;
			printf("\t\t增容成功\n");
		}
	}
}

3. Add information

This function first checks the capacity of the current address book, if it is full, then increase the capacity, if not, then directly increase the information.

void AddContact(struct Contact* ps)
{
    
    
	system("cls");
	printf("\t\t----------------------------------\n");
	CheckCapacity(ps);//检测当前通讯录的容量,若满了,则增加容量,若没满,则就直接增加信息
	//增加数据
	printf("\t\t请输入名字:>");
	scanf("%s", ps->data[ps->size].name);
	printf("\t\t请输入年龄:>");
	scanf("%d", &(ps->data[ps->size].age));
	printf("\t\t请输入性别:>");
	scanf("%s", ps->data[ps->size].sex);
	printf("\t\t请输入电话:>");
	scanf("%s", ps->data[ps->size].tele);
	printf("\t\t请输入住址:>");
	scanf("%s", ps->data[ps->size].addr);
	ps->size++;
	printf("\t\t添加成功\n");
	printf("\t\t----------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}

4. Print information

The function of this function is to print the saved information, first of all, to detect the number of people in the address book, why? The reason is that you directly select this function as soon as you open the address book. With such a judgment, managers can interact better with the address book.

void ShowContact(const struct Contact* ps)
{
    
    
	system("cls");
	printf("\t\t--------------------------------------------------------------\n");
	if (ps->size == 0)
	{
    
    
		printf("\t\t通讯录为空\n");
	}
	else
	{
    
    
		int i = 0;
		printf("\t\t%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字","年龄","性别","电话","住址");
		for (i = 0; i < ps->size; i++)
		{
    
    
			printf("\t\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
				ps->data[i].name,
				ps->data[i].age,
				ps->data[i].sex,
				ps->data[i].tele,
				ps->data[i].addr);
		}
	}
	printf("\t\t---------------------------------------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}

4. Find location by name

int FindByName(struct Contact* ps, char name[MAX_NAME])
{
    
    
	system("cls");
	printf("\t\t----------------------------------\n");
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
    
    
		if (strcmp(ps->data[i].name, name) == 0)
		{
    
    
			return i;
		}
	}
	return -1;
	printf("\t\t----------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}

5. Delete information

void DelContact(struct Contact* ps)
{
    
    
	system("cls");
	printf("\t\t----------------------------------\n");
	char name[MAX_NAME];
	printf("\t\t请输入删除人的名字:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);

	if (pos==-1)
	{
    
    
		printf("\t\t您要删除的人不存在!\n");
	}
	else
	{
    
    
		int j = 0;
		for (j = pos; j < ps->size - 1; j++)
		{
    
    
			ps->data[j] = ps->data[j + 1];
		}
		ps->size--;
		printf("\t\t删除成功\n");
	}
	printf("\t\t----------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}

6. Find information

void SearchContact(struct Contact* ps)
{
    
    
	system("cls");
	printf("\t\t----------------------------------------------------------------------\n");
	char name[MAX_NAME];
	printf("\t\t请输入您要查找对象的名字:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	if (pos == -1)
	{
    
    
		printf("\t\t对不起,没有找到您要查找的人!\n");
	}
	else
	{
    
    
		printf("\t\t%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "住址");
		printf("\t\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
			ps->data[pos].name,
			ps->data[pos].age,
			ps->data[pos].sex,
			ps->data[pos].tele,
			ps->data[pos].addr);
	}
	printf("\t\t-----------------------------------------------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}

7. Modify information

void ModifyContact(struct Contact* ps)
{
    
    
	system("cls");
	printf("\t\t---------------------------------------------------------------------------\n");
	char name[MAX_NAME];
	printf("\t\t请输入您要修改对象的名字:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	if (pos == -1)
	{
    
    
		printf("\t\t对不起,没有找到您要查找的人!\n");
	}
	else
	{
    
    
		printf("\t\t请输入名字:>");
		scanf("%s", ps->data[pos].name);
		printf("\t\t请输入年龄:>");
		scanf("%d", &(ps->data[pos].age));
		printf("\t\t请输入性别:>");
		scanf("%s", ps->data[pos].sex);
		printf("\t\t请输入电话:>");
		scanf("%s", ps->data[pos].tele);
		printf("\t\t请输入住址:>");
		scanf("%s", ps->data[pos].addr);
		printf("\t\t修改成功\n");
	}
	printf("\t\t----------------------------------------------------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}

8. Sort information

The sorting object selected by this function is the name, of course, it can also be replaced with other sorting objects according to your own needs.

void SortContact(struct Contact* ps)
{
    
    
	system("cls");
	printf("\t\t-----------------------------------------------------------------------\n");
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
    
    
		int j = 0;
		for (j = 0; j < ps->size-i-1; j++)
		{
    
    
			if (strcmp(ps->data[j].name ,ps->data[j+1].name)>0)
			{
    
    
				struct PeoInfo ret ;
				ret = ps->data[j];
				ps->data[j] = ps->data[j+1];
				ps->data[j+1] = ret;
			}
		}
	}
	printf("\t\t%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "住址");
	for (i = 0; i < ps->size; i++)
	{
    
    
		printf("\t\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
			ps->data[i].name,
			ps->data[i].age,
			ps->data[i].sex,
			ps->data[i].tele,
			ps->data[i].addr);
	}
	printf("\t\t-----------------------------------------------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}

8. Destroy address book

Because the address space opened by the dynamic memory is occupied by member information, it is only necessary to release the data items in the address book structure when releasing the memory.

void DestroyContact(struct Contact* ps)
{
    
    
	free(ps->data);
	ps->data = NULL;
	//错误案例
	//free(ps);
	//ps=NULL;
}

3. Source code display

1.test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"

int main()
{
    
    
	int n = 0;
	system("color 3E");
	struct Contact con;//con就是同学录,里边包含data指针和size和capacity
	//初始化同学录
	InitContcat(&con);
	do
	{
    
     
		printf("\n\n\t\t-------------------------欢迎使用-------------------------\n");
		printf("\t\t☆☆ 1. ADD                           ☆☆\n\n");
		printf("\t\t☆☆ 2. DEL                           ☆☆\n\n");
		printf("\t\t☆☆ 3. SEARCH                        ☆☆\n\n");
		printf("\t\t☆☆ 4. MODIFY                        ☆☆\n\n");
		printf("\t\t☆☆ 5. SHOW                          ☆☆\n\n");
		printf("\t\t☆☆ 6. SORT                          ☆☆\n\n");
		printf("\t\t☆☆ 0. EXIT                          ☆☆\n\n");
		printf("\t\t----------------------------------------------------------\n");
		printf("\t\t★★★★★★★★★★★★★★★★\n");
		printf("\t\t请选择您要运行的选项按(0-6):");
		scanf("%d", &n);
		printf("\t\t★★★★★★★★★★★★★★★★\n");
		Sleep(1000);
		switch (n)
		{
    
    
		case ADD:
			AddContact(&con);
			break;
		case DEL:
			DelContact(&con);
			break;
		case SEARCH:
			SearchContact(&con);
			break;
		case MODIFY:
			ModifyContact(&con);
			break;
		case SHOW:
			ShowContact(&con);
			break;
		case SORT:
			SortContact(&con);
			break;
		case EXIT:
			Sleep(1000);
			//销毁同学录
			DestroyContact(&con);
			printf("退出同学录\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (n);
	return 0;

}

2.contact.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void InitContcat(struct Contact* ps)
{
    
    
	ps->data = (struct PeoInfo*)malloc(3 * sizeof(struct PeoInfo));
	if (ps->data == NULL)
	{
    
    
		return;
	}
	ps->size = 0;
	ps->capacity = 3;
}
void CheckCapacity(struct Contact* ps)
{
    
    
	if (ps->size == ps->capacity)
	{
    
    
		struct PeoInfo* ptr = realloc(ps->data, (ps->capacity + 2)*sizeof(struct PeoInfo));
		if (ptr != NULL)
		{
    
    
			ps->data = ptr;
			ps->capacity += 2;
			printf("\t\t增容成功\n");
		}
	}
}
void AddContact(struct Contact* ps)
{
    
    
	system("cls");
	printf("\t\t----------------------------------\n");
	CheckCapacity(ps);//检测当前通讯录的容量,若满了,则增加容量,若没满,则就直接增加信息
	//增加数据
	printf("\t\t请输入名字:>");
	scanf("%s", ps->data[ps->size].name);
	printf("\t\t请输入年龄:>");
	scanf("%d", &(ps->data[ps->size].age));
	printf("\t\t请输入性别:>");
	scanf("%s", ps->data[ps->size].sex);
	printf("\t\t请输入电话:>");
	scanf("%s", ps->data[ps->size].tele);
	printf("\t\t请输入住址:>");
	scanf("%s", ps->data[ps->size].addr);
	ps->size++;
	printf("\t\t添加成功\n");
	printf("\t\t----------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}
void ShowContact(const struct Contact* ps)
{
    
    
	system("cls");
	printf("\t\t--------------------------------------------------------------\n");
	if (ps->size == 0)
	{
    
    
		printf("\t\t通讯录为空\n");
	}
	else
	{
    
    
		int i = 0;
		printf("\t\t%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字","年龄","性别","电话","住址");
		for (i = 0; i < ps->size; i++)
		{
    
    
			printf("\t\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
				ps->data[i].name,
				ps->data[i].age,
				ps->data[i].sex,
				ps->data[i].tele,
				ps->data[i].addr);
		}
	}
	printf("\t\t---------------------------------------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}
int FindByName(struct Contact* ps, char name[MAX_NAME])
{
    
    
	system("cls");
	printf("\t\t----------------------------------\n");
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
    
    
		if (strcmp(ps->data[i].name, name) == 0)
		{
    
    
			return i;
		}
	}
	return -1;
	printf("\t\t----------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}
void DelContact(struct Contact* ps)
{
    
    
	system("cls");
	printf("\t\t----------------------------------\n");
	char name[MAX_NAME];
	printf("\t\t请输入删除人的名字:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);

	if (pos==-1)
	{
    
    
		printf("\t\t您要删除的人不存在!\n");
	}
	else
	{
    
    
		int j = 0;
		for (j = pos; j < ps->size - 1; j++)
		{
    
    
			ps->data[j] = ps->data[j + 1];
		}
		ps->size--;
		printf("\t\t删除成功\n");
	}
	printf("\t\t----------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}
void SearchContact(struct Contact* ps)
{
    
    
	system("cls");
	printf("\t\t----------------------------------------------------------------------\n");
	char name[MAX_NAME];
	printf("\t\t请输入您要查找对象的名字:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	if (pos == -1)
	{
    
    
		printf("\t\t对不起,没有找到您要查找的人!\n");
	}
	else
	{
    
    
		printf("\t\t%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "住址");
		printf("\t\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
			ps->data[pos].name,
			ps->data[pos].age,
			ps->data[pos].sex,
			ps->data[pos].tele,
			ps->data[pos].addr);
	}
	printf("\t\t-----------------------------------------------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}
void ModifyContact(struct Contact* ps)
{
    
    
	system("cls");
	printf("\t\t---------------------------------------------------------------------------\n");
	char name[MAX_NAME];
	printf("\t\t请输入您要修改对象的名字:>");
	scanf("%s", name);
	int pos = FindByName(ps, name);
	if (pos == -1)
	{
    
    
		printf("\t\t对不起,没有找到您要查找的人!\n");
	}
	else
	{
    
    
		printf("\t\t请输入名字:>");
		scanf("%s", ps->data[pos].name);
		printf("\t\t请输入年龄:>");
		scanf("%d", &(ps->data[pos].age));
		printf("\t\t请输入性别:>");
		scanf("%s", ps->data[pos].sex);
		printf("\t\t请输入电话:>");
		scanf("%s", ps->data[pos].tele);
		printf("\t\t请输入住址:>");
		scanf("%s", ps->data[pos].addr);
		printf("\t\t修改成功\n");
	}
	printf("\t\t----------------------------------------------------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}
void SortContact(struct Contact* ps)
{
    
    
	system("cls");
	printf("\t\t-----------------------------------------------------------------------\n");
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
    
    
		int j = 0;
		for (j = 0; j < ps->size-i-1; j++)
		{
    
    
			if (strcmp(ps->data[j].name ,ps->data[j+1].name)>0)
			{
    
    
				struct PeoInfo ret ;
				ret = ps->data[j];
				ps->data[j] = ps->data[j+1];
				ps->data[j+1] = ret;
			}
		}
	}
	printf("\t\t%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "住址");
	for (i = 0; i < ps->size; i++)
	{
    
    
		printf("\t\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
			ps->data[i].name,
			ps->data[i].age,
			ps->data[i].sex,
			ps->data[i].tele,
			ps->data[i].addr);
	}
	printf("\t\t-----------------------------------------------------------------------\n");
	printf("\t\t3秒后自动退出至主页面\n");
	Sleep(3000);
	system("cls");
}

void DestroyContact(struct Contact* ps)
{
    
    
	free(ps->data);
	ps->data = NULL;
}

3.contact.h

#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 12
#define MAX_ADDR 30
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
enum Option
{
    
    
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SHOW,
	SORT,
};
struct PeoInfo
{
    
    
	char name[MAX_NAME];
	int age;
	char sex[MAX_SEX];
	char tele[MAX_TELE];
	char addr[MAX_ADDR];
};
//通讯录类型
struct Contact
{
    
    
	struct PeoInfo *data;//存放一个信息
	int size;//记录当前已经有的元素个数
	int capacity;//当前通讯录最大容量
};

//声明函数

void InitContcat(struct Contact* ps);//初始化同学录

void AddContact(struct Contact* ps);//增加

void ShowContact(const struct Contact* ps);//展示

void DelContact(struct Contact* ps);//删除

void SearchContact(struct Contact* ps);//查找

void ModifyContact(struct Contact* ps);//修改

void SortContact(struct Contact* ps);//排序

void DestroyContact(struct Contact* ps);//销毁

Note: This article is an advanced version of the address book management system, and the ultimate version of file preservation will be released later.

Guess you like

Origin blog.csdn.net/weixin_47648037/article/details/127045389
Recommended