![](https://i-blog.csdnimg.cn/blog_migrate/ac527782d7e12829466b37692f40bd01.png)
#include<iostream>
using namespace std;
#include<string>
#define MAX 1000 //最大人数
//联系人结构体
struct Person {
string m_Name;
int m_sex;
int m_Age;
string m_phone;
string m_Addr;
}p;
struct Addressbooks
{
struct Person personArray[MAX];//通讯录中保存的联系人数组
int m_Size;//联系人个数
};
//菜单界面
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;
}
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 sex;
cout << "请输入性别:(1为男生2为女生)" << endl;
cin >> sex;
abs->personArray[abs->m_Size].m_sex = sex;
int age;
cout << "请输入年龄:" << endl;
cin >> age;
abs->personArray[abs->m_Size].m_Age = age;
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;
}
}
void ShowPeople(Addressbooks *a) {
if (a->m_Size == 0) {
cout << "通讯录为空"<<endl;
}
else {
string s;
for (int i = 0; i < a->m_Size; i++)
{
cout << "-----------------"<<endl;
cout << "姓名:" << a->personArray[i].m_Name << endl;
if (a->personArray[i].m_sex == 1) {
s = "男";
}
else s = "女";
cout << "性别:" << s << endl;
cout << "年龄:" << a->personArray[i].m_Age << endl;
cout << "电话:" << a->personArray[i].m_phone << endl;
cout << "住址:" << a->personArray[i].m_Addr << endl;
cout << "-----------------" << endl;
}
}
}
int getIndex(string name, Addressbooks* a) {
for (int i = 0; i < a->m_Size; i++)for (int i = 0; i < a->m_Size; i++) {
if (a->personArray[i].m_Name == name) {
return i;
}
}
return -1;
}
void DelPeople(Addressbooks* a) {
string name;
cout << "请输入要删除的联系人姓名" << endl;
cin >> name;
int index=getIndex(name, a);
if (index == -1) {
cout << "查无此人" << endl;
}
else {
for (int i = index; i < a->m_Size - 1;i++) {
a->personArray[i] = a->personArray[i + 1];
}
a->m_Size--;
cout << "删除成功\n";
}
}
void SearchPerson(Addressbooks* a) {
string name;
cout << "请输入要查找的联系人姓名" << endl;
cin >> name;
int i = getIndex(name, a);
if (i == -1) {
cout << "查无此人" << endl;
}
else {
string s;
cout << "查找成功信息如下:\n";
cout << "-----------------" << endl;
cout << "姓名:" << a->personArray[i].m_Name << endl;
if (a->personArray[i].m_sex == 1) {
s = "男";
}
else s = "女";
cout << "性别:" << s << endl;
cout << "年龄:" << a->personArray[i].m_Age << endl;
cout << "电话:" << a->personArray[i].m_phone << endl;
cout << "住址:" << a->personArray[i].m_Addr << endl;
cout << "-----------------" << endl;
}
}
void AlterPerson(Addressbooks* a) {
string name;
cout << "请输入要修改的联系人姓名" << endl;
cin >> name;
int i = getIndex(name, a);
if (i == -1) {
cout << "查无此人" << endl;
}
else {
//添加具体联系人
string name;
cout << "请输入姓名:" << endl;
cin >> name;
a->personArray[i].m_Name = name;
int sex;
cout << "请输入性别:(1为男生2为女生)" << endl;
cin >> sex;
a->personArray[i].m_sex = sex;
int age;
cout << "请输入新的年龄:" << endl;
cin >> age;
a->personArray[i].m_Age = age;
string phone;
cout << "请输入电话号码:" << endl;
cin >> phone;
a->personArray[i].m_phone = phone;
string addr;
cout << "请输入家庭地址:" << endl;
cin >> addr;
a->personArray[i].m_Addr = addr;
cout << "修改成功" << endl;
}
}
void ClearList(Addressbooks* a) {
a->m_Size =0;
cout << "通讯已清空" << endl;
}
void Clear() {
system("pause");//请按任意键继续
system("cls");//清屏
}
int main() {
//创建通讯录结构体变量
Addressbooks abs;
//初始化
abs.m_Size = 0;
int select = 0;//创建用户选择输入的变量
while (1) {
//system("cls");
//菜单调用
showMenu();
cin >> select;
switch (select)
{
case 0://0、退出通讯录
goto Exit;
break;
case 1://1、添加联系人
AddPerson(&abs);
Clear();
break;
case 2://2、显示联系人
ShowPeople(&abs);
Clear();
break;
case 3://3、删除联系人
DelPeople(&abs);
Clear();
break;
case 4://4、查找联系人
SearchPerson(&abs);
Clear();
break;
case 5://5、修改联系人
AlterPerson(&abs);
Clear();
break;
case 6://6、清空联系人
ClearList(&abs);
Clear();
break;
default:
break;
}
}
Exit:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
}
演示截图如下:
![](https://i-blog.csdnimg.cn/blog_migrate/c20c736437782705adc3dcf0f92c7dea.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9f47a81e06236ec3c9caae89346304d1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e3cce7921c4fcd841cf75f4bd007a589.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8d9097ab9c6db6f355317363c52cc9db.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b2c26510ca188bbda187d2b0589b1f69.png)
剩余功能请自行测试