注意:这篇文章上次更新于1836天前,文章内容可能已经过时。
This article was last updated1836 days ago, the content may be outdated.
系统需求
通讯录是一个可以记录亲人、好友信息的工具。
本教程主要利用C++来实现一个通讯录管理系统
系统中需要实现的功能如下:
- 添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
- 显示联系人:显示通讯录中所有联系人信息
- 删除联系人:按照姓名进行删除指定联系人
- 查找联系人:按照姓名查看指定联系人信息
- 修改联系人:按照姓名重新修改指定联系人
- 清空联系人:清空通讯录中所有信息
- 退出通讯录:退出当前使用的通讯录
System Requirements
An address book is a tool that can record the information of family and friends.
This tutorial mainly uses C++ to implement an address book management system
The functions that need to be implemented in the system are as follows:
- Add contact: add a new person to the address book, with information including (name, gender, age, phone number, home address), recording up to 1000 people
- Display contacts: display the information of all contacts in the address book
- Delete contact: delete the specified contact by name
- Find contact: view the information of the specified contact by name
- Modify contact: re-modify the specified contact by name
- Clear contacts: clear all information in the address book
- Exit address book: exit the address book currently in use
Create the Project
After opening VS2017, click “Create New Project” to create a new C++ project

Fill in the project name and select the project path

Add Files


After adding successfully, the effect is as shown:

At this point, the project has been created
Menu Function
Function description: The interface where the user selects functions
The menu interface effect is shown below:

Steps:
- Encapsulate a function to display this interface, such as
void showMenu() - Call the encapsulated function in the main function
Code:
1 |
|
Exit Function
Function description: exit the address book management system
Approach: according to the user’s different choices, different functions are entered. A switch branch structure can be chosen to build the entire architecture
When the user selects 0, exit is executed; when other options are selected, no operation is performed and the program will not exit
Code:
1 | int main() { |
效果图:

Screenshot of the effect:

添加联系人
功能描述:
实现添加联系人功能,联系人上限为1000人,联系人信息包括(姓名、性别、年龄、联系电话、家庭住址)
添加联系人实现步骤:
- 设计联系人结构体
- 设计通讯录结构体
- main函数中创建通讯录
- 封装添加联系人函数
- 测试添加联系人功能
Add Contact
Function description:
Implement the function of adding contacts. The upper limit of contacts is 1000 people, and the contact information includes (name, gender, age, phone number, home address)
Steps to implement adding contacts:
- Design the contact structure
- Design the address book structure
- Create the address book in the main function
- Encapsulate the add-contact function
- Test the add-contact function
Design the Contact Structure
The contact information includes: name, gender, age, phone number, home address
Design as follows:
1 |
|
Design the Address Book Structure
When designing, we can maintain an array with a capacity of 1000 for storing contacts in the address book structure, and record the current number of contacts in the address book
Design as follows
1 |
|
Create the Address Book in the main Function
After the add-contact function is encapsulated, create an address book variable in the main function; this is the address book we need to maintain all along
1 | mian函数起始位置添加: |
Encapsulate the Add-Contact Function
Approach: before adding a contact, first check whether the address book is full. If it is full, no more contacts will be added. If it is not full, add the new contact’s information to the address book one by one
The add-contact code:
1 | //1、添加联系人信息 |
Test the Add-Contact Function
In the selection interface, if the player selects 1, it means adding a contact; we can test this function
In the switch case statement, add it in case 1:
1 | case 1: //添加联系人 |
测试效果如图:

The test result is shown below:

Display Contacts
Function description: display the contact information already in the address book
Steps to implement displaying contacts:
- Encapsulate the display-contacts function
- Test the display-contacts function
Encapsulate the Display-Contacts Function
Approach: check whether there are people in the current address book. If there are no people, prompt that the records are empty; if the number is greater than 0, display the information in the address book
The display-contacts code:
1 | //2、显示所有联系人信息 |
1 | case 2: //显示联系人 |
测试效果如图:

The test result is shown below:

Delete Contact
Function description: delete the specified contact by name
Steps to implement deleting contacts:
- Encapsulate the check for whether a contact exists
- Encapsulate the delete-contact function
- Test the delete-contact function
封装检测联系人是否存在
设计思路:
删除联系人前,我们需要先判断用户输入的联系人是否存在,如果存在删除,不存在提示用户没有要删除的联系人
因此我们可以把检测联系人是否存在封装成一个函数中,如果存在,返回联系人在通讯录中的位置,不存在返回-1
检测联系人是否存在代码:
Encapsulate the Check for Whether a Contact Exists
Design approach:
Before deleting a contact, we need to first determine whether the contact entered by the user exists. If it exists, delete it; if it does not exist, prompt the user that there is no contact to delete
Therefore, we can encapsulate the check for whether a contact exists into a function. If the contact exists, return its position in the address book; if it does not exist, return -1
The code for checking whether a contact exists:
1 | //判断是否存在查询的人员,存在返回在数组中索引位置,不存在返回-1 |
Encapsulate the Delete-Contact Function
Determine whether the person is in the address book based on the contact entered by the user
If found, delete the contact and prompt that the deletion succeeded
If not found, prompt that no such person exists.
1 | //3、删除指定联系人信息 |
1 | case 3: //删除联系人 |
测试效果如图:
存在情况:

不存在情况:

The test result is shown below:
When the contact exists:

When the contact does not exist:

Find Contact
Function description: view the information of the specified contact by name
Steps to implement finding a contact
- Encapsulate the find-contact function
- Test finding the specified contact
Encapsulate the Find-Contact Function
Implementation approach: determine whether the contact specified by the user exists. If it exists, display the information; if it does not exist, prompt that no such person exists.
The find-contact code:
1 | //4、查找指定联系人信息 |
1 | case 4: //查找联系人 |
测试效果如图
存在情况:

不存在情况:

The test result is shown below
When the contact exists:

When the contact does not exist:

Modify Contact
Function description: re-modify the specified contact by name
Steps to implement modifying a contact
- Encapsulate the modify-contact function
- Test the modify-contact function
Encapsulate the Modify-Contact Function
Implementation approach: find the contact entered by the user. If the search succeeds, perform the modification; if it fails, prompt that no such person exists
The modify-contact code:
1 | //5、修改指定联系人信息 |
1 | case 5: //修改联系人 |
测试效果如图:
查不到指定联系人情况:

查找到联系人,并修改成功:

再次查看通讯录,确认修改完毕

The test result is shown below:
When the specified contact cannot be found:

The contact is found and modified successfully:

Check the address book again to confirm the modification is complete

Clear Contacts
Function description: clear all information in the address book
Steps to implement clearing contacts
- Encapsulate the clear-contacts function
- Test clearing contacts
Encapsulate the Clear-Contacts Function
Implementation approach: to clear all contact information in the address book, simply set the number of contacts recorded in the address book to 0 to perform a logical clear.
The clear-contacts code:
1 | //6、清空所有联系人 |
1 | case 6: //清空联系人 |
测试效果如图:
清空通讯录

再次查看信息,显示记录为空

至此,通讯录管理系统完成!
The test result is shown below:
Clear the address book

Check the information again, and the records are shown to be empty

At this point, the address book management system is complete!
1 |
|


