问题:
编写一个通讯录管理系统实现以下功能:
1.添加联系人:添加联系人的姓名,性别,年龄,电话,地址信息,可多次添加
2.显示联系人:显示已经添加的所有联系人
3.删除联系人:输入姓名后删除该联系人全部信息
4.查找联系人:输入姓名后查找该联系人
5.修改联系人:输入姓名后修改联系人全部信息
6.清空通讯录:删除所有联系人信息
0.退出系统
代码:
//封装函数显示页面,如 viod showMenu()
//在main函数中调用封装好的函数
#include<iostream>
using namespace std;
#include<string>
#define MAX 1000
//设计联系人结构体
struct Person {
string m_Name;
int m_Sex;
int m_Age;
string m_Phone;
string m_Addr;
};
//设计通讯录结构体
struct Adderssbooks {
//通讯录中保存的联系人数组
Person personArray[MAX];
//通信录中当前记录联系人个数
int m_size;
};
//菜单界面
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;
}
//输入信息
void printfPerson(Adderssbooks* abs, int num)
{
//添加具体联系人
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[num].m_Name = name;
//性别
int sex = 0;
cout << "请输入性别:" << endl;
cout << "1---- 男:" << endl;
cout << "2---- 女:" << endl;
while (1)
{
cin >> sex;
//如果输入的是1或者2可以退出循环,如果输入有误,重新输入
if (sex == 1 || sex == 2)
{
abs->personArray[num].m_Sex = sex;
break;
}
else
{
cout << "性别输入错误,请重新输入!" << endl;
}
}
//年龄
int age = 0;
cout << "请输入年龄:" << endl;
cin >> age;
abs->personArray[num].m_Age = age;
//电话
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
abs->personArray[num].m_Phone = phone;
//地址
string addr;
cout << "请输入地址:" << endl;
cin >> addr;
abs->personArray[num].m_Addr = addr;
}
//检测联系人是否存在,如果存在,返回联系人所在数组中的具体位置,不存在返回-1
//参数1:通讯录 参数2:对比姓名
int isExist(Adderssbooks* abs, string name)
{
for (int i = 0; i < abs->m_size; i++)
{
if (abs->personArray[i].m_Name == name)
{
return i;
}
}
return -1;
}
//1.添加联系人
void addPerson(Adderssbooks* abs)
{
//判断通讯录是都已满,如果满了就不再添加
if (abs->m_size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
//添加联系人
printfPerson(abs, abs->m_size);
//更新通讯录人数
abs->m_size++;
cout << "添加成功!" << endl;
}
system("pause");
system("cls");//清屏
}
//2、显示联系人
void showPerson(Adderssbooks* abs)
{
//判断通讯录中人数是否为0,为0提示为空,不为0输出联系人信息
if (abs->m_size == 0)
{
cout << "通讯录记录为空" << endl;
}
else
{
cout << "姓名\t" << "性别\t" << "年龄\t" << "电话\t" << "住址\t" << endl;
for (int i = 0; i < abs->m_size; i++)
{
cout << abs->personArray[i].m_Name << "\t"
<< (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t"
<< abs->personArray[i].m_Age << "\t"
<< abs->personArray[i].m_Phone << "\t"
<< abs->personArray[i].m_Addr << "\t" << endl;
}
}
system("pause");
system("cls");//清屏
}
//3.删除联系人
void delPerson(Adderssbooks* abs)
{
//判断通讯录中人数是否为0
if (abs->m_size == 0)
{
cout << "通讯录记录为空" << endl;
}
else
{
string name;
cout << "请输入删除联系人姓名:" << endl;
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(Adderssbooks* abs)
{
string name;
cout << "请输入查找联系人姓名:" << endl;
cin >> name;
int ret = isExist(abs, name);
if (ret == -1)
{
cout << "查无此人!" << endl;
}
else
{
cout << "姓名\t" << "性别\t" << "年龄\t" << "电话\t" << "住址\t" << endl;
cout << abs->personArray[ret].m_Name << "\t"
<< (abs->personArray[ret].m_Sex == 1 ? "男" : "女") << "\t"
<< abs->personArray[ret].m_Age << "\t"
<< abs->personArray[ret].m_Phone << "\t"
<< abs->personArray[ret].m_Addr << "\t" << endl;
cout << "查找成功!" << endl;
}
system("pause");
system("cls");//清屏
}
//5.修改联系人
void modifyPerson(Adderssbooks* abs)
{
//判断通讯录中人数是否为0
if (abs->m_size == 0)
{
cout << "通讯录记录为空" << endl;
}
else
{
cout << "请输入要修改的联系人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret == -1)
{
cout << "查无此人!" << endl;
}
else
{
//修改联系人
printfPerson(abs, ret);
}
}
system("pause");
system("cls");//清屏
}
//6.清空联系人
//只要将通讯录记录的联系人数量重置为0
void cleanPerson(Adderssbooks* abs)
{
cout << "是否确定清空通讯录?" << endl;
cout << "Y/N" << endl;
string s;
cin >> s;
if (s != "Y")
{
;
}
else
{
abs->m_size = 0;
cout << "通讯录已清空!" << endl;
}
system("pause");
system("cls");//清屏
}
int main()
{
//创建通讯录结构体变量
Adderssbooks abs;
abs.m_size = 0;
int select = 0; //创建用户选择输入的变量
while (1)
{
//菜单调用
showMenu();
cout << "请输入要执行的功能:";
cin >> select;
switch (select)
{
case 1: //1、添加联系人
addPerson(&abs); //利用地址传递,可以修饰形参
break;
case 2: //2、显示联系人
showPerson(&abs);
break;
case 3: //3、删除联系人
delPerson(&abs);
break;
case 4: //4、查找联系人
findPerson(&abs);
break;
case 5: //5、修改联系人
modifyPerson(&abs);
break;
case 6: //6、清空联系人
cleanPerson(&abs);
break;
case 0: //0、退出通讯录
cout << "欢迎下次使用!" << endl;
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
}
结果:
注:该版本能实现基本功能,但还可以优化细节。