1.有菜单,专门由一个封装函数显示
2.while(true)和return 0 的用法,使菜单能循环显示,能一直使用通讯录
3.模块函数中不再只struct add *p,也可使其与结构体变量名相同,方便理解,避免全是 p。如struct add *addlink。
4.system("pause"); //请按任意键继续
system("cls"); //清屏
5.清空只是逻辑清空,把联系人数设为0,实际数据还在,只是无法访问。
#include <iostream>
#include <cmath>
#include <iomanip> // 控制符
#include <ctime>
#include <string>
#define max 1000 //直接定义max,方便后期修改
using namespace std;
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;
}
struct person //联系人结构体
{
string name;
int age;
string sex;
string tel;
string addr;
};
struct addressbooks
{
struct person personarry[max]; //通讯录中保存的联系人数组
int size; //通讯录中人员个数
};
//添加联系人
void addperson(struct addressbooks *ads) //这里也可取名*p,与结构体变量名相同更易分辨,维护
{
if (ads->size == max) //联系人如果满了
{
cout << "通讯录已满" << endl;
}
else
{
string name;
cout << "请输入姓名" << endl;
cin >> name;
ads->personarry[ads->size].name = name; //每次添加后,size++,则下次添加时会添加到数组最新一位中
string sex;
cout << "请输入性别" << endl;
cin >> sex;
ads->personarry[ads->size].sex = sex;
int age;
cout << "请输入年龄" << endl;
cin >> age;
ads->personarry[ads->size].age = age;
string tel;
cout << "请输入电话号码" << endl;
cin >> tel;
ads->personarry[ads->size].tel = tel;
string address;
cout << "请输入住址" << endl;
cin >> address;
ads->personarry[ads->size].addr = address;
cout << "添加成功" << endl;
system("pause"); //请按任意键继续
system("cls"); //清屏
ads->size++; //因为人数多了一个
}
}
//显示联系人
void showperson(struct addressbooks *ads)
{
if (ads->size == 0)
{
cout << "记录为空" << endl;
}
else
{
for (int i = 0; i < ads->size; i++)
{
cout << "姓名:" << ads->personarry[i].name << "\t";
cout << "性别:" << ads->personarry[i].sex << "\t";
cout << "年龄:" << ads->personarry[i].age << "\t";
cout << "电话:" << ads->personarry[i].tel << "\t";
cout << "住址:" << ads->personarry[i].addr << endl;
}
}
system("pause"); //请按任意键继续
system("cls"); //清屏
}
//检测联系人是否存在,如果存在,返回数组中具体位置,不存在,返回-1
int isexist(struct addressbooks *ads,string name) //传输通讯录地址,和要查找人的名字
{ //因为这里是返回一个数值,所以是int XX()
for (int i = 0; i < ads->size; i++)
{
if (ads->personarry[i].name == name)
{
return 0; //返回一个编号
}
}
return -1; //遍历结束都没找到
}
//删除联系人
void deleteperson(struct addressbooks *ads)
{
cout << "请输入要删除的联系人姓名" << endl;
string name;
cin >> name;
int ret = isexist(ads, name); //传输通讯录地址ads和名字到判断函数,这里的ads是传输过来的地址
if (ret != -1)
{
for (int i = ret;i < ads->size; i++) //从此人的下标开始一个个向前覆盖,形成删除的效果
{
ads->personarry[i] = ads->personarry[i + 1];
}
ads->size--; //删除后人数减1,更新通讯录人员数
cout << "删除成功" << endl;
}
else if (ret == -1)
{
cout << "查无此人" << endl;
}
system("pause"); //请按任意键继续
system("cls"); //清屏
}
void findperson(struct addressbooks *ads)
{
cout << "请输入要查找的联系人姓名" << endl;
string name;
cin >> name;
int ret = isexist(ads, name); //传输通讯录地址ads和名字到判断函数,这里的ads是传输过来的地址
if (ret != -1)
{
cout << "有这人儿" << endl;
cout << "姓名:" << ads->personarry[ret].name << "\t";
cout << "性别:" << ads->personarry[ret].sex << "\t";
cout << "年龄:" << ads->personarry[ret].age << "\t";
cout << "电话:" << ads->personarry[ret].tel << "\t";
cout << "住址:" << ads->personarry[ret].addr << endl;
}
else if (ret == -1)
{
cout << "查无此人" << endl;
}
system("pause"); //请按任意键继续
system("cls"); //清屏
}
void modifyperson(struct addressbooks *ads)
{
cout << "请输入要修改的联系人姓名" << endl;
string name;
cin >> name;
int ret = isexist(ads, name); //传输通讯录地址ads和名字到判断函数,这里的ads是传输过来的地址
if (ret != -1)
{
cout << "有这人儿" << endl;
//姓名
string name;
cout << "请输入姓名" << endl;
cin >> name;
ads->personarry[ret].name = name;
//性别
string sex;
cout << "请输入性别" << endl;
cin >> sex;
ads->personarry[ret].sex = sex;
//年龄
int age;
cout << "请输入年龄" << endl;
cin >> age;
ads->personarry[ret].age = age;
//电话
string tel;
cout << "请输入电话" << endl;
cin >> tel;
ads->personarry[ret].tel = tel;
//住址
string addr;
cout << "请输入住址" << endl;
cin >> addr;
ads->personarry[ret].addr = addr;
cout << "修改成功" << endl;
}
else if (ret == -1)
{
cout << "查无此人" << endl;
}
system("pause"); //请按任意键继续
system("cls"); //清屏
}
void cleanperson(struct addressbooks *ads)
{
ads->size = 0;
cout << "清空成功" << endl;
system("pause"); //请按任意键继续
system("cls"); //清屏
}
int main()
{
int select = 0;
struct addressbooks ads;
ads.size = 0; //一开始初始化通讯录,人数为0
while (true)
{
showmenu();
cin >> select;
switch (select)
{
case 1:addperson(&ads); //这里的ads不是结构体数组,所以加&符号
break;
case 2:showperson(&ads);
break;
case 3:deleteperson(&ads);
break;
case 4:findperson(&ads);
break;
case 5:modifyperson(&ads);
break;
case 6:cleanperson(&ads);
break;
case 0:cout << "欢迎下次使用" << endl;
return 0;//因为while循环的条件是true,是1.return 0 返回值为0 ,所以循环结束,整个程序结束。
}
}
system("pause");
return 0;
}