效果图
需求
通讯录是一个可以记录亲人、好友信息的工具。
本文主要利用 C++ 来实现一个通讯录管理系统。
实现功能:
- 添加联系人
- 显示联系人
- 删除联系人
- 查找联系人
- 修改联系人
- 退出通讯录
代码
#include <iostream>
#include <string>
using namespace std;
#define MAX 1000
struct Person
{
string m_Name; //姓名
int m_Sex; // 性别 1. 男 2. 女
int m_Age; // 年龄
string m_Phone; // 电话
string m_Address; //地址
};
struct AddressBooks
{
struct 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 addPerson(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;
// 添加性别
int sex = 0;
cout << "请输入联系人的性别:" << endl;
cout << "1 --- 男:" << endl;
cout << "2 --- 女:" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
cout << "输入信息有误,请重新输入" << endl;
}
// 添加年龄
int age;
cout << "请输入联系人的年龄:" << endl;
cin >> age;
abs->personArray[abs->m_Size].m_Age = age;
// 添加电话
string phone;
cout << "请输入联系人的电话:" << endl;
cin >> phone;
abs->personArray[abs->m_Size].m_Phone = phone;
// 添加地址
string address;
cout << "请输入联系人的地址:" << endl;
cin >> address;
abs->personArray[abs->m_Size].m_Address = address;
// 更新通讯录人数
abs->m_Size++;
cout << "添加成功" << endl;
system("pause"); // 请按任意键继续
system("cls"); // 清屏操作
}
}
// 2. 显示联系人
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_Address << 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;
}
// 3. 删除联系人
void deletePerson(AddressBooks* abs)
{
cout << "请输入您要删除的联系人: " << endl;
string name;
cin >> name;
int result = isExist(abs, name);
if (result != -1)
{
for (int i = result; i < abs->m_Size; i++)
{
// 数据前移
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_Size--; //更新通讯录中的人员数
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
// 4. 查找联系人
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_Address << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
// 5. 修改联系人
void modfiyPerson(AddressBooks* abs)
{
cout << "请输入您要修改的联系人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
// 添加姓名
string name;
cout << "请输入联系人的姓名:" << endl;
cin >> name;
abs->personArray[ret].m_Name = name;
// 添加性别
int sex = 0;
cout << "请输入联系人的性别:" << endl;
cout << "1 --- 男:" << endl;
cout << "2 --- 女:" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[ret].m_Sex = sex;
break;
}
cout << "输入信息有误,请重新输入" << endl;
}
// 添加年龄
int age;
cout << "请输入联系人的年龄:" << endl;
cin >> age;
abs->personArray[ret].m_Age = age;
// 添加电话
string phone;
cout << "请输入联系人的电话:" << endl;
cin >> phone;
abs->personArray[ret].m_Phone = phone;
// 添加地址
string address;
cout << "请输入联系人的地址:" << endl;
cin >> address;
abs->personArray[ret].m_Address = address;
cout << "修改成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
// 6. 清空联系人
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();
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: // 修改联系人
modfiyPerson(&abs);
break;
case 6: // 清空联系人
cleanPerson(&abs);
break;
case 0: // 退出通讯录
cout << "欢迎再次使用通讯录管理系统" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}