C language - implementation of address book

content

Create a project environment

Create a structure

test.c file

Create an address book

add contacts

print address book

delete specified contact

find a contact

change contact

Arrange contacts

Complete address book

code

Epilogue


Create a project environment

For the implementation of this address book, we can think about how to think like writing a chess game, then we must first create a project environment

 This time, I will also go to sub-modules to write

First of all, let's think about what function the address book has. It needs to save everyone's information. For this information, we also need to add, delete, search, and modify the operation. Of course, the mobile phone address book we use will also be alphabetical. to sort by size.

At the beginning of the header file we created, we can still write our library functions, custom types, etc. here, and refer to "contact.h" in other source files.


Create a structure

First of all, we need to store a person's information. Here we create a structure to store the person's name, gender, age, etc.

Here it is renamed with tpyedef, and PeoInfo will be this structure in the future


test.c file

The starting file prints a menu, and after writing it, a switch statement is used

can be written according to our menu

 


Create an address book

The following Contact is the address book we created, which can store the information of 1,000 individuals, and the number of sz records.

The code just now can also be defined with define, which is convenient for us to modify this value later.

Then you can use it in the test.c file 

 Of course, we can also use the enumeration mentioned in the previous article , so that it can match the menu

 The following switch can also be rewritten like this

Of course, you can also get the enumeration in the header file 


add contacts

Before adding contacts, we did not initialize the members in the Contact structure, so the next step is to initialize the address book.

 Use memset to initialize the entire array to 0

After that, you can write additional functions, and you also need to declare them in the header file.

 


print address book

In the previous step, we wrote a function to add contacts, and then we wrote a function to print the address book to test whether the information of the added contacts was passed in.

 

 After testing, the entered information has been passed into the address book.


delete specified contact

When we want to delete a specified contact, we must first determine whether there is any data in the address book. Then we need to find the person we want to delete, and then find the contact and change the contact to find the contact, so here we can write a function to help us find it, and we can delete it after we find it.


find a contact

When we want to realize the search, many of the previous codes can be applied, such as when the address book is empty, when the address book cannot be found, and when the print is found, these codes have also been written before.


change contact

 Not much to say here, the code has been written before, and it can be used flexibly


Arrange contacts

        When sorting contacts, you can use the qsort function of the previous pointer chapter, which is also very convenient, but here is the structure sorting, parameter 1: pass in the starting position to be sorted, since it is to be sorted, point to the first position of the contact One; parameter 2: the number of elements, that is, how many people are in the address book; parameter 3: the bytes of an element, that is, the byte size of the entire person; parameter 4: the function address for sorting.

        When writing the sorting function, it is different from arranging integer arrays. Here, e1 and e2 are cast into structure pointers. There is no need to dereference here. The pointers are directly used ->. In our daily address book, Generally, the first letter of the contact is used to compare, so here is the name to compare directly. Name is a string, and strcmp is used to compare.


Complete address book

       At this point, the address book is basically completed. Finally, let’s see what can be optimized. For example, the functions of searching and printing do not need to be modified, but we do not want the target structure to change, so we add const to modify it; One point is that when the structure is passed parameters, use the call by reference (the previous article also talked about why the structure should be passed by address instead of by value). Since the pointer is used for the transfer of the address, it is still asserted when the function is called. good.


code

All codes are as follows

contact.h file

#pragma once

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

//类型的声明

#define MAX 1000
#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 12
#define ADDR_MAX 30

enum Option
{
	EXIT,//0
	ADD,//1
	DEL,//2
	SEARCH,//3
	MODIFY,//4
	SORT,//5
	PRINT//6
};

typedef struct PeoInfo
{
	//存放一个人的信息
	char name[NAME_MAX];
	char sex[SEX_MAX];
	int age;
	char tele[TELE_MAX];
	char addr[ADDR_MAX];
}PeoInfo;

typedef struct Contact
{
	PeoInfo data[MAX];//存放1000个人的信息
	int sz;//记录通讯录中已经保存的信息个数
}Contact;

//函数的声明

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

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

//打印通讯录
void PrintContact(const Contact* pc);

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

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

//更改联系人
void ModifyContact(Contact* pc);

//排列联系人
void SortContact(Contact* pc);

const.c file

#define _CRT_SECURE_NO_WARNINGS 1

#include "contact.h"

void InitContact(Contact* pc)
{
	assert(pc);
	pc->sz = 0;
	memset(pc->data, 0, sizeof(pc->data));
}

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");

}

void PrintContact(const Contact* pc)
{
	assert(pc);
	printf("%-20s %-5s %-5s %-12s %-30s\n", "姓名", "年龄", "性别", "电话", "地址");
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		printf("%-20s %-5d %-5s %-12s %-30s\n", pc->data[i].name, pc->data[i].age,
			pc->data[i].sex, pc->data[i].tele, pc->data[i].addr);
	}
}

//查找函数,找到返回下标,找不到返回-1
int FindByName(const Contact* pc, char name[])
{
	assert(pc);
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (0 == strcmp(pc->data[i].name, name))
			return i;
	}
	return -1;

}

void DelContact(Contact* pc)
{
	assert(pc);
	if (pc->sz == 0)
	{
		printf("通讯录已空,无法删除\n");
		return;
	}
	//1. 找到指定联系人
	char name[NAME_MAX] = { 0 };
	printf("请输入要删除人的名字:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("要删除的人不存在\n");
		return;
	}
	//删除
	int j = 0;
	for (j = pos; j < pc->sz - 1; j++)
	{
		pc->data[j] = pc->data[j + 1];
	}
	pc->sz--;
	printf("删除成功\n");
}

void SearchContact(const Contact* pc)
{
	assert(pc);
	if (pc->sz == 0)
	{
		printf("通讯录已空,无法查找\n");
		return;
	}
	//1. 找到指定联系人
	char name[NAME_MAX] = { 0 };
	printf("请输入要查找人的名字:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("要查找的人不存在\n");
		return;
	}
	//找到了,打印
	printf("%-20s %-5s %-5s %-12s %-30s\n", "姓名", "年龄", "性别", "电话", "地址");
	printf("%-20s %-5d %-5s %-12s %-30s\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)
{
	assert(pc);
	if (pc->sz == 0)
	{
		printf("通讯录已空,无法更改\n");
		return;
	}
	//1. 找到指定联系人
	char name[NAME_MAX] = { 0 };
	printf("请输入要更改人的名字:>");
	scanf("%s", name);
	int pos = FindByName(pc, name);
	if (pos == -1)
	{
		printf("要更改的人不存在\n");
		return;
	}
	//更改信息
	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 cmp_name(const void* e1, const void* e2)
{
	assert(e1 && e2);
	return strcmp(((PeoInfo*)e1)->name, (((PeoInfo*)e2)->name));
}
void SortContact(Contact* pc)
{
	assert(pc);
	qsort(pc->data, pc->sz, sizeof(pc->data[0]), cmp_name);
	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");
}	
void test()
{
	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 0:
			printf("退出通讯录\n");
			break;
		default:
			printf("选择错误,请重新选择\n");
			break;
		}
	} while(input);
}
int main()
{
	test();
	return 0;
}

Epilogue

The address book is finished, the so-called end is also the beginning, and more dry goods will be shared in the future, so stay tuned! ! !

Guess you like

Origin blog.csdn.net/m0_64607843/article/details/123770808