效果
1.菜单
![总菜单](https://i-blog.csdnimg.cn/blog_migrate/c93547a35a56c2c233355e24e49724be.png)
2.添加联系人
![添加联系人](https://i-blog.csdnimg.cn/blog_migrate/abdfe63ba5068b2004a1fdbba238c6dd.png)
3.显示联系人
![显示联系人](https://i-blog.csdnimg.cn/blog_migrate/437d3b089890822ddf31d560f18b6107.png)
4.删除联系人
5.查找联系人
6.修改联系人
7.清空联系人
8.退出
#include<iostream>
#include<string>
#define max 1000
using namespace std;
struct Person
{
string name;
int sex;
int age;
string phone;
string Addr;
};
struct Addressbooks
{
struct Person personArray[max];
int size;
};
void shoeMenu()
{
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 addPerson(Addressbooks *abs)
{
if (abs->size == max)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
cout << "请输入:" << endl << "姓名:";
cin>>abs->personArray[abs->size].name;
cout<< endl << "性别(1.男 2.女):";
cin >> abs->personArray[abs->size].sex;
cout << endl << "年龄:";
cin >> abs->personArray[abs->size].age;
cout << endl << "电话:";
cin >> abs->personArray[abs->size].phone;
cout << endl << "住址:";
cin >> abs->personArray[abs->size].Addr;
cout << "添加成功!!" << endl;
}
abs->size++;
system("pause");
system("cls");
}
void showPerson(Addressbooks *abs)
{
if (abs->size == 0)
{
cout << "通讯录为空!" << endl;
}
else
{
for (int i = 0; i < abs->size; i++)
{
cout << "姓名:"<<abs->personArray[i].name <<"\t";
cout << "性别:" << (abs->personArray[i].sex==1?"男":"女") << "\t";
cout << "年龄:" << abs->personArray[i].age << "\t";
cout << "地址:" << abs->personArray[i].Addr << "\t";
cout << "电话:" << abs->personArray[i].phone << endl;
}
system("pause"); system("cls");
}
}
int isExist(Addressbooks *abs, string name)
{
for (int i = 0; i < abs->size; i++)
{
if (abs->personArray[i].name == name)
{
return i;
}
}
return -1;
}
void findPerson(Addressbooks *abs)
{
cout << "请输入您要查找的联系人姓名:";
string name;
cin >> name;
int flag = isExist(abs, name);
if (flag != -1)
{
cout << "姓名:" << abs->personArray[flag].name << "\t";
cout << "性别:" << (abs->personArray[flag].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArray[flag].age << "\t";
cout << "地址:" << abs->personArray[flag].Addr << "\t";
cout << "电话:" << abs->personArray[flag].phone << endl;
}
else
{
cout << "查无此人!";
}
system("pause"); system("cls");
}
void modifyPerson(Addressbooks *abs)
{
string name;
cout << "请输入您要修改的联系人姓名:";
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
cout << "请修改:" << endl << "姓名:";
cin >> abs->personArray[ret].name;
cout << endl << "性别(1.男 2.女):";
cin >> abs->personArray[ret].sex;
cout << endl << "年龄:";
cin >> abs->personArray[ret].age;
cout << endl << "电话:";
cin >> abs->personArray[ret].phone;
cout << endl << "住址:";
cin >> abs->personArray[ret].Addr;
cout << "修改成功!!" << endl;
}
else
cout << "查无此人!";
system("pause"); system("cls");
}
void cleanPeople(Addressbooks *abs)
{
abs->size = 0;
cout << "通讯录已清空" << endl;
system("pause"); system("cls");
}
int main()
{
Addressbooks abs;
Person personArray[max];
abs.size = 0;
int flag = 0;
string name;
while (1)
{
shoeMenu();
cin >> flag;
if (flag == 0)
{
cout << "欢迎下次使用" << endl;
break;
}
switch (flag)
{
case 0:cout << "欢迎下次使用" << endl; break;break;
case 1:addPerson(&abs); break;
case 2:showPerson(&abs); break;
case 3:
cout << "请输入要删除的联系人姓名:";
cin >> name;
cout << (isExist(&abs, name) == -1 ? "查无此人!" : "you") << endl;
; break;
case 4:findPerson(&abs); break;
case 5:modifyPerson(&abs); break;
case 6: cleanPeople(&abs); break;
}
}
system("pause");
}