C++ address book management system
Address book management system
1. System requirements
The address book is a tool that can record the information of relatives 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 contacts: add new people to the address book, the information includes (name, gender, age, contact number, home address) to record up to 1000 people
- Show Contacts: Show all contact information in the address book
- Delete contact: delete the specified contact by name
- Find Contacts: View the specified contact information by name
- Modify Contact: Re-modify the specified contact according to the name
- Clear Contacts: Clear all information in the address book
- Exit address book: Exit the current address book
2. Create a project
The steps to create a project are as follows:
- Create new project
- add files
2.1 Create a project
After opening vs2017, click Create New Project to create a new C++ project
Fill in the project name and select the project path
2.2 Add files
So far, the project has been created
3. Menu function
Function description: The interface for the user to select the function
The menu interface effect is as follows:
step:
- The encapsulated function displays the interface as
void showMenu()
- Call the encapsulated function in the main function
Code:
#include<iostream>
using namespace std;
//菜单界面
void showMenu()
{
cout << "***************************" << endl;
cout << "***** 1、添加联系人 *****" << endl;
cout << "***** 2、显示联系人 *****" << endl;
cout << "***** 3、删除联系人 *****" << endl;
cout << "***** 4、查找联系人 *****" << endl;
cout << "***** 5、修改联系人 *****" << endl;
cout << "***** 6、清空联系人 *****" << endl;
cout << "***** 0、退出通讯录 *****" << endl;
cout << "***************************" << endl;
}
int main() {
showMenu();
system("pause");
return 0;
}
4. Exit function
Function description: Exit the address book system
Idea: According to the different choices of users, enter different functions, you can choose the switch branch structure, and build the entire structure
When the user selects 0, execute exit, select other to do nothing, and will not exit the program
Code:
int main() {
int select = 0;
while (true)
{
showMenu();
cin >> select;
switch (select)
{
case 1: //添加联系人
break;
case 2: //显示联系人
break;
case 3: //删除联系人
break;
case 4: //查找联系人
break;
case 5: //修改联系人
break;
case 6: //清空联系人
break;
case 0: //退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
5. Add contacts
Function description:
Realize the function of adding contacts, the maximum number of contacts is 1000, and the contact information includes (name, gender, age, contact number, home address)
Steps to add a contact:
- Design the Contact Structure
- Design the address book structure
- Create an address book in the main function
- Encapsulate the add contact function
- Test adding contacts
5.1 Designing the Contact Structure
Contact information includes: name, gender, age, contact number, home address
The design is as follows:
#include <string> //string头文件
//联系人结构体
struct Person
{
string m_Name; //姓名
int m_Sex; //性别:1男 2女
int m_Age; //年龄
string m_Phone; //电话
string m_Addr; //住址
};
5.2 Design the address book structure
When designing, you can maintain an array of contacts with a capacity of 1000 in the address book structure, and record the number of contacts in the current address book
The design is as follows
#define MAX 1000 //最大人数
//通讯录结构体
struct Addressbooks
{
struct Person personArray[MAX]; //通讯录中保存的联系人数组
int m_Size; //通讯录中人员个数
};
5.3 Create an 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 the time
mian函数起始位置添加:
//创建通讯录
Addressbooks abs;
//初始化通讯录中人数
abs.m_Size = 0;
5.4 Encapsulate the add contact function
Idea: Before adding contacts, first determine whether the address book is full. If it is full, it will not be added. If it is not full, add new contact information to the address book one by one.
Add contact code:
//1、添加联系人信息
void addPerson(Addressbooks *abs)
{
//判断电话本是否满了
if (abs->m_Size == MAX)
{
cout << "通讯录已满,无法添加" << endl;
return;
}
else
{
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
cout << "请输入性别:" << endl;
cout << "1 -- 男" << endl;
cout << "2 -- 女" << endl;
//性别
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
cout << "输入有误,请重新输入";
}
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
cin >> age;
abs->personArray[abs->m_Size].m_Age = age;
//联系电话
cout << "请输入联系电话:" << endl;
string phone = "";
cin >> phone;
abs->personArray[abs->m_Size].m_Phone = phone;
//家庭住址
cout << "请输入家庭住址:" << endl;
string address;
cin >> address;
abs->personArray[abs->m_Size].m_Addr = address;
//更新通讯录人数
abs->m_Size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
}
5.5 Test the function of adding contacts
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 to case1:
case 1: //添加联系人
addPerson(&abs);
break;
6. Display contacts
Function description: Display the existing contact information in the address book
Display contact implementation steps:
- Encapsulate the display contact function
- Test the display contacts feature
6.1 Encapsulate the display contact function
Idea: Judging that if there is no person in the current address book, it will prompt that the record is empty, the number of people is greater than 0, and the information in the address book will be displayed
Show contact code:
//2、显示所有联系人信息
void showPerson(Addressbooks * abs)
{
if (abs->m_Size == 0)
{
cout << "当前记录为空" << endl;
}
else
{
for (int i = 0; i < abs->m_Size; i++)
{
cout << "姓名:" << abs->personArray[i].m_Name << "\t";
cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArray[i].m_Age << "\t";
cout << "电话:" << abs->personArray[i].m_Phone << "\t";
cout << "住址:" << abs->personArray[i].m_Addr << endl;
}
}
system("pause");
system("cls");
}
6.2 Test the display contact function
In the switch case statement, add in case 2
case 2: //显示联系人
showPerson(&abs);
break;
7. Delete contacts
Function description: delete the specified contact according to the name
Steps to delete a contact:
- Encapsulates the existence of a contact
- Encapsulate delete contact function
- Test delete contacts
7.1 Encapsulation to detect whether the contact exists
Design ideas:
Before deleting a contact, we need to first determine whether the contact entered by the user exists.
Therefore, we can encapsulate the detection of the existence of a contact into a function. If it exists, return the location of the contact in the address book. If it does not exist, return -1
Detect if a contact exists with a code:
//判断是否存在查询的人员,存在返回在数组中索引位置,不存在返回-1
int isExist(Addressbooks * abs, string name)
{
for (int i = 0; i < abs->m_Size; i++)
{
if (abs->personArray[i].m_Name == name)
{
return i;
}
}
return -1;
}
7.2 Encapsulate the delete contact function
Determine whether there is this person in the address book according to the contact entered by the user
Find and delete, and prompt that the deletion is successful
Can't find the prompt to find no such person.
//3、删除指定联系人信息
void deletePerson(Addressbooks * abs)
{
cout << "请输入您要删除的联系人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
for (int i = ret; i < abs->m_Size; i++)
{
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_Size--;
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
7.3 Test the delete contact function
In the switch case statement, add in case3:
case 3: //删除联系人
deletePerson(&abs);
break;
The test effect is as follows:
8. Find contacts
Function description: View the specified contact information by name
Find Contacts Implementation Steps
- Encapsulate the find contact function
- Test to find the specified contact
8.1 Encapsulating the Find Contact Function
Implementation idea: Determine whether the contact specified by the user exists. If there is display information, if it does not exist, it will prompt that there is no such person.
Find the contact code:
//4、查找指定联系人信息
void findPerson(Addressbooks * abs)
{
cout << "请输入您要查找的联系人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
cout << "姓名:" << abs->personArray[ret].m_Name << "\t";
cout << "性别:" << abs->personArray[ret].m_Sex << "\t";
cout << "年龄:" << abs->personArray[ret].m_Age << "\t";
cout << "电话:" << abs->personArray[ret].m_Phone << "\t";
cout << "住址:" << abs->personArray[ret].m_Addr << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
8.2 Test to find the specified contact
In the switch case statement, add in case4:
case 4: //查找联系人
findPerson(&abs);
break;
9. Modify contacts
Function description: Re-modify the specified contact according to the name
Modify the contact implementation steps
- Encapsulate the modify contact function
- Test the edit contact function
9.1 Encapsulate and modify the contact function
Implementation idea: Find the contact entered by the user. If the search is successful, the modification operation is performed. If the search fails, it will prompt that there is no such person.
Modify the contact code:
//5、修改指定联系人信息
void modifyPerson(Addressbooks * abs)
{
cout << "请输入您要修改的联系人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[ret].m_Name = name;
cout << "请输入性别:" << endl;
cout << "1 -- 男" << endl;
cout << "2 -- 女" << endl;
//性别
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[ret].m_Sex = sex;
break;
}
cout << "输入有误,请重新输入";
}
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
cin >> age;
abs->personArray[ret].m_Age = age;
//联系电话
cout << "请输入联系电话:" << endl;
string phone = "";
cin >> phone;
abs->personArray[ret].m_Phone = phone;
//家庭住址
cout << "请输入家庭住址:" << endl;
string address;
cin >> address;
abs->personArray[ret].m_Addr = address;
cout << "修改成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
9.2 Test the function of modifying contacts
In the switch case statement, in case 5 add:
case 5: //修改联系人
modifyPerson(&abs);
break;
The test effect is as follows:
The specified contact cannot be found:
10. Clear contacts
Function description: Clear all information in the address book
Steps to clear contacts
- Encapsulate clear contact function
- test clear contacts
10.1 Encapsulate the clear contact function
Implementation idea: To clear all contact information in the address book, just set the number of contacts recorded in the address book to 0, and do a logical clearing.
Clear contact code:
//6、清空所有联系人
void cleanPerson(Addressbooks * abs)
{
abs->m_Size = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
10.2 Test clearing contacts
In the switch case statement, in case 6 add:
case 6: //清空联系人
cleanPerson(&abs);
break;
e 5: //Modify the contact
modifyPerson(&abs);
break;
## 10、清空联系人
功能描述:清空通讯录中所有信息
清空联系人实现步骤
* 封装清空联系人函数
* 测试清空联系人
### 10.1 封装清空联系人函数
实现思路: 将通讯录所有联系人信息清除掉,只要将通讯录记录的联系人数量置为0,做逻辑清空即可。
清空联系人代码:
```C++
//6、清空所有联系人
void cleanPerson(Addressbooks * abs)
{
abs->m_Size = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
10.2 Test clearing contacts
In the switch case statement, in case 6 add:
case 6: //清空联系人
cleanPerson(&abs);
break;
At this point, the address book management system is complete!
Code download address (VS2019)
link: https://pan.baidu.com/s/1ILMLZRpfpujAa63DVE7I7A
Extraction code: 8888