Address book implementation [C language]

Table of contents

Preface

1. Overall logical analysis

2. Implementation steps

1. Problems with creating menus and multiple operations

2. Create an address book

3. Initialize address book

4. Add contacts

5. Display contacts

6. Delete specified contacts

7. Find the designated contact person

8. Modify contact information

9. Sort contact information

3. All source code


Preface

We have introduced custom types in detail in the last issue. In this issue, we will use the knowledge we have learned to make a small project to practice. In this issue we will use C language to implement a static version of the address book! Dynamic memory management and file operations will be introduced later. We will optimize and improve based on this version!

1. Overall logical analysis

Just like before when we implemented the three-piece chess and minesweeper small projects, we first sort out the logic of the overall project in as much detail as possible. Otherwise, it would be a slap in the face at the beginning, and it would be bad to find out that it was all bugs at the end! We analyze it as clearly as possible at the beginning, so that even if there are problems later, we can quickly identify them! This issue still uses multi-file programming as in previous issues! OK, let’s sort out the basic implementation logic of this small project:

1. Problems with creating menus and multiple operations

2. Create an address book

3. Initialization of address book and operations such as adding, deleting, checking and modifying

2. Implementation steps

1. Problems with creating menus and multiple operations

We hope that when we operate the address book, we can be given a menu function before the operation! Considering that we will need to operate the address book many times in the future (adding, deleting, checking, modifying, etc.), we quickly thought of using a do..while loop to process it. We have written this two or three times before, so we directly code it:

#include"contact.h"

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

void test()
{
	int input = 0;
	do
	{
		menu();
		printf("请选择操作数:> ");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			//add
			break;
		case 2:
			//del
			break;
		case 3:
			//search
			break;
		case 4:
			//modify
			break;
		case 5:
			//show
			break;
		case 6:
			//sort
			break;
		case 0:
			printf("退出成功!\n");
			break;
		default:
			printf("选择数非法!\n");
			break;
		}

	} while (input);
}

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

See the effect:

2. Create an address book

Before we perform various operations on the address book, we must first have an address book! The essence of the address book is to describe a person's attributes such as: name, age, gender, phone number, address, etc.! These are all different types, and we can easily think of using structures to solve them! But this is just describing one person. There cannot be only one person in your address book. There should be many! And these contacts are of the same type, so a structure array is used to create the address book!

typedef struct PeoInfo
{
	char name[20];
	int age;
	char sex[5];
	char tele[15];
	char addr[20];
}PeoInfo;

This is a structure that describes a person's basic attributes! After creating this structure, we can create a structure array as an address book in the do..while loop in the file test.c!

But there is a problem with writing this way: when you save or delete a contact and want to print it out to see if it is the contact in the address book at that time, how much should you print? We still wonder. So here we have to define another variable sz to record the size of the address book!

In fact, we found that sz can not only record the size of the address book, but also when adding contacts, you only need to increase it to the sz position, add an sz++, and delete an sz--; in this way, sz and the address book are actually bound to At the same time, we might as well encapsulate the address book into a structure. One member is the PeoInfo structure array, and the other is sz that records the size of the address book:

typedef struct PeoInfo
{
	char name[20];
	int age;
	char sex[5];
	char tele[15];
	char addr[20];
}PeoInfo;


typedef struct Contact
{
	PeoInfo data[100];
	int sz;
}Contact;

Then create it in the test.c file!

OK! This address book is created and recorded! In fact, we can still do a little optimization here: we found that there are a lot of constants in both structures. If our needs change later, these will have to be changed! Therefore, for the convenience of later modification, we changed these into #define constants, which will be easy to modify later! Here you may say that enumerations were introduced in the last issue. Wouldn’t it be better to have multiple constant enumerations here? Enumeration here can indeed achieve the purpose, but enumeration is to enumerate all possibilities before starting, which is not the case with ours! So, no need to enumerate here!

#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 15
#define ADDR_MAX 20
#define MAX 100

typedef struct PeoInfo
{
	char name[NAME_MAX];
	int age;
	char sex[SEX_MAX];
	char tele[TELE_MAX];
	char addr[ADDR_MAX];
}PeoInfo;


typedef struct Contact
{
	PeoInfo data[MAX];
	int sz;
}Contact;

