求一个注释,帮忙解析以下代码
#include<iostream>
#include<string>
#include<cstdlib>
#define MAX 2000
using namespace std;
//通讯录管理系统
//设计联系人结构体
struct Person {
string m_Name;
int m_Sex;
int m_Age;
string m_Phone;
string m_Addr;
};
//设计通讯录结构体
struct Addressbooks {
//通讯录中保存的联系人数组
struct Person personArray[MAX];
//通讯录中当前记录的联系人个数
int m_Size;
};
//添加联系人
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 << "输入有误,重新输入" << endl;
}
//年龄
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"); //清屏操作
}
}
//显示联系人
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"); //清屏操作
}
//检测联系人是否存在
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 << "请输入要删除的联系人" << 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"); //清屏操作
}
//查找联系人
void findPersin(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 == 1 ? "男" : "女") << "\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"); //清屏操作
}
//修改联系人
void modifyRerson(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;
//年龄
int age;
cout << "请输入年龄" << endl;
cin >> age;
abs->personArray[ret].m_Age = age;
//电话
int phone;
cout << "请输入电话" << endl;
cin >> phone;
abs->personArray[ret].m_Phone = phone;
//住址
string address;
cout << "请输入住址" << endl;
cin >> address;
abs->personArray[ret].m_Addr = address;
cout << "修改成功" << endl;
}
else {
cout << "查无此人" << endl;
}
system("pause");
system("cls"); //清屏操作
}
//清空联系人
void cleanPerson(Addressbooks* abs) {
abs->m_Size = 0; //只需要将联系人数量置为0,做逻辑清空就可以了
cout << "通讯录已清空"<< endl;
}
//菜单界面
void showMenu() {
cout << "1. 添加联系人" << endl;
cout << "2. 显示联系人" << endl;
cout << "3. 删除联系人" << endl;
cout << "4. 查找联系人" << endl;
cout << "5. 修改联系人" << endl;
cout << "6. 清空联系人" << endl;
cout << "0. 退出通讯录" << endl;
}
int main() {
//创建通讯录
Addressbooks abs;
//初始化通讯录当前人员个数
abs.m_Size = 0;
int select = 0; //创建用户选择输入的变量
while (true) {
// 菜单界面
showMenu();
cin >> select;
switch (select) {
case 1: //1. 添加联系人
addPerson(&abs); //利用地址传递修改实参
break;
case 2: //2. 显示联系人
showPerson(&abs);
break;
case 3: //3. 删除联系人
//{
// cout << "请输入删除联系人姓名:" << endl;
// string name;
// cin >> name;
// if (isExist(&abs, name) == -1) {
// cout << "查无此人" << endl;
// }
// else {
// cout << "找到此人" << endl;
// }
//}
deletePerson(&abs);
break;
case 4: //4. 查找联系人
findPersin(&abs);
break;
case 5: //5. 修改联系人
modifyRerson(&abs);
break;
case 6: //6. 清空联系人
cleanPerson(&abs);
break;
case 0: //0. 退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
defult:
break;
}
}
system("pause");
return 0;
}
标签:课程设计,cout,ret,name,abs,通讯录,personArray,Size,线性表
From: https://blog.csdn.net/2301_78402165/article/details/136987074