要求
使用C++编写一个通讯录管理系统,要求如下:
1、要保存的联系人信息为:姓名,性别,年龄,电话号码,住址。
2、系统要具备这些功能:添加联系人,显示联系人,删除联系人,查找联系人,修改联系人,清空联系人,退出通讯录。
3、能保存1000个人的信息。
代码
#include <iostream>
#include <string>
#define Max 1000
using namespace std;
// 联系人结构体
struct Person
{
// 姓名
string Name;
// 性别,1男2女
int Sex;
// 年龄
int Age;
// 电话
string Phone;
// 住址
string Addr;
};
// 通讯录结构体
struct AdderessBook
{
// 通讯录中保存联系人的数组
struct Person personArray[Max];
// 通讯录中当前记录的联系人数量
int Size;
};
// 菜单界面
void showMeun()
{
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(AdderessBook* abs)
{
//判断通讯录是否已满
if (abs->Size == Max)
{
cout << "通讯录已满!" << endl;
return;
}
else
{
// 姓名
string name;
cout << "请输入姓名:";
cin >> name;
abs->personArray[abs->Size].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->Size].Sex = sex;
break;
}
cout << "输入有误,请重新输入!" << endl;
}
//年龄
cout << "请输入年龄:" ;
int age = 0;
while (true)
{
cin >> age;
if (age >= 0)
{
abs->personArray[abs->Size].Age = age;
break;
}
cout << "输入有误,请重新输入!" << endl;
}
// 电话
cout << "请输入联系电话:";
string phone;
cin >> phone;
abs->personArray[abs->Size].Phone = phone;
//住址
cout << "请输入家庭住址:";
string address;
cin >> address;
abs->personArray[abs->Size].Addr = address;
// 更新通讯录人数
abs->Size++;
cout << "添加成功!" << endl;
system("pause");
system("cls");
}
}
// 显示联系人
void showPerson(AdderessBook* abs)
{
// 判断通讯录中人数是否为0
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].Phone << "\t";
cout << "住址:" << abs->personArray[i].Addr << endl;
}
}
system("pause");
system("cls");
}
// 检测联系人是否存在,存在则返回联系人所在位置,不存在则返回-1
int checkPerson(AdderessBook* abs, string name)
{
for (int i = 0; i < abs->Size; i++)
{
if (abs->personArray[i].Name == name)
{
return i;
}
}
return -1;
}
// 删除指定联系人
void deletePerson(AdderessBook* abs)
{
cout << "请输入要删除的联系人姓名:";
string name;
cin >> name;
int res = checkPerson(abs, name);
if (res != -1)
{
for (int i = res; i < abs->Size - 1; i++)
{
abs->personArray[i] = abs->personArray[i + 1];
}
abs->Size--;
cout << "删除成功!" << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
// 查找联系人
void findPerson(AdderessBook* abs)
{
cout << "请输入要查找的联系人姓名:" ;
string name;
cin >> name;
int res = checkPerson(abs, name);
if (res != -1)
{
cout << "姓名:" << abs->personArray[res].Name << "\t";
cout << "性别:" << (abs->personArray[res].Sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArray[res].Age << "\t";
cout << "电话:" << abs->personArray[res].Phone << "\t";
cout << "住址:" << abs->personArray[res].Addr << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
// 修改联系人
void modifyPerson(AdderessBook* abs)
{
cout << "请输入要修改的联系人姓名:" ;
string name;
cin >> name;
int res = checkPerson(abs, name);
if (res != -1)
{
cout << "请输入姓名:";
string name;
cin >> name;
abs->personArray[res].Name = name;
cout << "请输入性别:" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
int sex;
while (true) // 检测是否输入有误
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->Size].Sex = sex;
break;
}
cout << "输入有误,请重新输入!" << endl;
}
cout << "请输入年龄:";
int age = 0;
while (true)
{
cin >> age;
if (age >= 0)
{
abs->personArray[abs->Size].Age = age;
break;
}
cout << "输入有误,请重新输入!" << endl;
}
cout << "请输入联系电话:";
string phone;
cin >> phone;
abs->personArray[abs->Size].Phone = phone;
cout << "请输入家庭住址:";
string address;
cin >> address;
abs->personArray[abs->Size].Addr = address;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
//清空通讯录
void cleanPerson(AdderessBook* abs)
{
abs->Size = 0;
cout << "通讯录已清空!" << endl;
system("pause");
system("cls");
}
int main()
{
// 创建通讯录结构体变量
AdderessBook abs;
// 初始化
abs.Size = 0;
// 主体结构
while (true)
{
// 显示菜单
showMeun();
int select;
cout << "请输入操作对应编号:";
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:
cleanPerson(&abs);
break;
case 0:
cout << "欢迎下次使用!" << endl;
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}