This is the optimal code under the current logic!

3. Initialize address book

After we create the address book, we first initialize it! Initialize each element of the structure array to 0, and set the address book size to 0! Because we want to change the content of the structure, we should pass the address! What we use here is that the memset function is directly initialized to 0, or it can be initialized with a loop! Click memset if you don’t understand memset

void InitContact(Contact* pc)
{
	assert(pc);

	memset(pc->data, 0, sizeof(pc->data));
	pc->sz = 0;
}

4. Add contacts

As we have said before, we only need to increase the position of sz! But consider the situation when the address book is full!

void AddContact(Contact* pc) 
{
	assert(pc);
	//通讯录已满
	if (pc->sz == MAX)
	{
		printf("通讯录已满,无法添加!\n");
		return;
	}

	//添加联系人
	printf("请输入联系人姓名:> ");
	scanf("%s", pc->data[pc->sz].name);
	printf("请输入联系人年龄:> ");
	scanf("%d", &pc->data[pc->sz].age);
	printf("请输入联系人性别:> ");
	scanf("%s", pc->data[pc->sz].sex);
	printf("请输入联系人电话:> ");
	scanf("%s", pc->data[pc->sz].tele);
	printf("请输入联系人住址:> ");
	scanf("%s", pc->data[pc->sz].addr);
	pc->sz++;

	printf("添加成功!\n");
}

OK, let’s write a display contact first to verify whether it was added successfully!

5. Display contacts

Displaying contacts means printing the contacts in the address book on the console!

void ShowContact(Contact* pc)
{
	assert(pc);

	printf("%-s\t%-s\t%-5s\t%-15s\t%-20s\n", "姓名", "年龄", "性别", "电话", "住址");
	for (int i = 0; i < pc->sz; i++)
	{
		printf("%-s\t%-d\t%-5s\t%-15s\t%-20s\n",
			pc->data[i].name,
			pc->data[i].age,
			pc->data[i].sex,
			pc->data[i].tele,
			pc->data[i].addr
		);
	}
}

OK, let’s see if the addition is successful and the contacts are displayed!

OK, adding contacts and displaying contacts were successful! It shows that there is no problem with our above logic!

6. Delete specified contacts

We enter the name of the person we want to delete, search it, and delete it. Otherwise, there is no such contact in the output! Please consider the situation when the address book is empty! Search is mentioned here. This search is a little different from the search in the address book. This one only needs to return the subscript after the search, while the other one has a different function of printing contact information. So we are encapsulating a function alone!

//查找要删除的人并返回下标
int FindContact(const Contact* pc, const char* name)
{
	for (int i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)
		{
			return i;//找到了
		}
	}

	return -1;//没找到
}

//删除联系人
void DelContact(Contact* pc)
{
	assert(pc);
    //通讯录为空
	if (pc->sz == 0)
	{
		printf("通讯录为空,无法删除!\n");
		return;
	}

	char name[NAME_MAX] = { 0 };
	printf("请输入要删除人的姓名:> ");
	scanf("%s", name);

	int pos = FindContact(pc, name);

	if (pos != -1)
	{
		//删除
		for (int i = pos; i < pc->sz - 1; i++)
		{
			pc->data[i] = pc->data[i + 1];
		}

		pc->sz--;
		printf("成功删除联系人!\n");
	}
	else
	{
		printf("没有此联系人!\n");
	}
}

What should be noted here is that strcmp must be used to compare strings, and == cannot be used. We introduced this in detail in the issue of string functions and memory functions! Also pay attention to the subscript out-of-bounds problem when deleting!

See the effect:

 7. Find the designated contact person

To find the specified contact, you should search in the specified method first. If found, print it, otherwise the output will not include this contact! This page that displays a contact here may also be used to modify the contact later, so for convenience, we encapsulate it into a function and call it directly!


//显示单个联系人信息
void Print(const Contact* pc, int pos)
{
	assert(pc);

	printf("%-s\t%-s\t%-5s\t%-15s\t%-20s\n", "姓名", "年龄", "性别", "电话", "住址");

	printf("%-s\t%-d\t%-5s\t%-15s\t%-20s\n",
		pc->data[pos].name,
		pc->data[pos].age,
		pc->data[pos].sex,
		pc->data[pos].tele,
		pc->data[pos].addr);

}

