C Address Book <Static Version>

content

1. Basic requirements

2. Analysis of the functions of the address book

    (1) Menu implementation

    (2) Initialization function

    (3) Add contacts and print

    (4) Delete the specified contact

    (5) Find the specified contact and print

    (6) Modify the information of the designated person

    (7) Print all information

    (8) Sort address book information

3. Overall code


1. Basic requirements

  • The address book can store the information of 1000 people, including (name + age + gender + phone + address)
  • Features include:
  1. add person information
  2. Delete the information of the specified person
  3. Modify designator information
  4. Find assignee information
  5. Sort address book information
  • Note: This code needs to use three files to complete, the functions are as follows:
  1. test.c file: module for testing address book
  2. contact.h file: type definition, function declaration
  3. contact.c file: function implementation

2. Analysis of the functions of the address book

(1) Menu implementation

  • test.c file:
#include"contact.h"
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");
}

  •  At this point the overall code of test.c:
#include"contact.h"
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");
}
enum Option
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SORT,
	PRINT
};
int main()
{
	int input = 0;
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			//增加
			break;
		case DEL:
			//删除
			break;
		case SEARCH:
			//查找
			break;
		case MODIFY:
			//修改
			break;
		case SORT:
			//排序
			break;
		case PRINT:
			//打印
			break;
		case EXIT:
			//退出
			break;
		default:
			break;
		}
	} while (input);
	return 0;
}
  • Notice:

When the menu is printed out, it is necessary to select a number to implement the corresponding function. In order to facilitate the coders to know what the corresponding number is, the enumeration type enum keyword is used above.

(2) Initialization function

Before initializing the address book, you must first create an address book:

  • contact.h file:
#pragma once
#include<stdio.h>
#define MAX_NAME 20
#define MAI_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30
#define MAX 1000
//存放每一个联系人的信息内容
typedef struct PeoInfo
{
	char name[MAX_NAME];
	char sex[MAI_SEX];
	int age;
	char tele[MAX_TELE];
	char addr[MAX_ADDR];
}PeoInfo;
//创建通讯录
typedef struct Contact
{
	PeoInfo data[MAX]; //存放添加进来人的信息
	int sz; //记录当前通讯录中有效信息的个数
}Contact;

After creating the address book, the next step is to initialize

  • contact.c file:
void InitContact(Contact* pc)
{
	pc->sz = 0;
	//memset(); 内存设置
	memset(pc->data, 0, sizeof(pc->data));
}
  • contact.h file:
void InitContact(Contact* pc);
  • test.c file:

(3) Add contacts and print

  • contact.h file
void AddContact(Contact* pc);
  • contact.c file
void AddContact(Contact* pc)
{
	if (pc->sz == MAX)
	{
		printf("通讯录已满,无法添加\n");
		return;
	}
	//增加一个人的信息
	printf("请输入名字:>");
	scanf("%s", pc->data[pc->sz].name);
	printf("请输入年龄:>");
	scanf("%s", &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");
}

After adding a contact, I don't know if the addition was successful, so we need to print it out to see

  • contact.h file
void PrintContact(const Contact* pc);
  • contact.c file
//打印联系人信息
void PrintContact(const Contact* pc)
{
	if (pc->sz == 0)
	{
		printf("通讯录为空!\n");
		return;
	}
	int i = 0;
	//打印标题
	printf("%-12s\t%-5s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
	//打印数据
	for (i = 0; i < pc->sz; i++)
	{
		printf("%-12s\t%-5d\t%-5s\t%-12s\t%-20s\n",
			    pc->data[i].name,           
			    pc->data[i].age, 
			    pc->data[i].sex, 
	            pc->data[i].tele, 
		        pc->data[i].addr
		       );
	}
}
  • The operation effect is as follows:

(4) Delete the specified contact

  • Before we delete the specified contact, we must first find out whether the person to be deleted exists.
	//1、得先查找要删除的人
	//有/没有
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("要删除的人不存在\n");
		return;
	}
  • Next, the process of implementing the FindByName function is as follows:
//查找要删除的联系人
static int FindByName(Contact* pc, char name[])
{
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1; //找不到
}
  • After finding the person to be deleted, the next step is to perform the delete operation. The total code is as follows:
  • contact.c file
//删除联系人信息
void DelContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	if (pc->sz == 0)
	{
		printf("通讯录为空,无需删除\n");
		return;
	}
	printf("请输入要删除人的名字:>");
	scanf("%s", name);
	//1、得先查找要删除的人
	//有/没有
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("要删除的人不存在\n");
		return;
	}
	//2、删除
	int i = 0;
	for (i = pos; i < pc->sz - 1; i++)
	{
		pc->data[i] = pc->data[i + 1];
	}
	pc->sz--;
	printf("删除成功\n");
}
  • The test results are as follows:

