C language final course design - [address book management system] makes course design no longer painful

Table of contents

Summary

Chapter One Introduction

1.1 Significance of the project

1.2 Contacts function

Chapter 2 Detailed Design and Implementation 

2.1 Contact program operation flow chart

2.2 AddContact (add) function flow chart

2.3 DelContact (delete) function flow chart

2.4 SearchConact (search) function flow chart

2.5 ModifyContact (modify) function flow chart

2.6 SortContact (sorting) function flow chart

Chapter 3 System Testing

3.1 Menu

3.2 Add contact information

3.3 Delete contact information

3.4 Find contact information

 3.5 Modify contact information

3.6 Sort by contact information

 3.7 Print Contact Information

4. Experience 

5. Source code

5.1 DynamicContact.h

5.2 DynamicContact.c


This blog is the homework of the blogger's C language course design at the end of the term. It is written as a template and shared with everyone. You can learn from it as needed.

Summary

This paper introduces an address book management system based on C language. The system can realize functions such as adding, searching, modifying, deleting, displaying and sorting contact information, and provides an easy-to-use user interface. Use C language structures and pointers to store and manage contact information, and use file read and write functions to save and access contact information. In the program design phase, we used a modular design to divide different functions into different functions to improve the readability and maintainability of the program.

The interactive interface of the system is simple and clear. Users can add, delete, search, and modify contact information through a few simple input commands. All operations are reflected on a visual menu, which is convenient for users to understand the current operation and system status. The address book system is also capable of performing some other useful functions, such as backing up and restoring contact data.

By using the efficiency and simplicity of C language, we designed an easy-to-use and maintain address book management system, making it suitable for a wide range of user groups. The system can be used in daily life and work to help users manage contact information and improve work efficiency and accuracy. For beginners, this system is also a good C language practice project, which can help them understand more deeply the concepts and usages of C language structures, pointers, and file reading and writing.

Keywords: address book management system, function, structure, pointer, file reading and writing


Chapter One Introduction

 The purpose of the C language course design, the significance of the software you developed, and what functions the software should achieve.

 The above is the requirement, the following is the template

1.1 Significance of the project

1. Improve programming ability: By implementing the address book management system, you can improve your C language programming ability and master the basic grammar and data structure of C language.

2. Practical project development: The implementation of the address book management system is a small project development, which allows students to master the process and methods of project development in practice.

3. Improve problem-solving ability: In the process of implementing the address book management system, students will encounter various problems, which require students to think and solve by themselves, so as to improve their problem-solving ability.

4. Cultivate the spirit of teamwork: The address book management system can be divided into multiple modules, which require the cooperation of multiple people to complete, and can cultivate the teamwork spirit of students.

5. Realize practical functions: The address book management system is a practical tool that can help users manage contact information and improve life efficiency.

1.2 Contacts function

1. Add contact: Enter the contact's name, phone number, address and other information, and save it to the address book.

2. Display contacts: display the information of all contacts in the address book.

3. Find contacts: Find contacts by name or phone number and display their details.

4. Modify contacts: Find contacts by name or phone number and modify their information.

5. Delete contacts: Find contacts by name or phone number and delete them from the address book.

6. Clear contacts: delete all contacts in the address book.

7. Save contacts: save the contact information in the address book to a file.

8. Read contacts: Read contact information from the file and add it to the address book.

9. Exit the system: Exit the address book management system.


Chapter 2  Detailed Design and Implementation 

This part is the C language implementation of the address book system design part, the flow chart of each function and the flow chart of the entire project

2.1 Contact program operation flow chart

2.2  AddContact (add) function flow chart

2.3  DelContact (delete) function flow chart

2.4  SearchConact (search) function flow chart

 

2.5  ModifyContact (modify) function flow chart

2.6  SortContact (sorting) function flow chart


Chapter 3 System Testing

3.1 Menu

3.2 Add contact information

3.3 Delete contact information

3.4 Find contact information

 3.5 Modify contact information

3.6 Sort by contact information

 3.7 Print Contact Information


4. Experience 

In the process of implementing the address book management system, I have learned a lot of skills and experience about C language programming. The following is my experience

1. The choice of data structure is very important. In the address book management system, I use a structure to store contact information, so that it is convenient to add, delete, modify and query contacts.

2. Pay attention to memory management. When using dynamic memory allocation functions (such as malloc and free), pay attention to releasing memory in time to avoid memory leaks.

3. Error handling is important. When writing a program, it is necessary to take into account various possible error conditions and deal with them to ensure the stability and reliability of the program.

4. Modular programming can improve the readability and maintainability of the code. When writing the address book management system, I divided the code of different functions into multiple modules, so that the code can be easily modified and maintained.

