#include<iostream>
#include<string>
#define MAX 100
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 m_Name;
int m_Sex;
int m_Age;
string m_Phone;
string m_Addr;
};
//通讯录结构体
struct Addressbooks
{
struct Person personArray[MAX];
int m_Size;
};
//添加联系人
void addPerson(struct Addressbooks* abs)
{
if (abs->m_Size == MAX)//判断通讯录是否已满
{
printf("通讯录已满");
return;
}
else
{
string name;
printf("请输入姓名:\n");
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
int sex = 0;
printf("请输入性别:\n");
printf("1--男\n");
printf("2--女\n");
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
printf("重新输入\n");
}
int age = 0;
printf("请输入年龄:\n");
cin >> age;
abs->personArray[abs->m_Size].m_Age = age;
string phone;
printf("请输入联系电话:\n");
cin >> phone;
abs->personArray[abs->m_Size].m_Phone = phone;
string addr;
printf("请输入家庭地址:\n");
cin >> addr;
abs->personArray[abs->m_Size].m_Addr = addr;
abs->m_Size++;
printf("输入成功\n");
system("pause");
system("cls");
}
}
//显示联系人
void showPerson(struct Addressbooks* abs)
{
if (abs->m_Size == 0)
{
printf("记录为空");
}
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 isExit(struct 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(struct Addressbooks* abs)
{
printf("请输入要删除的联系人\n");
string name;
cin >> name;
int ret = isExit(abs, name);
if (ret != -1)
{
for (int i = ret; i < abs->m_Size; i++)
{
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_Size--;
}
else
{
printf("查无此人\n");
}
system("pause");
system("cls");
}
//查找联系人
void findPerson(struct Addressbooks* abs)
{
printf("请输入要查找的联系人\n");
string name;
cin >> name;
int ret = isExit(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
printf("查无此人\n");
system("pause");
system("cls");
}
//修改联系人
void modifyPerson(struct Addressbooks* abs)
{
printf("请输入要修改的联系人\n");
string name;
cin >> name;
int ret = isExit(abs, name);
if (ret != -1)
{
string name;
printf("请输入姓名:\n");
cin >> name;
abs->personArray[ret].m_Name = name;
int sex = 0;
printf("请输入性别:\n");
printf("1--男\n");
printf("2--女\n");
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[ret].m_Sex = sex;
break;
}
printf("重新输入\n");
}
int age = 0;
printf("请输入年龄:\n");
cin >> age;
abs->personArray[ret].m_Age = age;
string phone;
printf("请输入联系电话:\n");
cin >> phone;
abs->personArray[ret].m_Phone = phone;
string addr;
printf("请输入家庭地址:\n");
cin >> addr;
abs->personArray[ret].m_Addr = addr;
}
else
printf("查无此人\n");
system("pause");
system("cls");
}
//清空联系人
void cleanPerson(struct Addressbooks* abs)
{
abs->m_Size = 0;
printf("通讯录已清空");
system("pause");
system("cls");
}
//主函数
int main()
{
struct Addressbooks abs;
abs.m_Size = 0;
int select = 0;//用户输入
while (true)
{
showMenu();
scanf_s("%d", &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://退出通讯录
printf("欢迎下次使用\n");
return 0;
break;
default:
break;
}
}
return 0;
}
/*读书笔记
还有很多可以优化的空间
*/