(5) Find the specified contact and print

  • contact.h file:
void SearchContact(Contact* pc);
  • contact.c file:
//查找指定联系人
void SearchContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	printf("请输入要查找人的名字:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("要查找的人不存在\n");
		return;
	}
	else
	{
		int i = 0;
		//打印标题
		printf("%-12s\t%-5s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
		//打印数据
		printf("%-12s\t%-5d\t%-5s\t%-12s\t%-20s\n",
			pc->data[pos].name,
			pc->data[pos].age,
			pc->data[pos].sex,
			pc->data[pos].tele,
			pc->data[pos].addr);
	}
}
  • The results are as follows:

(6) Modify the information of the designated person

  • contact.h file:
void ModifyContact(Contact* pc);
  • contact.c file:
//修改指定联系人
void ModifyContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	printf("请输入要修改人的名字:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("要修改的人不存在\n");
		return;
	}
	else
	{
		printf("请输入名字:>");
		scanf("%s", pc->data[pos].name);
		printf("请输入年龄:>");
		scanf("%d", &pc->data[pos].age);
		printf("请输入性别:>");
		scanf("%s", &pc->data[pos].sex);
		printf("请输入电话:>");
		scanf("%s", pc->data[pos].tele);
		printf("请输入地址:>");
		scanf("%s", pc->data[pos].addr);
		printf("修改成功\n");
	}
}
  • The results are as follows:

(7) Print all information

  • In fact, this code has already appeared before, and then review it:
//打印联系人信息
void PrintContact(const Contact* pc)
{
	if (pc->sz == 0)
	{
		printf("通讯录为空!\n");
		return;
	}
	int i = 0;
	//打印标题
	printf("%-12s\t%-5s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
	//打印数据
	for (i = 0; i < pc->sz; i++)
	{
		printf("%-12s\t%-5d\t%-5s\t%-12s\t%-20s\n",
			    pc->data[i].name,           
			    pc->data[i].age, 
			    pc->data[i].sex, 
	            pc->data[i].tele, 
		        pc->data[i].addr
		       );
	}
}

(8) Sort address book information

  • contact.c file
//通过姓名排序
int CmpByName(const void* e1, const void* e2)
{
	return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name);
}
//通过年龄排序
int CmpByAge(const void* e1, const void* e2)
{
	return ((PeoInfo*)e1)->age - ((PeoInfo*)e2)->age;
}
//排序通讯录中联系人的先后顺序
void SortContact(Contact* ps)
{
	int input = 0;
	printf("1、姓名\t2、年龄\n");
	printf("请选择你要排序的方式:>");
	scanf("%d", &input);
	switch (input)
	{
	case 1:
		qsort(ps->data, ps->sz, sizeof(PeoInfo), CmpByName);//排序
		printf("排序成功\n");
		break;
	case 2:
		qsort(ps->data, ps->sz, sizeof(PeoInfo), CmpByAge);//排序
		printf("排序成功\n");
		break;
	default:
		printf("请输入有效数字\n");
	}
}

3. Overall code

  • contact.h file:
#pragma once
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_NAME 20
#define MAI_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30
#define MAX 1000

