#include
#define MAX 1000
using namespace std;
// 通讯录管理系统,该项目只有 单个 cpp文件
//1.添加联系人
//2.显示联系人
//3.删除联系人
//4.查找联系人
//5.修改联系人
//6.清空联系人
//7.退出系统
// 个人信息
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;
};
// 界面菜单
void showMenu()
{
cout << "*************************" << endl;
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 addPenson(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;
string phone;
cin >> phone;
abs->personArray[abs->m_Size].m_Phone = phone;
//住址
cout << "最后输入住址:" << endl;
string addr;
cin >> addr;
abs->personArray[abs->m_Size].m_Addr = addr;
//更新通讯录人数
abs->m_Size++; //通讯录中人数加1
//完成添加提示
cout << "恭喜您添加成功!" << endl;
system("pause"); //按任意键继续
system("cls"); //清屏操作
}
}
// 显示联系人
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;
}
// 删除
void deletePerson(Addressbooks* abs)
{
cout << "请输入删除联系人的姓名:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -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)
{
cout << "请输入要查找的联系人:" << endl;
string name;
cin >> name;
// 判空
int ret = isExist(abs, name);
if (ret != -1)
{
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 << endl;
}
else
{
cout << "查无此人,请重新输入!" << endl;
}
system("pause");
system("cls");
}
// 修改联系人
void modifyPerson(Addressbooks * abs)
{
cout << "请输入要修改的联系人:" << endl;
string name;
cin >> name;
// 判空
int ret = isExist(abs, name);
if (ret != -1)
{
cout << "请输出修改后的姓名:" << endl;
string name;
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;
}
// 年龄
cout << "请输入修改的年龄:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age >= 0 || age <= 150)
{
abs->personArray[ret].m_Age = age;
break;
}
cout << "请输入正确年龄!" << endl;
}
// 电话
cout << "输入电话:" << endl;
string phone;
cin >> phone;
abs->personArray[ret].m_Phone = phone;
//住址
cout << "最后输入住址:" << endl;
string addr;
cin >> addr;
abs->personArray[ret].m_Addr = addr;
cout << "恭喜您修改成功!" << endl;
}
else
{
cout << "查无此人,请重新输入!" << endl;
}
system("pause");
system("cls");
}
// 清空联系人
void cleanPerson(Addressbooks * abs)
{
abs->m_Size = 0;
cout << "通讯录已清空!" << endl;
system("pause");
system("cls");
}
int main()
{
// 创建通讯录
Addressbooks abs;
abs.m_Size = 0;
int select = 0;
while (true)
{
showMenu();
cout << "请输入您的操作数字:";
cin >> select;
switch (select)
{
case 1:
// 添加联系人
addPenson(&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:
break;
}
}
system("pause");
return 0;
}