C language - address book management system

Address Book Management System Project Introduction

Function Description

  1. Console black window implementation
  2. The program needs to meet the following functions
    Insert image description here
  3. When the program starts running, the selection menu interface is first displayed, and the function is determined based on user input.

Program interface

Insert image description here

Code

Multiple file implementation

Similar to the actual project written before, the multi-file implementation method is also used here. The
multi-file code writing method can make the logical structure of the code we write clearer. The form of multiple file implementation of a project is also consistent with the actual work. The implementation process of the project helps us develop good programming habits.
Insert image description here
Address_Book.h : Contains all header files and function declarations used in the project, as well as some macro definitions and structure declarations.
Address_Book.c : This .c file is used to implement most of the basic functions in the project (excluding the main function implementation)
test.c : project main function file, project main logic implementation (including main function)

Project logic

Insert image description here

header file part

Contains all header files to be referenced by the project, and some macro definitions

//↓↓↓↓↓引入要用的头文件↓↓↓↓↓
#include <stdio.h>
#include<stdlib.h>//清屏函数的头文件
#include <string.h>


//↓↓↓↓↓使用到的宏定义↓↓↓↓↓
#define MAX_NUM 100//通讯录最多存储100个联系人
#define FORMAT "%-10s %-10s %-10d %-25s %-30s\n"
#define DATA ptxl->peoples[i].name, ptxl->peoples[i].sex, ptxl->peoples[i].age, ptxl->peoples[i].phoneNumber, ptxl->peoples[i].address
//这里定义FORMAT和DATA是为了后面打印显示通讯录方便简洁,避免出现同一段代码重复出现多次的情况
//避免代码冗余

The implementation of the address book management system is based on structures and structure arrays. Describing a contact requires the use of multiple types of data. This requires defining a structure that describes a single contact. The code is as follows:

//创建联系人结构体
struct People
{
    
    
	char name[20];
	char sex[4];
	int age;
	char phoneNumber[12];//电话号码一般是11位数,后面加一位'\0'
	char address[30];
};

However, describing multiple contacts in an address book requires a structure array. At the same time, in order to better count the number of contacts recorded in the address book, an integer variable count is also needed.Add a contact and the count will increase by one; delete a contact and the count will decrease by one.;In order to realize the binding relationship between count and address book (structure array), an address book structure is defined here. One of the structure members is a structure array that stores contact information, and the other is a statistical contact. The number of integer elements count.

//创建通讯录,也就是联系人数组,最大容量为MAX_NUM,宏定义为100
struct Txl
{
    
    
	struct People peoples[MAX_NUM];
	int count ;
};

At the same time, the header file also contains the declaration of the functions in the project

//↓↓↓↓↓函数声明↓↓↓↓↓
void menu();//菜单函数
void initiate(struct Txl *ptxl); //初始化通讯录总数为0
void Add(struct Txl* ptxl);//添加联系人的函数
void Show(struct Txl* ptxl);//显示联系人的函数
void Find(struct Txl* ptxl);//查找联系人的函数
void Change(struct Txl* ptxl);//修改联系人的函数
void Delete(struct Txl* ptxl);//删除联系人的函数
void Clear(struct Txl* ptxl);//清空通讯录

One thing to note here is that the parameters of these functions are all structure pointer types, not structures.
This is because when passing structure parameters, it is recommended to pass the address of the structure.
When a function passes parameters, the parameters need to be pushed onto the stack, which will cause system overhead in time and space. If a structure object is passed and the structure object is too large, the system overhead of pushing parameters onto the stack will be relatively large. If it is large, it will cause performance degradation of the program.

Internal function implementation

Implementation of menu() function

The implementation of the menu function is relatively simple, mainly the printf function. The code is as follows:

//打印选择菜单
void menu()
{
    
    
	printf("********************************\n");
	printf("********  1.添加联系人  ********\n");
	printf("********  2.显示联系人  ********\n");
	printf("********  3.删除联系人  ********\n");
	printf("********  4.查找联系人  ********\n");
	printf("********  5.修改联系人  ********\n");
	printf("********  6.清空联系人  ********\n");
	printf("********  0.退出通讯录  ********\n");
	printf("********************************\n");
}