//存放每个人的信息
typedef struct PeoInfo
{
	char name[MAX_NAME];
	char sex[MAI_SEX];
	int age;
	char tele[MAX_TELE];
	char addr[MAX_ADDR];
}PeoInfo;

//创建通讯录
typedef struct Contact
{
	PeoInfo data[MAX]; //存放添加进来人的信息
	int sz; //记录当前通讯录中有效信息的个数

}Contact;

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

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

//打印联系人信息
void PrintContact(const Contact* pc);

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

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

//修改指定联系人
void ModifyContact(Contact* pc);

//排序联系人
void SortContact(Contact* ps);
  • contact.c file:
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
//初始化通讯录
void InitContact(Contact* pc)
{
	pc->sz = 0;
	//memset(); 内存设置
	memset(pc->data, 0, sizeof(pc->data));
}
//添加联系人
void AddContact(Contact* 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");
}

//打印联系人信息
void PrintContact(const Contact* pc)
{
	if (pc->sz == 0)
	{
		printf("通讯录为空!\n");
		return;
	}
	int i = 0;
	//打印标题
	printf("%-12s\t%-5s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
	//打印数据
	for (i = 0; i < pc->sz; i++)
	{
		printf("%-12s\t%-5d\t%-5s\t%-12s\t%-20s\n",
			    pc->data[i].name,           
			    pc->data[i].age, 
			    pc->data[i].sex, 
	            pc->data[i].tele, 
		        pc->data[i].addr
		       );
	}
}

//查找要删除的联系人
static int FindByName(Contact* pc, char name[])
{
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(pc->data[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1; //找不到
}

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


//查找指定联系人
void SearchContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	printf("请输入要查找人的名字:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("要查找的人不存在\n");
		return;
	}
	else
	{
		int i = 0;
		//打印标题
		printf("%-12s\t%-5s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
		//打印数据
		printf("%-12s\t%-5d\t%-5s\t%-12s\t%-20s\n",
			pc->data[pos].name,
			pc->data[pos].age,
			pc->data[pos].sex,
			pc->data[pos].tele,
			pc->data[pos].addr);
	}
}

//修改指定联系人
void ModifyContact(Contact* pc)
{
	char name[MAX_NAME] = { 0 };
	printf("请输入要修改人的名字:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("要修改的人不存在\n");
		return;
	}
	else
	{
		printf("请输入名字:>");
		scanf("%s", pc->data[pos].name);
		printf("请输入年龄:>");
		scanf("%d", &pc->data[pos].age);
		printf("请输入性别:>");
		scanf("%s", &pc->data[pos].sex);
		printf("请输入电话:>");
		scanf("%s", pc->data[pos].tele);
		printf("请输入地址:>");
		scanf("%s", pc->data[pos].addr);
		printf("修改成功\n");
	}
}

//通过姓名排序
int CmpByName(const void* e1, const void* e2)
{
	return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name);
}
//通过年龄排序
int CmpByAge(const void* e1, const void* e2)
{
	return ((PeoInfo*)e1)->age - ((PeoInfo*)e2)->age;
}
//排序通讯录中联系人的先后顺序
void SortContact(Contact* ps)
{
	int input = 0;
	printf("1、姓名\t2、年龄\n");
	printf("请选择你要排序的方式:>");
	scanf("%d", &input);
	switch (input)
	{
	case 1:
		qsort(ps->data, ps->sz, sizeof(PeoInfo), CmpByName);//排序
		printf("排序成功\n");
		break;
	case 2:
		qsort(ps->data, ps->sz, sizeof(PeoInfo), CmpByAge);//排序
		printf("排序成功\n");
		break;
	default:
		printf("请输入有效数字\n");
	}
}
  • test.c file:
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
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");
}
enum Option
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SORT,
	PRINT
};
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:
			//排序
			SortContact(&con);
			break;
		case PRINT:
			//打印
			PrintContact(&con);
			break;
		case EXIT:
			//退出
			printf("退出通讯录\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	} while (input);
	return 0;
}

Guess you like

Origin blog.csdn.net/bit_zyx/article/details/122772332