C++ 小项目 - 通讯录管理系统
C++ 小项目系列教程:
➡️➡️➡️本教程参考自 黑马程序员 C++ 视频课程 其中的 Markdown 文档,仅用于自己学习,源码发布在 Contact-Management。
1. 系统需求
通讯录是一个可以记录亲人、好友信息的工具,本教程主要利用 C++
来实现一个通讯录管理系统,需要实现的功能如下:
(1)添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址),最多记录100人;
(2)显示联系人:显示通讯录中所有联系人信息;
(3)删除联系人:按照姓名进行删除指定联系人;
(4)查找联系人:按照姓名查看指定联系人信息;
(5)修改联系人:按照姓名重新修改指定联系人;
(6)清空联系人:清空通讯录中所有信息;
(7)退出通讯录:退出当前使用的通讯录。
2. 创建项目
作者是基于 Ubuntu 系统,在 VS Code 中编写代码再编译运行,详情请见 Ubuntu 系统安装 VS Code 并配置 C++ 环境。
项目结构如下图所示:
3. 菜单
功能描述: 用户选择功能的界面,如下图
步骤:
- 封装显示该界面的函数,如
void showMenu()
; - 在
main
主函数中调用该已封装好的函数。
部分代码:
void showMenu(){
cout << "******************************************" << endl;
cout << "*********** 1. Add contact **********" << endl;
cout << "*********** 2. Show contact **********" << endl;
cout << "*********** 3. Delete contact **********" << endl;
cout << "*********** 4. Find contact **********" << endl;
cout << "*********** 5. Modify contact **********" << endl;
cout << "*********** 6. Empty contact **********" << endl;
cout << "*********** 0. Exit **********" << endl;
cout << "******************************************" << endl;
}
4. 主要功能
主要包含如下功能:
- 添加联系人
- 显示联系人
- 删除联系人
- 查找联系人
- 修改联系人
- 清空联系人
- 退出
设计 - 联系人结构体:
struct Person{
string m_Name; // name
int m_Sex; // sex: 0(male) 1(female)
int m_Age; // age
string m_Phone; // phone
string m_Addr; // address
};
设计 - 通讯录结构体:
#define MAX 100
struct Addressbooks{
struct Person personArray[MAX];
int m_Size;
};
4.1 添加联系人
功能描述: 实现添加联系人功能(上限为100人),联系人信息包括(姓名、性别、年龄、联系电话和家庭住址)
部分代码:
void addPerson(Addressbooks *abs){
if (abs->m_Size == MAX){
cout << "Address book is full, can't be added!" << endl;
return;
}
else{
// name
string name;
cout << "Please enter a name:";
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
// sex
cout << "Please enter gender(0->male 1->female):";
int sex = 0;
cin >> sex;
while(true){
if (sex == 0 || sex == 1){
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
cout << "Incorrect input, please re-enter!";
}
// age
cout << "Please enter age:";
int age=0;
cin >> age;
abs->personArray[abs->m_Size].m_Age = age;
// phone
cout << "Please enter phone(four digits):";
string phone = "";
cin >> phone;
abs->personArray[abs->m_Size].m_Phone = phone;
// address
cout << "Please enter address:";
string address = "";
cin >> address;
abs->personArray[abs->m_Size].m_Addr = address;
// updated number of persons
abs->m_Size++;
cout << "Added Successfully!" << endl << endl;
}
}
运行效果:
4.2 显示联系人
功能描述: 显示通讯录中已有的联系人信息
部分代码:
void showPerson(Addressbooks *abs){
if (abs->m_Size == 0){
cout << "current record is empty" << endl << endl;
}
else{
for (int i = 0; i < abs->m_Size; i++){
cout << "Name: " << abs->personArray[i].m_Name << "\t";
cout << "Sex: " << (abs->personArray[i].m_Sex == 0 ? "male" : "female") << "\t";
cout << "Age: " << abs->personArray[i].m_Age << "\t";
cout << "Phone: " << abs->personArray[i].m_Phone << "\t";
cout << "Address: " << abs->personArray[i].m_Addr << endl;
}
}
cout << endl;
}
运行效果:
4.3 删除联系人
功能描述: 按照姓名来删除指定联系人
设计思路:
-
删除联系人前,需要先判断用户输入的联系人是否存在,存在则删除,反之,提示用户没有“这号人物”;
-
所以,可以将判断联系人是否存在封装到一个函数中,若存在,返回其索引位置,若不存在,则返回
-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;
}
部分代码:
void deletePerson(Addressbooks *abs){
cout << "Please enter the contact you want to delete:";
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 << "Deleted Successfully!" << endl;
}
else{
cout << "This person does not exist!" << endl;
}
cout << endl;
}
运行效果:
4.4 查找联系人
设计思路: 判断用户指定的联系人是否存在,存在即显示其相关信息,不存在则提示没有“这号人物”。
部分代码:
void findPerson(Addressbooks *abs){
cout << "Please enter the contact you want to find:";
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1){
cout << "Name: " << abs->personArray[ret].m_Name << "\t";
cout << "Sex: " << (abs->personArray[ret].m_Sex == 0 ? "male" : "female") << "\t";
cout << "Age: " << abs->personArray[ret].m_Age << "\t";
cout << "Phone: " << abs->personArray[ret].m_Phone << "\t";
cout << "Address: " << abs->personArray[ret].m_Addr << endl;
}
else{
cout << "This person does not exist!" << endl ;
}
cout << endl;
}
运行效果:
4.5 修改联系人
功能描述: 按照姓名重新修改指定联系人
设计思路: 查找用户输入的联系人,若查找成功,则进行修改操作,反之提示没有“这号人物”。
部分代码:
void modifyperson(Addressbooks *abs){
cout << "Please enter the contact you want to modify:";
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1){
// name
cout << "Please enter a modified name:";
cin >> name;
abs->personArray[ret].m_Name = name;
// sex
cout << "Please enter a modified gender(0->male 1->female):";
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 0 || sex == 2){
abs->personArray[ret].m_Sex = sex;
break;
}
cout << "Incorrect input, please re-enter!";
}
// age
cout << "Please enter a modified age:";
int age=0;
cin >> age;
abs->personArray[ret].m_Age = age;
// phone
cout << "Please enter a modified phone(four digits):";
string phone = "";
cin >> phone;
abs->personArray[ret].m_Phone = phone;
// address
cout << "Please enter a modified address:";
string address = "";
cin >> address;
abs->personArray[ret].m_Addr = address;
cout << "Modified successfully!" << endl;
}
else{
cout << "This person does not exist!" << endl;
}
cout << endl;
}
运行效果:
4.6 清空联系人
功能描述: 清空通讯录中的所有信息
实现思路: 将通讯录中的所有联系人信息清除掉,只需将其联系人数量置为 0
,做逻辑清空即可。
部分代码:
void emptyPerson(Addressbooks *abs){
abs->m_Size = 0;
cout << "Address book cleared!" << endl;
}
4.7 退出
功能描述: 退出通讯录系统,根据用户的不同选择,进入不同的功能,可以考虑 switch
分支结构;当用户输入 0
时,执行退出操作,输入其他时,执行对应的操作。
部分代码:
switch(select){
case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
deletePerson(&abs);
break;
case 4:
findPerson(&abs);
break;
case 5:
modifyperson(&abs);
break;
case 6:
emptyPerson(&abs);
break;
case 0: // exit
cout << "Welcome to next time!" << endl;
return 0;
break;
default: break;
}
运行效果:
原文地址:https://blog.csdn.net/m0_70885101/article/details/137919839
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!