#include<iostream>
#include<string>
using namespace std;
#define MAX 1000
//菜单界面
void showmenu()
{
cout << "***** 1.添加联系人 *****" << endl;
cout << "***** 2.显示联系人 *****" << endl;
cout << "***** 3.删除联系人 *****" << endl;
cout << "***** 4.查找联系人 *****" << endl;
cout << "***** 5.修改联系人 *****" << endl;
cout << "***** 6.清空联系人 *****" << endl;
cout << "***** 7.退出 ***********" << endl;
cout << "************************" << endl;
}
//联系人结构体
struct person
{
//姓名
string m_name;
//性别
int m_sex;
//年龄
int m_age;
//电话
string m_phone;
//住址
string m_addr;
};
//设计通讯录结构体
struct addressbooks
{
//通讯录中保存的联系人数组
struct person personarray[MAX];
//通讯录中记录当前联系人个数
int m_size=0;
};
//1.添加联系人
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 = 0;
while (true)
{
cin >> age;
if (age > 0 && age < 150)
{
abs->personarray[abs->m_size].m_age = age;
break;
}
cout << "年龄输入不合理,请重新输入" << endl;
cout << "请输入年龄" << endl;
}
//电话
cout << "请输入联系电话" << endl;
string phone;
while(true)
{
cin >> phone;
int count = phone.length();
if (count<= 11)
{
abs->personarray[abs->m_size].m_phone = phone;
break;
}
cout << "输入有误,请重新输入" << endl;
cout << "请输入电话" << endl;
}
//住址
cout << "请输入家庭住址" << endl;
string address;
cin >> address;
abs->personarray[abs->m_size].m_addr =address;
//更新通讯录人数
abs->m_size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
}
//2.显示联系人
void showperson(addressbooks* abs)
{
if (abs->m_size == 0)
{
cout << "当前记录为空" << endl;
}
else
{
for (int i = 0; i < abs->m_size; i++)
{
cout << "姓名: " << abs->personarray[i].m_name <<"\t";
cout << "性别: " <<(abs->personarray[i].m_sex==1?"男":"女") << "\t";
cout << "年龄: " << abs->personarray[i].m_age <<"\t";
cout << "电话: " << abs->personarray[i].m_phone <<"\t";
cout << "地址: " << abs->personarray[i].m_addr << endl;
}
}
system("pause");
system("cls");
}
//检测用户是否存在
int isexist(addressbooks* abs, string name)
{
for (int i = 0; i < abs->m_size; i++)
{
if (abs->personarray[i].m_name == name)
{
return i;
}
}
return -1;
}
//3.删除指定的联系人
void deleteperson(addressbooks* abs)
{
cout << "请输入你要删除的联系人" << endl;
string name;
cin >> name;
int ret=isexist(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
}
else
{
for (int i = ret; i < abs->m_size; i++)
{
abs->personarray[i] = abs->personarray[i + 1];//数据迁移,进行覆盖
}
abs->m_size--;//每删除一个人通讯录减少一个人
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
//4.查找指定的联系人
void findperson(addressbooks* abs)
{
cout << "请输入你要查找的联系人" << endl;
string name;
cin >> name;
int ret = isexist(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "姓名:" << abs->personarray[ret].m_name << "\t";
cout << "性别:" << (abs->personarray[ret].m_sex == 1 ? "男":"女") << "\t";
cout << "年龄:" << abs->personarray[ret].m_age << "\t";
cout << "电话:" << abs->personarray[ret].m_phone << "\t";
cout << "住址:" << abs->personarray[ret].m_addr << "\t";
}
system("pause");
system("cls");
}
//5.修改联系人
void modifyperson(addressbooks* abs)
{
cout << "请输入你要修改的联系人" << endl;
string name;
cin >> name;
int ret = isexist(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
}
else
{
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personarray[ret].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[ret].m_sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//年龄
int age = 0;
cout << "请输出年龄:" << endl;
cin >> age;
abs->personarray[ret].m_age = age;
//电话
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
abs->personarray[ret].m_phone = phone;
//住址
string address;
cout << "请输入住址" << endl;
cin >> address;
abs->personarray[ret].m_addr = address;
cout << "修改成功" << endl;
}
system("pasue");
system("cls");
}
//清空通讯录
void cleanperson(addressbooks* abs)
{
int n = 0;
cout << "你是否确定清空通讯录" << endl;
cout << "1----确定清空" << endl;
cout << "0----取消清空" << endl;
if (n == 1)
{
abs->m_size = 0;//将记录联系人数量置为0
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
else
{
system("pause");
system("cls");
}
}
int main()
{
int select = 0;//用户输入的数字
addressbooks abs;
while (true)
{
showmenu();
cin >> select;
switch (select)
{
case 1:addperson(&abs);//1.添加联系人
break;
case 2:showperson(&abs);//2.显示联系人
break;
case 3:deleteperson(&abs);//3.删除联系人
break;
case 4:findperson(&abs);//4.查找联系人
break;
case 5:modifyperson(&abs);//5.修改联系人
break;
case 6:cleanperson(&abs);//6.清空联系人
break;
case 7://7.退出
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
10-07