5. Debugging is an essential step in the programming process. When writing an address book management system, I often use debugging tools to find errors in the program so that they can be fixed in time.

In short, through the implementation of the address book management system, I not only learned a lot of skills and experience about C language programming, but also improved my programming ability and problem-solving ability.


5. Source code

5.1 DynamicContact.h

Address book function declaration part

#pragma once
#define _CRT_SECURE_NO_WARNINGS //vs 编译器需要,其他编译器不需要,可自行删去

//动态版通讯录

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

//类型声明

//PeoInit结构体所用
#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 12
#define ADDR_MAX 30
//通讯录初始状态的容量大小
#define DEFAULT_SZ 3

//枚举选项

enum Option //test函数所用的枚举
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SORT,
	PRINT
};

enum Modify //修改联系人所用的枚举
{
	EXIT0,
	NAME,
	SEX,
	AGE,
	TELE,
	ADDR
};

//结构体声明

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;//存放联系人的信息
	int count;//通讯录中已经保存的信息个数
	int capacity;//记录通讯录当前的最大容量
}Contact;


//函数声明

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

//销毁通讯录
void DestroyContact(Contact* p);

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

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

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

//修改联系人信息
void ModifyContact(Contact* p);

//打印联系人
void PrintContact(const Contact* p);

//排序
void SortContact(const Contact* p);

//保存通讯录的信息到文件
void SaveContact(const Contact* pc);

//加载文件信息到通讯录中
void LoadContact(Contact* p);

5.2 DynamicContact.c

Address book function implementation part

#include "DynamicContact.h"

//排序所用菜单
void menu2()
{
	printf("********************************\n");
	printf("******  1.name    2.age   ******\n");
	printf("******  0.exit            ******\n");
	printf("********************************\n");
}
//修改联系人所用的菜单
void menu1()
{
	printf("********************************\n");
	printf("******  1.name    2.sex   ******\n");
	printf("******  3.age     4.tele  ******\n");
	printf("******  5.addr    0.exit  ******\n");
	printf("********************************\n");

}

//检测通讯录容量
void CheckCapacity(Contact* p)
{
	assert(p);
	if (p->capacity == p->count)
	{
		PeoInfo* tmp = (PeoInfo*)realloc(p->data, (p->capacity + 2) * sizeof(PeoInfo));
		if (p->data != NULL)
		{
			p->data = tmp;
		}
		else
		{
			perror("CheckCapacity::realloc");
			return;
		}

		p->capacity += 2;
		printf("增容成功\n");
	}
}

//初始化通讯录
void InitContact(Contact* p)
{
	assert(p);
	p->count = 0;
	p->capacity = DEFAULT_SZ;
	p->data = (PeoInfo*)malloc(p->capacity * sizeof(PeoInfo));

	if (p->data == NULL)
	{
		perror("InitContact::malloc");
		return;
	}

	memset(p->data, 0, p->capacity * sizeof(PeoInfo));//把PeoInit全部初始化为0

	//加载文件信息到通讯录中
	LoadContact(p);
}

//销毁通讯录
void DestroyContact(Contact* p)
{
	free(p->data);
	p->data = NULL;
	p->capacity = 0;
	p->count = 0;

	printf("销毁成功\n");
}

//添加联系人
void AddContact(Contact* p)
{
	//检查容量
	CheckCapacity(p);

	//录入信息
	printf("请输入名字:>");
	scanf("%s", p->data[p->count].name);
	printf("请输入性别:>");
	scanf("%s", p->data[p->count].sex);
	printf("请输入年龄:>");
	scanf("%d", &(p->data[p->count].age));
	printf("请输入电话:>");
	scanf("%s", p->data[p->count].tele);
	printf("请输入地址:>");
	scanf("%s", p->data[p->count].addr);

	p->count++;
	printf("添加成功\n\n");
}

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

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

//查找联系人
void SearchContact(const Contact* p)
{
	assert(p);
	char name[NAME_MAX];
	printf("请输入要查找的名字:>");
	scanf("%s", name);
	int position = FindName(p, name);//查找
	if (-1 == position)
	{
		printf("要查找的联系人不存在\n\n");
		return;
	}
	printf("\n-----------------------------------------------\n");
	printf("%-10s %-5s %-5s %-12s %-30s\n", "姓名", "性别", "年龄", "电话", "地址");
	printf("%-10s %-5s %-5d %-12s %-30s\n", p->data[position].name,
		p->data[position].sex, p->data[position].age,
		p->data[position].tele, p->data[position].addr);
	printf("\n-----------------------------------------------\n\n");
}

