#include <iostream>
#include <string>
using namespace std;
#define MAX 5 //最大人数
//联系人结构体
struct Person
{
string m_name; //姓名
int m_sex; //性别
int m_age; //年龄
string m_phone; //电话
string m_addr; //住址
};
//通讯录结构体
struct Addressbooks
{
struct Person parray[MAX]; //通讯录中保存的联系人数组
int m_size; //通讯录中人员个数
};
//1.添加联系人函数
void addPerson(Addressbooks *abs)
{
//判断通讯录是否已满,满了就不再添加
if(abs->m_size == MAX)
{
cout<<"通讯录已满,无法添加!"<<endl;
system("pause");
system("cls");
return;
}
else
{
//添加联系人姓名
string name;
cout<<"请输入姓名:";
cin>>name;
abs->parray[abs->m_size].m_name = name;
//添加联系人性别
int sex = 0;
cout<<"请输入性别(1.男 2.女):";
while(1)
{
cin>>sex;
if(sex == 1 || sex == 2)
{
abs->parray[abs->m_size].m_sex = sex;
break;
}
else
{
cout<<"输入有误,请重新输入!"<<endl;
}
}
//添加联系人年龄
int age = 0;
cout<<"请输入年龄:";
cin>>age;
abs->parray[abs->m_size].m_age = age;
//添加联系人电话
string phone;
cout<<"请输入联系电话:";
cin>>phone;
abs->parray[abs->m_size].m_phone = phone;
//添加联系人住址
string address;
cout<<"请输入家庭住址:";
cin>>address;
abs->parray[abs->m_size].m_addr = address;
//更新通讯录人数
abs->m_size++;
cout<<"添加成功!"<<endl;
system("pause");
system("cls");
}
}
//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->parray[i].m_name<<"\t";
cout<<"性别:"<<(abs->parray[i].m_sex == 1 ? "男":"女")<<"\t";
cout<<"年龄:"<<abs->parray[i].m_age<<"\t";
cout<<"电话:"<<abs->parray[i].m_phone<<"\t";
cout<<"住址:"<<abs->parray[i].m_addr<<endl;
}
}
system("pause");
system("cls");
}
//检测联系人是否存在,如果存在返回数组下标编号,不存在返回-1
int isExist(Addressbooks *abs,string name)
{
for(int i=0;i<abs->m_size;i++)
{
//找到目标姓名
if(abs->parray[i].m_name == name)
{
return i;//找到了,返回数组下标编号
}
}
return -1;//如果遍历结束没找到,返回-1
}
//3.删除指定联系人
void deletePerson(Addressbooks *abs)
{
string name;
cout<<"请输入您要删除的联系人姓名:";
cin>>name;
int ret = isExist(abs,name);
if(ret != -1)
{
//找到进行删除
for(int i=ret;i<abs->m_size-1;i++)
{
abs->parray[i] = abs->parray[i+1];
}
abs->m_size--;//更新通讯录中的人员个数
cout<<"删除成功!"<<endl;
}
else
{
cout<<"查无此人!"<<endl;
}
system("pause");
system("cls");
}
//4.查找指定联系人信息
void findPerson(Addressbooks *abs)
{
string name;
cout<<"请输入您要查找的联系人姓名:";
cin>>name;
//判断指定的联系人是否在通讯录中
int ret = isExist(abs,name);
if(ret != -1)
{
cout<<"姓名:"<<abs->parray[ret].m_name<<"\t";
cout<<"性别:"<<(abs->parray[ret].m_sex == 1 ? "男":"女")<<"\t";
cout<<"年龄:"<<abs->parray[ret].m_age<<"\t";
cout<<"电话:"<<abs->parray[ret].m_phone<<"\t";
cout<<"住址:"<<abs->parray[ret].m_addr<<endl;
}
else
{
cout<<"查无此人!"<<endl;
}
system("pause");
system("cls");
}
//5.修改指定的联系人信息
void modifyPerson(Addressbooks *abs)
{
string name;
cout<<"请输入您要修改的联系人姓名:";
cin>>name;
//判断指定的联系人是否在通讯录中
int ret = isExist(abs,name);
if(ret != -1)
{
//添加联系人姓名
string name;
cout<<"请输入姓名:";
cin>>name;
abs->parray[ret].m_name = name;
//添加联系人性别
int sex = 0;
cout<<"请输入性别(1.男 2.女):";
while(1)
{
cin>>sex;
if(sex == 1 || sex == 2)
{
abs->parray[ret].m_sex = sex;
break;
}
else
{
cout<<"输入有误,请重新输入!"<<endl;
}
}
//添加联系人年龄
int age = 0;
cout<<"请输入年龄:";
cin>>age;
abs->parray[ret].m_age = age;
//添加联系人电话
string phone;
cout<<"请输入联系电话:";
cin>>phone;
abs->parray[ret].m_phone = phone;
//添加联系人住址
string address;
cout<<"请输入家庭住址:";
cin>>address;
abs->parray[ret].m_addr = address;
cout<<"修改成功!"<<endl;
}
else
{
cout<<"查无此人!"<<endl;
}
system("pause");
system("cls");
}
//清空联系人
void cleanPerson(Addressbooks *abs)
{
int select = 0;
cout<<"确实要将通讯录清空吗?(1.确定 2.取消)"<<endl;
while(1)
{
cin>>select;
if(select == 1)
{
abs->m_size = 0;
cout<<"通讯录已清空!"<<endl;
system("pause");
system("cls");
break;
}
else if(select == 2)
{
system("pause");
system("cls");
break;
return;
}
else
{
cout<<"输入有误,请重新输入!"<<endl;
}
}
}
//通讯录界面菜单
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;
}
int main()
{
//创建通讯录结构体变量
Addressbooks abs;
//初始化通讯录中当前人员个数
abs.m_size = 0;
int select = 0;
while(true)
{
showMenu();
cin>>select;
switch(select)
{
case 1: //添加联系人
addPerson(&abs);//利用地址传递可以修饰实参
break;
case 2: //显示联系人
showPerson(&abs);
break;
case 3: //删除联系人
deletePerson(&abs);
break;
case 4: //查找联系人
findPerson(&abs);
break;
case 5: //修改联系人
modifyPerson(&abs);
break;
case 6: //清空联系人
cleanPerson(&abs);
break;
case 0: //退出通讯录
cout<<"欢迎下次使用!"<<endl;
system("pause");
return 0;
break;
default:cout<<"输入有误,请重新输入!"<<endl;
break;
}
}
system("pause");
return 0;
}