#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;
};
//通讯录结构体
struct addressbooks
{
struct person personnumber[MAX];
int m_size;
};
//添加联系人
void addperson(addressbooks *abs)
{
//先判断通讯录是否已满,如果满了就不再添加。
if(abs->m_size == MAX)
{
cout<<"添加失败,通讯录已满"<<endl;
return;
}
else
{
//添加具体联系人
//姓名
string name;
cout<<"请输入姓名:"<<endl;
cin>>name;
abs->personnumber[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->personnumber[abs->m_size].m_sex = sex;
break;
}
cout<<"输入错误,请重新输入!"<<endl;
}
//年龄
cout<<"请输入年龄:"<<endl;
int age = 0;
while(true)
{
cin>>age;
if(age>0 && age<150)
{
abs->personnumber[abs->m_size].m_age = age;
break;
}
cout<<"输入错误,请输入0-150岁之间的年龄"<<endl;
}
//电话
cout<<"请输入电话号码:"<<endl;
string phone;
while(true)
{
cin>>phone;
if(phone.length()<12)
{
abs->personnumber[abs->m_size].m_phone = phone;
break;
}
cout<<"输入有误,请输入正确的电话号码"<<endl;
}
//地址
cout<<"请输入家庭住址:"<<endl;
string addr;
cin>>addr;
abs->personnumber[abs->m_size].m_addr = addr;
//更新通讯录中的人数
abs->m_size++;
cout<<"添加成功"<<endl;
system("pause");
system("cls");
}
}
//检测联系人是否存在,如果存在,返回联系人所在数组的具体位置,不存在返回-1
int isExist(addressbooks *abs, string name)
{
for(int i=0; i<abs->m_size; i++)
{
if(abs->personnumber[i].m_name==name)
{
return i;
}
}
return -1;
}
//通讯录的菜单
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 showperson(addressbooks *abs)
{
if(abs->m_size == 0)
{
cout<<"当前通讯录为空"<<endl;
}
else
{
for(int i=0; i<abs->m_size; i++)
{
cout<<"姓名:"<<abs->personnumber[i].m_name<<"\t";
cout<<"性别:"<<(abs->personnumber[i].m_sex==1?"男":"女")<<"\t";
cout<<"年龄:"<<abs->personnumber[i].m_age<<"\t";
cout<<"电话:"<<abs->personnumber[i].m_phone<<"\t" ;
cout<<"地址:"<<abs->personnumber[i].m_addr<<"\t"<<endl;
}
}
system("pause");
system("cls");
}
//删除联系人
void deleteperson(addressbooks *abs)
{
cout<<"想要删除联系人的姓名:"<<endl;
string name;
cin>>name;
int num=isExist(abs,name);
if(num != -1)
{
for(int i=num; i<abs->m_size; i++)
{
abs->personnumber[i]=abs->personnumber[i+1];
}
abs->m_size--;
cout<<"删除成功"<<endl;
}
else
{
cout<<"抱歉,通讯录中没有此人"<<endl;
}
system("pause");
system("cls");
}
//查找联系人
void findperson(addressbooks *abs)
{
cout<<"请输入您要查找的人的姓名:"<<endl;
string name;
cin>>name;
int num = isExist(abs,name);
if(num != -1)
{
cout<<"已找到,信息如下:"<<endl;
cout<<"姓名:"<<abs->personnumber[num].m_name<<"\t";
cout<<"性别:"<<(abs->personnumber[num].m_sex==1?"男":"女")<<"\t";
cout<<"年龄:"<<abs->personnumber[num].m_age<<"\t";
cout<<"电话:"<<abs->personnumber[num].m_phone<<"\t" ;
cout<<"地址:"<<abs->personnumber[num].m_addr<<"\t"<<endl;
}
else
{
cout<<"抱歉,通讯录中没有此人"<<endl;
}
system("pause");
system("cls");
}
//修改联系人
void modifyperson(addressbooks *abs)
{
cout<<"请输入要修改的联系人的姓名:"<<endl;
string name;
cin>>name;
int num = isExist(abs,name);
if(num != -1)
{
//修改姓名
string name;
cout<<"请输入要修改的姓名"<<endl;
cin>>name;
abs->personnumber[num].m_name = name;
//修改性别
cout<<"请输入性别:"<<endl;
cout<<"1----男"<<endl;
cout<<"2----女"<<endl;
int sex=0;
while(true)
{
cin>>sex;
if(sex==1 || sex==2)
{
abs->personnumber[num].m_sex = sex;
break;
}
cout<<"输入错误,请重新输入!"<<endl;
}
//修改年龄
cout<<"请输入年龄:"<<endl;
int age = 0;
while(true)
{
cin>>age;
if(age>0 && age<150)
{
abs->personnumber[num].m_age = age;
break;
}
cout<<"输入错误,请输入0-150岁之间的年龄"<<endl;
}
//修改电话
cout<<"请输入电话号码:"<<endl;
string phone;
while(true)
{
cin>>phone;
if(phone.length()<12)
{
abs->personnumber[num].m_phone = phone;
break;
}
cout<<"输入有误,请输入正确的电话号码"<<endl;
}
//修改地址
cout<<"请输入家庭住址:"<<endl;
string addr;
cin>>addr;
abs->personnumber[num].m_addr = addr;
cout<<"修改成功"<<endl;
system("pause");
system("cls");
}
}
//清空通讯录
void deleteall(addressbooks *abs)
{
int num;
while (true)
{
cout<<"您确定清空通讯录吗?"<<endl;
cout<<"1----确定清空"<<endl;
cout<<"2----放弃清空"<<endl;
cin>>num;
if(num == 1)
{
abs->m_size = 0;
cout<<"清空成功"<<endl;
system("pause");
system("cls");
break;
}
else if(num == 2)
{
cout<<"您已放弃清空"<<endl;
system("pause");
system("cls");
break;
}
else
{
cout<<"输入无效,请依据提示输入有效字符"<<endl;
system("cls");
}
}
/*if(num ==1)
abs->m_size = 0;
cout<<"清空成功"<<endl;
system("pause");
system("cls");*/
}
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://清空通讯录
deleteall(&abs);
break;
case 0://退出通讯录
cout<<"欢迎下次使用"<<endl;
system("pause");
return 0;
break;
default:
break;
}
}
return 0;
}
存在问题:
性别输入字母字符会死循环。
年龄输入字母字符也会死循环。
清空操作时输入字母字符也会死循环。