//查找指定联系人
void Search(const Contact* pc)
{
	assert(pc);

	char name[NAME_MAX] = { 0 };
	printf("请输入要查找联系人姓名:> ");
	scanf("%s", name);

	int pos = -1;//初始化为-1表示没有此联系人
	for (int i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)
		{
			pos = i;//找到了
			break;
		}
	}

	if (pos != -1)
	{
		Print(pc, pos);
	}
	else
	{
		printf("没有此联系人!\n");
	}
}

In fact, it can be more concise. FindContact has been implemented above and we can use it:

void Search(const Contact* pc)
{
	assert(pc);

	char name[NAME_MAX] = { 0 };
	printf("请输入要查找联系人姓名:> ");
	scanf("%s", name);

	int pos = FindContact(pc, name);

	if (pos != -1)
	{
		Print(pc, pos);
	}
	else
	{
		printf("没有此联系人!\n");
	}
}

See the effect:

8. Modify contact information

There are many possibilities for modifying contact information. For example, you may modify only one item such as name or gender, or you may modify multiple items such as name, age, etc., or you may have to modify all of these situations. When we want to modify something, we can still expect to have a menu for us to choose from, and after modifying an item, we may still want to continue modifying it. Based on this, we adopt the do...while loop + menu in the test.c file!

//修改联系人信息菜单
void menu1()
{
    system("cls");
	printf("*****************************************\n");
	printf("********* 1. name     2. age   **********\n");
	printf("********* 3. sex      4. tele  **********\n");
	printf("********* 5. addr     6. all   **********\n");
	printf("********* 0. exit              **********\n");
	printf("*****************************************\n");
}

//修改名字
void ModName(Contact* pc, int ret)
{
	assert(pc);

	printf("请输入修改后联系人姓名:> ");
	scanf("%s", pc->data[ret].name);
	printf("修改成功!\n");
}

//修改年龄
void ModAge(Contact* pc, int ret)
{
	assert(pc);

	printf("请输入修改后联系人年龄:> ");
	scanf("%d", &pc->data[ret].age);
	printf("修改成功!\n");
}

//修改性别
void ModSex(Contact* pc, int ret)
{
	assert(pc);

	printf("请输入修改后联系人性别:> ");
	scanf("%s", &pc->data[ret].sex);
	printf("修改成功!\n");
}

//修改电话
void ModTele(Contact* pc, int ret)
{
	assert(pc);

	printf("请输入修改后联系人电话:> ");
	scanf("%s", &pc->data[ret].tele);
	printf("修改成功!\n");
}

//修改住址
void ModAddr(Contact* pc, int ret)
{
	assert(pc);

	printf("请输入修改后联系人住址:> ");
	scanf("%s", &pc->data[ret].addr);
	printf("修改成功!\n");
}


void ModAll(Contact* pc, int ret)
{
	assert(pc);

	printf("请输入联系人姓名:> ");
	scanf("%s", pc->data[ret].name);
	printf("请输入联系人年龄:> ");
	scanf("%d", &pc->data[ret].age);
	printf("请输入联系人性别:> ");
	scanf("%s", pc->data[ret].sex);
	printf("请输入联系人电话:> ");
	scanf("%s", pc->data[ret].tele);
	printf("请输入联系人住址:> ");
	scanf("%s", pc->data[ret].addr);
	
	printf("修改成功!\n");
}


//修改联系人
void ModContact(Contact* pc)
{
	assert(pc);
	//判断是否为空
	if (pc->sz == 0)
	{
		printf("通讯录为空,无法修改!\n");
		return;
	}

	char name[NAME_MAX] = { 0 };
	printf("请输入要修改系人姓名:> ");
	scanf("%s", name);
	//判断是否有该联系人
	int ret = FindContact(pc, name);
	
	//有该联系人
	if (ret != -1)
	{
		int n = 0;
		do
		{
			menu1();
			printf("请选择修改内容:> ");
			scanf("%d", &n);
			//修改
			switch (n)
			{
			case 1:
				ModName(pc,ret);
				break;
			case 2:
				ModAge(pc, ret);
				break;
			case 3:
				ModSex(pc, ret);
				break;
			case 4:
				ModTele(pc, ret);
				break;
			case 5:
				ModAddr(pc, ret);
				break;
			case 6:
				ModAll(pc, ret);
				break;
			case 0:
				printf("修改结束!\n");
				break;
			default:
				printf("选择数非法!\n");
				break;
			}

		} while (n);
	}
	else
	{
		printf("没有此联系人!\n");
	}
}