//修改联系人信息
void ModifyContact(Contact* p)
{
	assert(p);
	int intput = 0;
	char name[NAME_MAX];
	printf("请输入要修改联系人的名字:>");
	scanf("%s", name);
	int position = FindName(p, name);//查找
	if (-1 != position)
	{
		printf("\n-----------------------------------------------\n");
		printf("%-10s %-5s %-5s %-12s %-30s\n", "姓名", "性别", "年龄", "电话", "地址");
		printf("%-10s %-5s %-5d %-12s %-30s\n", p->data[position].name, p->data[position].sex,
			p->data[position].age, p->data[position].tele, p->data[position].addr);
		printf("\n-----------------------------------------------\n\n");
		do
		{
			menu1();
			printf("请输入要修改的选项:>");
			scanf("%d", &intput);
			switch (intput)
			{
			case NAME:
				printf("请修改名字:>");
				scanf("%s", p->data[position].name);
				printf("修改成功\n\n");
				break;
			case SEX:
				printf("请修改性别:>");
				scanf("%s", p->data[position].sex);
				printf("修改成功\n\n");
				break;
			case AGE:
				printf("请修改年龄:>");
				scanf("%d", &(p->data[position].age));
				printf("修改成功\n\n");
				break;
			case TELE:
				printf("请修改电话号码:>");
				scanf("%s", p->data[position].tele);
				printf("修改成功\n\n");
				break;
			case ADDR:
				printf("请修改地址:>");
				scanf("%s", p->data[position].addr);
				printf("修改成功\n\n");
				break;
			case EXIT0:
				printf("退出修改\n\n");
				break;
			default:
				printf("选择错误,请重新选择\n\n");
				break;
			}
		} while (intput);
	}
	else
	{
		printf("所要修改的联系人不存在\n\n");
		return;
	}
}

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


int cmp_name(const void* e1, const void* e2)
{
	return strcmp(((struct PeoInfo*)e1)->name, ((struct PeoInfo*)e2)->name);
}

int cmp_age(const void* e1, const void* e2)
{
	return (((struct PeoInfo*)e1)->age - ((struct PeoInfo*)e2)->age);
}

//排序
void SortContact(const Contact* p)
{
	assert(p);
	int intput = 0;
	do
	{
		menu2();
		printf("请选择需要排序的选项:>");
		scanf("%d", &intput);
		switch (intput)
		{
		case 1:
			qsort(p->data, p->count, sizeof(struct PeoInfo), cmp_name);
			printf("按名字排序成功\n\n");
			break;
		case 2:
			qsort(p->data, p->count, sizeof(struct PeoInfo), cmp_age);
			printf("按年龄排序成功\n\n");
			break;
		case 0:
			printf("退出排序\n\n");
			break;
		default:
			printf("选择错误,请重新选择\n\n");
			break;
		}
	} while (intput);

}


//保存通讯录的信息到文件
void SaveContact(const Contact* p)
{
	//打开并创建文件
	FILE* pf = fopen("contact.data.txt", "w");//w:只写,
	if (pf == NULL)
	{
		perror("SaveContact::fopen");
		return;
	}

	//写文件
	int i = 0;
	for (i = 0; i < p->count; i++)
	{
		fwrite(p->data + i, sizeof(PeoInfo), 1, pf);
	}

	//关闭文件
	fclose(pf);
	pf = NULL;
}

//加载文件信息到通讯录中
void LoadContact(Contact* p)
{
	//打开文件
	FILE* pf = fopen("contact.data.txt", "r");//r:只读
	if (pf == NULL)
	{
		perror("LoadContact::fopen");
		return;
	}


	//读文件
	PeoInfo tmp = { 0 };
	while (fread(&tmp, sizeof(PeoInfo), 1, pf))
	{
		CheckCapacity(p);
		p->data[p->count] = tmp;
		p->count++;
	}

	//关闭文件
	fclose(pf);
	pf = NULL;

}

5.3 test.c

Address book system design test part

#include "DynamicContact.h"

void menu()
{
	printf("================================\n");
	printf("*********** Contact ************\n");
	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 intput = 0;
	Contact con;//创建通讯录
	InitContact(&con);//初始化通讯录
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &intput);
		switch (intput)
		{
		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:
			SaveContact(&con);//销毁通讯录之前把数据存入文件中
			DestroyContact(&con);
			printf("退出通讯录\n");
			break;
		default:
			printf("输入错误,请重新输入\n\n");
			break;
		}

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


This blog is based on the blogger Mr. Maple Leaf . If you want to learn the flow chart drawing method and tool acquisition in this article, you can go to follow this blogger! ! !

If you think the article is good, I look forward to your one-click triple link. Your encouragement is the source of motivation for my creation. Let us work together and see you at the top! ! !

Guess you like

Origin blog.csdn.net/qq_58286439/article/details/131130368