案例(图源:B站黑马程序员):
#include<iostream>
#include<string>
using namespace std;
#define MAX 1000
struct Person //联系人的结构体
{
string m_name;
int m_sex;//性别,1男 2女
int m_age;
string m_phone;
string m_adder;
};
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;
//输入性别
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;
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_adder = address;
abs->m_size++;//更新通讯录人数
cout << "录入成功!" << endl;
system("pause");
system("cls");
}
}
void Showperson(Addressbooks *abs)//现实联系人函数
{
//判断是否为0
if (abs->m_size == 0)
{
cout << "记录为空" << endl;
}
else
{
cout << "目前保存联系人的个数为 :" <<abs->m_size<< endl;
for (int i = 0; i < abs->m_size; i++)
{
cout << "姓名 :"<<abs->personArray[i].m_name <<"\t"
<< " 性别 :" <<(abs->personArray[i].m_sex == 1?"男":"女") << "\t"
<< " 年龄 :" << abs->personArray[i].m_age << "\t"
<< " 电话 :" << abs->personArray[i].m_phone << "\t"
<< " 地址 :" << abs->personArray[i].m_adder<< endl;
}
}
system("pause");
system("cls");
}
//检测联系人是否存在,如果存在,返回联系人具体位置,不存在返回-1(所以是整形)
int isExist(Addressbooks *abs, string name)//判断是否存在,首先得在通讯录中判断,随后使用姓名进行查找,所以需要传入姓名
{
for (int i = 0; i < abs->m_size; i++)
{
if (name == abs->personArray[i].m_name)
{
return i;//找到了,返回数组编号
}
}
return -1;//没找到联系人
}
//删除联系人功能
void deletePerson(Addressbooks *abs)
{
cout << "请输入要删除的联系人姓名 " << endl;
string name;
cin >> name;
//两种结果:ret == -1,没查到 ret != -1,查到了
int ret = isExist(abs, name);//调用函数,因为在这个函数中,abs已经是指针,所以不需要用&abs
if (ret != -1)
{
//删除一个联系人的方法:找到此联系人,将此联系人后面的数据前移并覆盖,并且记录个数-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 findperson(Addressbooks *abs)
{
string name;
cout << "请输入所需要查找的联系人姓名 :" << endl;
cin >> name;
int ret = isExist(abs, name);//查找联系人是否存在并返回给ret
if (ret != -1)
{
cout << "姓名 :" << abs->personArray[ret].m_name << "\t"
<< "性别 :" << abs->personArray[ret].m_sex << "\t"
<< "年龄 :" << abs->personArray[ret].m_age << "\t"
<< "电话 :" << abs->personArray[ret].m_phone << "\t"
<< "地址 :" << abs->personArray[ret].m_adder << "\t" << endl;
}
else
{
cout << "联系人不存在 " << endl;
}
system("pause");
system("cls");
}
void moidfyPerson(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;
cin >> sex;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->m_size].m_sex = sex;
break;
}
cout << "输入有误 " << endl;
}
//年龄
cout << "请输入年龄 :" << endl;
int age;
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_adder = address;
cout << "修改成功 " << endl;
}
else
{
cout << "查无此人 " << endl;
}
system("pause");
system("cls");
}
//清空联系人:只需要把联系人的个数改为0,就算清空
void cleanPerson(Addressbooks *abs)
{
abs->m_size = 0;
cout << "联系人已经清空 " << endl;
system("pause");
system("cls");
}
int main()
{
//创建通讯录的结构体变量数组
Addressbooks abs;
abs.m_size = 0;//初始化通讯录中人数
while (true)
{
showMenu();
int select = 0;
cin >> select;
switch (select)//如果case语句很长,就会报错,所以很长的语句要放入代码段中(添加{}即可)
{
case 1://添加联系人
Addperson(&abs);//地址传递才能真正添加人,否则一直为0
break;
case 2://显示联系人
Showperson(&abs);
break;
case 3:
deletePerson(&abs);
break;
case 4:
findperson(&abs);
break;
case 5:
moidfyPerson(&abs);
break;
case 6:
cleanPerson(&abs);
break;
case 0://退出程序
cout << "欢迎下次使用!" << endl;
system("pause");
return 0;
default:
break;
}
}
system("pause");
return 0;
}
代码很长,建议初学者自己尝试敲一次,就会理解里面很多的东西