Look at the effect: (The editor did a clear screen)

before fixing:

 After modification:

OK, it was quite successful! Let’s sort it out again!

9. Sort contact information

We have already implemented the basic function of adding, deleting, checking and modifying the address book above! We want him to have another sorting function, such as sorting by name, or sorting by age! We introduced the callback function in the pointer advancement section. Here we use qsort to sort the address book! We expect there will still be a menu selection at the beginning like above!


//排序菜单
void menu2()
{
	system("cls");
	printf("**********************************\n");
	printf("***** 1. name    2. age **********\n");
	printf("**********************************\n");
}

//名字比较函数
int cmp_name(const void* str1, const void* str2)
{
	//return strcmp(((PeoInfo*)str1)->name, ((PeoInfo*)str2)->name);
	return strcmp((((Contact*)str1)->data)->name, (((Contact*)str2)->data)->name);
}

//名字排序
void SortName(Contact* pc)
{
	qsort(pc->data, pc->sz, sizeof(pc->data[0]), cmp_name);
}

//年龄比较函数
int cmp_age(const void* str1, const void* str2)
{

	//return ((PeoInfo*)str1)->age - ((PeoInfo*)str2)->age;
	return (((Contact*)str1)->data)->age - (((Contact*)str2)->data)->age;
}
//年龄排序
void SortAge(Contact* pc)
{
	qsort(pc->data, pc->sz, sizeof(pc->data[0]), cmp_age);
}

//排序联系人信息
void SortContact(Contact* pc)
{
	assert(pc);
	//判断为空
	if (pc->sz == 0)
	{
		printf("通讯录为空,无法排序!\n");
		Sleep(3000);
		system("cls");
		return;
	}


	menu2();
	int n = 0;
	printf("请选择排序方式:> ");
	scanf("%d", &n);
	//排序
	switch (n)
	{
	case 1:
		SortName(pc);
		system("cls");
		printf("排序成功!\n");
		break;
	case 2:
		SortAge(pc);
		system("cls");
		printf("排序成功!\n");
		break;
	default:
		printf("选择数非法!\n");
		Sleep(3000);
		system("cls");
		break;
	}
}

See the effect:

Before sorting (age):

 After sorting (age):

Before sorting (name):

 After sorting (name):

OK, sorting is implemented!

3. All source code

 contact.h

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#include<windows.h>

#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 15
#define ADDR_MAX 20
#define MAX 100

typedef struct PeoInfo
{
	char name[NAME_MAX];
	int age;
	char sex[SEX_MAX];
	char tele[TELE_MAX];
	char addr[ADDR_MAX];
}PeoInfo;


typedef struct Contact
{
	PeoInfo data[MAX];
	int sz;
}Contact;


//初始化通讯录
void InitContact(Contact* pc);

//添加联系人
void AddContact(Contact* pc);

//显示联系人
void ShowContact(const Contact* pc);

//删除联系人
void DelContact(Contact* pc);

//查找指定联系人
void Search(const Contact* pc);

//修改联系人
void ModContact(Contact* pc);

//排序联系人信息
void SortContact(Contact* pc);


contact.c

#include"contact.h"

//初始化通讯录
void InitContact(Contact* pc)
{
	assert(pc);

	memset(pc->data, 0, sizeof(pc->data));
	pc->sz = 0;
}

