一、系统需求

二、创建项目

三、菜单功能
// 封装函数显示该界面 如 void showMenu()
// 在mian函数中调用封装好的函数
#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 << "***** 5、清空联系人 *****" << endl;
cout << "***** 6、退出通讯录 *****" << endl;
cout << "*****************************" << endl;
}
int main() {
//菜单调用
showMenu();
system("pause");//按任意键继续
return 0;
}

四、退出功能

// 封装函数显示该界面 如 void showMenu()
// 在mian函数中调用封装好的函数
#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 << "***** 5、清空联系人 *****" << endl;
cout << "***** 6、退出通讯录 *****" << endl;
cout << "***** 0、退出系统 *****" << endl;
cout << "*****************************" << endl;
}
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:
cout << "输入错误,请重新输入!" << endl;
break;
}
}
system("pause");//按任意键继续
return 0;
}

五、添加联系人

(一)设计联系人结构体
联系人信息包括:姓名、性别、年龄、联系电话、家庭住址
using namespace std;
//设计联系人的结构体
struct Person {
//姓名
string m_Name;
//性别
int m_Sex;
//年龄
int m_Age;
//电话
string m_Phone;
//住址
string m_Addr;
};
(二)设计通讯录结构体
设计时候可以再通讯录结构体中,维护一个容量为1000的存放联系人的数组,并记录当前通讯录中联系人数量设计如下
#define MAX 1000//最大联系人个数
//设计通讯录的结构体
struct AddressBook {
//通讯录中保存联系人的数组
struct Person personArray[MAX];
//通讯录中记录当前联系人个数
int m_Size;
};
(三)main函数中创建通讯录
//创建通讯录结构图变量
AddressBooks abs;
//初始化通讯录中当前的人员个数
abs.m_Size = 0;
(四)封装添加联系人函数
//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;
//年龄
int age;
cout << "请输入年龄:" << endl;
cin >> age;
abs->personArray[abs->m_Size].m_Age = age;
//性别
int sex;
cout << "请输入性别(1:男,2:女):" << endl;
while (true) {
//如果输入的是1或者2可以退出循环,因为输入的是正确的值
//如果输入有误,重新输入
cin >> sex;
if (sex == 1 || sex == 2) {
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//电话
string phone;
cout << "请输入联系电话:" << endl;
cin >> phone;
abs->personArray[abs->m_Size].m_Phone = phone;
//住址
string addr;
cout << "请输入地址:" << endl;
cin >> addr;
abs->personArray[abs->m_Size].m_Addr = addr;
//更新通讯录人数
abs->m_Size++;
cout << "添加成功" << endl;
//清屏
system("pause");//请按任意键继续
system("cls");//清屏
}
}

六、显示联系人

(一)封装显示联系人函数
思路:判断如果当前通讯录中没有人员,就提示记录为空,人数大于0,显示通讯录中信息
//2、显示所有联系人
void showPerson(AddressBooks* abs) {
//判断通讯录的人数是否为0,如果为0,提示记录为空
//如果不为0,显示记录的联系人信息
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;
cout << "*********************" << endl;
}
}
system("pause");//请按任意键继续
system("cls");//清屏
}

七、删除联系人功能

(一)封装检测联系人是否存在
设计思路:
删除联系人前,我们需要先判断用户输入的联系人是否存在,如果存在删除,不存在提示用户没有删除的联系人,因此我们可以把检测联系人是否存在封装成一个函数,如果存在,返回联系人在通讯录中的位置,不存在返回-1
//检测联系人是否存在,如果存在返回联系人所在数组中的具体位置,不存在返回-1
//参数1 通讯录 参数2 对比姓名
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;
}
(二)封装删除联系人功能
//3、删除指定的联系人
void deletePersopn(AddressBooks* abs) {
cout << "请输入要删除的联系人姓名:" << endl;
string name;
cin >> name;
//检测联系人是否存在
int index = isExist(abs, name);
if (index == -1) {
cout << "查无此人" << endl;
}
else {
//找到了联系人,删除
for (int i = index; i < abs->m_Size; i++) {
//数据前移
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_Size--;//更新通讯录人数
cout << "删除成功" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏
}

八、查找联系人

(一)封装查找联系人函数
//4、查找指定联系人
void findPerson(AddressBooks* abs) {
cout << "请输入要查找的联系人姓名:" << endl;
string name;
cin >> name;
//检测联系人是否存在
int index = isExist(abs, name);
if (index != -1) {
//找到了联系人,显示信息
cout << "姓名:" << abs->personArray[index].m_Name << "\t";
cout << "性别:" << (abs->personArray[index].m_Sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArray[index].m_Age << "\t";
cout << "电话:" << abs->personArray[index].m_Phone << "\t";
cout << "住址:" << abs->personArray[index].m_Addr << endl;
}
else {
cout << "查无此人" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏
}

九、修改联系人

(一)封装修改联系人函数
//5、修改指定联系人信息
void modifyPerson(AddressBooks* abs) {
cout << "请输入要修改的联系人姓名:" << endl;
string name;
cin >> name;
//检测联系人是否存在
int index = isExist(abs, name);
if (index != -1) {
//姓名
cout << "请输入新的姓名:" << endl;
string newname;
cin >> newname;
abs->personArray[index].m_Name = newname;
//年龄
cout << "请输入新的年龄:" << endl;
int newage;
cin >> newage;
abs->personArray[index].m_Age = newage;
//性别
cout << "请输入新的性别(1:男,2:女):" << endl;
int newsex;
while (true) {
cin >> newsex;
if (newsex == 1 || newsex == 2) {
abs->personArray[index].m_Sex = newsex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//电话
cout << "请输入新的联系电话:" << endl;
string newphone;
cin >> newphone;
abs->personArray[index].m_Phone = newphone;
//住址
cout << "请输入新的地址:" << endl;
string newaddr;
cin >> newaddr;
abs->personArray[index].m_Addr = newaddr;
cout << "修改成功" << endl;
}else {
cout << "查无此人" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏
}

十、清空联系人
//6、清空联系人(逻辑清空)
void clearPerson(AddressBooks* abs) {
//清空通讯录
abs->m_Size = 0;//将当前记录联系人数量
cout << "通讯录已清空" << endl;
system("pause");//请按任意键继续
system("cls");//清屏
}
2万+

被折叠的 条评论
为什么被折叠?