Implementation of Add(struct Txl *ptxl) function

The Add function is mainly an access operation of the structure array, but before that, it is necessary to determine whether the address book is full, that is, whether the value of the count member in the address book is equal to MAX_NUM (the defined maximum capacity of the address book) , if yes, output the prompt, if not, then access a single structure member in the structure array, the code is as follows:

//添加联系人
void Add(struct Txl* ptxl)
{
    
    
	if (ptxl->count == MAX_NUM)
	{
    
    
		printf("通讯录已满!不能再添加联系人了~\n");
	}
	else
	{
    
    
		//添加姓名
		printf("姓名:");
		scanf("%s", ptxl->peoples[ptxl->count].name);

		//添加性别
		printf("性别(男 或 女):");
		scanf("%s", ptxl->peoples[ptxl->count].sex);

		//添加年龄
		printf("年龄:");
		scanf("%d", &ptxl->peoples[ptxl->count].age);//这里要取地址操作符!!!

		//添加联系电话
		printf("联系电话:");
		scanf("%s", ptxl->peoples[ptxl->count].phoneNumber);

		//添加地址
		printf("地址:");
		scanf("%s", ptxl->peoples[ptxl->count].address);

		(ptxl->count)++;
		printf("添加联系人成功!\n");
	}
}

Implementation of Show (struct Txl *ptxl) function

The implementation of the Show function is also relatively simple. Just loop through and print the members in the structure array. The condition of the loop is that it is less than the value of the count variable in the address book structure. The code is as follows:

//显示联系人
void Show(struct Txl* ptxl)
{
    
    
	int i = 0;
	printf("%-10s %-10s %-10s %-25s %-30s\n", "姓名", "性别", "年龄", "联系电话", "地址");
	for (i = 0; i < (ptxl->count); i++)
	{
    
    
		printf(FORMAT,DATA);
	}
}

Implementation of Delete(struct Txl *ptxl) function

Delete the specified contact in the structure array based on the name information entered by the user

  1. First, define an array of character type to receive the user’s input name information.
  2. Then, iterate through the name member of each element of the structure array
  3. Use the strcmp string comparison function to compare the user input with the name member of each element of the structure array, and use ret to receive the return value.
  4. If the return value is 0, then the deletion operation is performed (that is, starting from the current position of the structure array members, assigning the next element to the previous element, until the entire structure array is cycled). Simply put, it is to overwrite the previous elements with the later elements. Elements
  5. Then reduce the count describing the total number of contacts in the address book by one.

However, the last element in the structure array here has not been overwritten but has not been deleted. However, because the value of count has been decremented by one, when the structure array is printed later, although the last element has not been overwritten or deleted, , but it won't print out either.

code show as below:

//删除联系人
void Delete(struct Txl* ptxl)
{
    
    
	char input[20] = {
    
    0};
	int i = 0;
	int flag = 0;
	printf("请输入你要删除的联系人姓名:");
	scanf("%s",input);
	for (i = 0; i < ptxl->count; i++)
	{
    
    
		int ret = strcmp(input,ptxl->peoples[i].name);
		if (ret == 0)
		{
    
    
			flag = 1;
			int j = 0;
			int k = i;
			for (j = 0; j <(ptxl->count) - i-1; j++)
			{
    
    
				ptxl->peoples[k] = ptxl->peoples[k+1];
				k++; 
			}
			printf("删除联系人成功~\n");
			ptxl->count--;
			break;
		}
	}
	if (flag != 1)
	{
    
    
		printf("没有找到此联系人!\n");
	}
}

A flag is also used to mark whether the string is matched successfully. If successful, the deletion operation is performed and the loop is jumped out. Otherwise, a prompt is output.

Implementation of Find(struct Txl *ptxl) function

The implementation of the Find function is similar to the Delete function. It also traverses the structure array and uses the strcmp function to match. If a match is found, it will be printed out. If no match is found, a prompt will be output.