//增加联系人
void AddContact(Contact* pc) 
{
	assert(pc);
	//通讯录已满
	if (pc->sz == MAX)
	{
		printf("通讯录已满,无法添加!\n");
		Sleep(2000);
		return;
	}

	//添加联系人
	printf("请输入联系人姓名:> ");
	scanf("%s", pc->data[pc->sz].name);
	printf("请输入联系人年龄:> ");
	scanf("%d", &pc->data[pc->sz].age);
	printf("请输入联系人性别:> ");
	scanf("%s", pc->data[pc->sz].sex);
	printf("请输入联系人电话:> ");
	scanf("%s", pc->data[pc->sz].tele);
	printf("请输入联系人住址:> ");
	scanf("%s", pc->data[pc->sz].addr);
	pc->sz++;

	printf("添加成功!\n");
	Sleep(1000);
}

//显示联系人
void ShowContact(const Contact* pc)
{
	assert(pc);

	printf("%-s\t%-s\t%-5s\t%-15s\t%-20s\n", "姓名", "年龄", "性别", "电话", "住址");
	for (int i = 0; i < pc->sz; i++)
	{
		printf("%-s\t%-d\t%-5s\t%-15s\t%-20s\n",
			pc->data[i].name,
			pc->data[i].age,
			pc->data[i].sex,
			pc->data[i].tele,
			pc->data[i].addr
		);
	}
}


//查找要删除的人并返回下标
int FindContact(const Contact* pc, const char* name)
{
	for (int i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)
		{
			return i;//找到了
		}
	}

	return -1;//没找到
}

//删除联系人
void DelContact(Contact* pc)
{
	assert(pc);
	if (pc->sz == 0)
	{
		printf("通讯录为空,无法删除!\n");
		Sleep(2000);
		return;
	}

	char name[NAME_MAX] = { 0 };
	printf("请输入要删除人的姓名:> ");
	scanf("%s", name);

	int pos = FindContact(pc, name);

	if (pos != -1)
	{
		//删除
		for (int i = pos; i < pc->sz - 1; i++)
		{
			pc->data[i] = pc->data[i + 1];
		}

		pc->sz--;
		printf("成功删除联系人!\n");
		Sleep(1000);
	}
	else
	{
		printf("没有此联系人!\n");
		Sleep(2000);
	}
}



//显示单个联系人信息
void Print(const Contact* pc, int pos)
{
	assert(pc);

	printf("%-s\t%-s\t%-5s\t%-15s\t%-20s\n", "姓名", "年龄", "性别", "电话", "住址");

	printf("%-s\t%-d\t%-5s\t%-15s\t%-20s\n",
		pc->data[pos].name,
		pc->data[pos].age,
		pc->data[pos].sex,
		pc->data[pos].tele,
		pc->data[pos].addr);

}

//查找指定联系人
void Search(const Contact* pc)
{
	assert(pc);

	char name[NAME_MAX] = { 0 };
	printf("请输入要查找联系人姓名:> ");
	scanf("%s", name);

	int pos = FindContact(pc, name);
	if (pos != -1)
	{
		Print(pc, pos);
	}
	else
	{
		printf("没有此联系人!\n");
		Sleep(2000);
		system("cls");
	}
}


//修改联系人信息菜单
void menu1()
{
	system("cls");
	printf("*****************************************\n");
	printf("********* 1. name     2. age   **********\n");
	printf("********* 3. sex      4. tele  **********\n");
	printf("********* 5. addr     6. all   **********\n");
	printf("********* 0. exit              **********\n");
	printf("*****************************************\n");
}

//修改名字
void ModName(Contact* pc, int ret)
{
	assert(pc);

	printf("请输入修改后联系人姓名:> ");
	scanf("%s", pc->data[ret].name);
}

//修改年龄
void ModAge(Contact* pc, int ret)
{
	assert(pc);

	printf("请输入修改后联系人年龄:> ");
	scanf("%d", &pc->data[ret].age);
}

//修改性别
void ModSex(Contact* pc, int ret)
{
	assert(pc);

	printf("请输入修改后联系人性别:> ");
	scanf("%s", &pc->data[ret].sex);
}

//修改电话
void ModTele(Contact* pc, int ret)
{
	assert(pc);

	printf("请输入修改后联系人电话:> ");
	scanf("%s", &pc->data[ret].tele);
}

//修改住址
void ModAddr(Contact* pc, int ret)
{
	assert(pc);

	printf("请输入修改后联系人住址:> ");
	scanf("%s", &pc->data[ret].addr);
}