//查找联系人
void Find(struct Txl* ptxl)
{
    
    
	char input[20] = {
    
     0 };
	printf("请输入你要查找的联系人的姓名:");
	scanf("%s", &input);
	int i = 0;
	int flag = 0;//定义一个标志,找到了置为1;
	for (i = 0; i < ptxl->count; i++)
	{
    
    
		int ret = strcmp(ptxl->peoples[i].name, input);
		if (ret == 0)
		{
    
    
			printf("查找成功,该联系人相关信息如下↓↓↓\n");
			printf("%-10s %-10s %-10s %-25s %-30s\n", "姓名", "性别", "年龄", "联系电话", "地址");
			printf(FORMAT, DATA);
			flag = 1;
			break;
		}
	}
	if (flag == 0)
	{
    
    
		printf("查找失败!通讯录中没有此联系人信息!\n");
	}
}

The implementation of the following functions are similar, and the framework structures are similar, so I won’t go into details.


Implementation of Change(struct Txl *ptxl) function

//修改联系人
void Change(struct Txl* ptxl)
{
    
    
	char input[20] = {
    
     0 };
	printf("请输入你要修改的联系人的姓名:");
	scanf("%s", &input);
	int i = 0;
	int flag = 0;
	for (i = 0; i < ptxl->count; i++)
	{
    
    
		int ret = strcmp(ptxl->peoples[i].name, input);
		if (ret == 0)
		{
    
    
			//姓名
			printf("姓名:");
			scanf("%s", ptxl->peoples[i].name);

			//添加性别
			printf("性别(男 或 女):");
			scanf("%s", ptxl->peoples[i].sex);

			//添加年龄
			printf("年龄:");
			scanf("%d", &ptxl->peoples[i].age);//这里要取地址操作符!!!

			//添加联系电话
			printf("联系电话:");
			scanf("%s", ptxl->peoples[i].phoneNumber);

			//添加地址
			printf("地址:");
			scanf("%s", ptxl->peoples[i].address);

			printf("联系人信息修改成功!\n");
			flag = 1;
			break;
		}
	}
	if (flag == 0)
	{
    
    
		printf("此联系人不在通讯录中!无法修改!\n");
	}
}

Implementation of Clear(struct Txl *ptxl) function

void Clear(struct Txl* ptxl)
{
    
    
	ptxl->count = 0;
	printf("通讯录清空成功~~~\n");
	//这里只是简单的把结构体txl中的count值设置为0,
	//这样打印的时候就什么都不会打印,看起来像是清空了通讯录
	//实际上内存中还是存在数据的,程序结束前并没有把数组中的数据清除
	//这里具体后面在想办法改善//动态内存管理相关内容
}

Main program code

#include "Address_Book.h"//包含自己写的头文件

int main()
{
    
    
	int input = 0;
	struct Txl txl;
	initiate(&txl);
	do 
	{
    
    
		menu();
		printf("请选择你要进行的操作->");
		scanf("%d",&input);
		switch (input)
		{
    
    
		case 1:
			//Add
			Add(&txl);
			break;
		case 2:
			//Show
			Show(&txl);
			break;
		case 3:
			Delete(&txl);
			break;
		case 4:
			Find(&txl);
			break;
		case 5:
			Change(&txl);
			break;
		case 6:
			Clear(&txl);//这里只是简单的把结构体txl中的count值设置为0,这样打印的时候就什么都不会打印,看起来像是清空了通讯录,实际上数据还是存在数组中的!!
			break;
		case 0:
			//Exit
			printf("退出系统~~~\n");
			break;
		default:
			printf("选择错误,请输入0~6 的数字!\n");
			break;
		}
	} while (input);
	return 0;
}

Thinking and summarizing

This part of the code is still very honorable

  1. The above functions can use transfer tables (function pointers for optimization), and their parameters and return value types are the same.
  2. I also discovered when I wrote the code later that the code that traverses the structure array and then uses the strcmp library function to perform string matching appears many times. It is very redundant and can be encapsulated into a function.
  3. The operations of deleting and clearing contacts do not actually clear the data and occupied space. You can use dynamic memory related knowledge for optimization in the future.
  4. You can add a sorting function to the address book by name, age, etc.
  5. Modifying contacts can be optimized. Specifically what attributes should be modified?
  6. Linked list implementation?

Guess you like

Origin blog.csdn.net/yjagks/article/details/133035443
Recommended