void ModAll(Contact* pc, int ret)
{
	assert(pc);

	printf("请输入联系人姓名:> ");
	scanf("%s", pc->data[ret].name);
	printf("请输入联系人年龄:> ");
	scanf("%d", &pc->data[ret].age);
	printf("请输入联系人性别:> ");
	scanf("%s", pc->data[ret].sex);
	printf("请输入联系人电话:> ");
	scanf("%s", pc->data[ret].tele);
	printf("请输入联系人住址:> ");
	scanf("%s", pc->data[ret].addr);
}


//修改联系人
void ModContact(Contact* pc)
{
	assert(pc);
	//判断是否为空
	if (pc->sz == 0)
	{
		printf("通讯录为空,无法修改!\n");
		Sleep(2000);
		system("cls");
		return;
	}

	char name[NAME_MAX] = { 0 };
	printf("请输入要修改系人姓名:> ");
	scanf("%s", name);
	//判断是否有该联系人
	int ret = FindContact(pc, name);
	
	//有该联系人
	if (ret != -1)
	{
		int n = 0;
		do
		{
			menu1();
			Print(pc, ret);
			printf("请选择修改内容:> ");
			scanf("%d", &n);
			//修改
			switch (n)
			{
			case 1:
				ModName(pc,ret);
				break;
			case 2:
				ModAge(pc, ret);
				break;
			case 3:
				ModSex(pc, ret);
				break;
			case 4:
				ModTele(pc, ret);
				break;
			case 5:
				ModAddr(pc, ret);
				break;
			case 6:
				ModAll(pc, ret);
				break;
			case 0:
				printf("修改结束!\n");
				Sleep(2000);
				system("cls");
				break;
			default:
				printf("选择数非法!\n");
				Sleep(2000);
				system("cls");
				break;
			}

		} while (n);
	}
	else
	{
		printf("没有此联系人!\n");
		Sleep(2000);
		system("cls");
	}
}

//排序菜单
void menu2()
{
	system("cls");
	printf("**********************************\n");
	printf("***** 1. name    2. age **********\n");
	printf("**********************************\n");
}

//名字比较函数
int cmp_name(const void* str1, const void* str2)
{
	return strcmp(((PeoInfo*)str1)->name, ((PeoInfo*)str2)->name);
}

//名字排序
void SortName(Contact* pc)
{
	qsort(pc->data, pc->sz, sizeof(pc->data[0]), cmp_name);
}

//年龄比较函数
int cmp_age(const void* str1, const void* str2)
{

	return ((PeoInfo*)str1)->age - ((PeoInfo*)str2)->age;
}
//年龄排序
void SortAge(Contact* pc)
{
	qsort(pc->data, pc->sz, sizeof(pc->data[0]), cmp_age);
}

//排序联系人信息
void SortContact(Contact* pc)
{
	assert(pc);
	//判断为空
	if (pc->sz == 0)
	{
		printf("通讯录为空,无法排序!\n");
		Sleep(3000);
		system("cls");
		return;
	}


	menu2();
	int n = 0;
	printf("请选择排序方式:> ");
	scanf("%d", &n);
	//排序
	switch (n)
	{
	case 1:
		SortName(pc);
		system("cls");
		printf("排序成功!\n");
		break;
	case 2:
		SortAge(pc);
		system("cls");
		printf("排序成功!\n");
		break;
	default:
		printf("选择数非法!\n");
		Sleep(3000);
		system("cls");
		break;
	}
}

test.c

#include"contact.h"

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

void test()
{
	int input = 0;
	Contact con;
	InitContact(&con);
	do
	{
		menu();
		printf("请选择操作数:> ");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			AddContact(&con);
			system("cls");
			break;
		case 2:
			DelContact(&con);
			system("cls"); 
			break;
		case 3:
			system("cls");
			Search(&con);
			break;
		case 4:
			ModContact(&con);
			break;
		case 5:
			system("cls");
			ShowContact(&con);
			break;
		case 6:
			SortContact(&con);
			break;
		case 0:
			printf("退出成功!\n");
			break;
		default:
			printf("选择数非法!\n");
			break;
		}

	} while (input);
}

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

OK, that’s it for this sharing! Good brothers, see you next time!

Guess you like

Origin blog.csdn.net/m0_75256358/article/details/132369058