利用C++来实现一个通讯录管理系统
系统中需要实现的功能如下:
添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄,联系电话、家庭住址)最多记录1000人
显示联系人:显示通讯录中所有联系人信息
删除联系人:按照姓名进行删除指定联系人
查找联系人:按照姓名查看指定联系人信息
修改联系人:按照姓名重新修改指定联系人
清空联系人:清空通讯录中所有信息
退出通讯录:退出当前使用的通讯录
代码实现
- 结构体
//联系人结构体
struct Person{
string Name;//姓名
int Sex;//性别
int Age;//年龄
string Phone;//电话
string Addr;//住址
};
// 通讯录结构体
struct Addressbooks
{
//通讯录中保存的联系人数组
struct Person personArray[MAX];
//通讯录中当前记录联系人个数
int Size;
};
- 主函数
#include<iostream>
#include<string>
#define MAX 1000
using namespace std;
int main() {
Addressbooks abs;
abs.Size = 0;
int select = 0;
while (true)
{
showMenu();
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;
system("pause");
return 0;
break;
default:
cout << "请重新输入" << endl;
break;
}
system("pause");//请按任意键继续
system("cls");//清屏
}
}
- 菜单界面
//菜单界面
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->Size==MAX)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else {
string name;//姓名
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[abs->Size].Name=name;
int sex = 0;//性别
cout << "请输入性别:" << endl;
cout << "1---男" << "\t";
cout << "2---女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2) {
abs->personArray[abs->Size].Sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
cout << "1---男" << "\t";
cout << "2---女" << endl;
}
int age = 0;//年龄
cout << "请输入年龄" << endl;
cin >> age;
abs->personArray[abs->Size].Age = age;
string phone;//电话
cout << "请输入电话:" << endl;
cin >> phone;
abs->personArray[abs->Size].Phone = phone;
string addr;//住址
cout << "请输入住址:" << endl;
cin >> addr;
abs->personArray[abs->Size].Addr = addr;
abs->Size= abs->Size+1;
cout << "添加成功" << endl;
}
}
- 显示联系人
//显示联系人
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].Phone << "\t";
cout << "住址:" << abs->personArray[i].Addr << endl;
}
}
}
- 检测是否存在
//检测是否存在,如果存在,返回联系人所在数组的具体位置,不存在返回-1
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 deletePerson(Addressbooks* abs) {
cout << "请输入要删除的联系人姓名" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret==-1){
cout << "查无此人" << endl;
}
else
{
for (int i = ret; i < abs->Size;i++) {
abs->personArray[i] = abs->personArray[i + 1];
}
abs->Size--;
cout << "删除成功" << endl;
}
}
- 查找指定联系人
//查找指定联系人信息
void findPerson(Addressbooks* abs) {
cout << "请输入要查找的联系人姓名" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret == -1) {
cout << "查无此人" << endl;
}
else
{
cout << "姓名:" << abs->personArray[ret].Name << "\t";
cout << "性别:" << (abs->personArray[ret].Sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArray[ret].Age << "\t";
cout << "电话:" << abs->personArray[ret].Phone << "\t";
cout << "住址:" << abs->personArray[ret].Addr << endl;
}
}
- 删除指定联系人
//修改指定联系人信息
void modifyPerson(Addressbooks* abs) {
cout << "请输入要修改的联系人姓名" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret == -1) {
cout << "查无此人" << endl;
}
else
{
string name;//姓名
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[ret].Name = name;
int sex = 0;//性别
cout << "请输入性别:" << endl;
cout << "1---男" << "\t";
cout << "2---女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2) {
abs->personArray[ret].Sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
cout << "1---男" << "\t";
cout << "2---女" << endl;
}
int age = 0;//年龄
cout << "请输入年龄" << endl;
cin >> age;
abs->personArray[ret].Age = age;
string phone;//电话
cout << "请输入电话:" << endl;
cin >> phone;
abs->personArray[ret].Phone = phone;
string addr;//住址
cout << "请输入住址:" << endl;
cin >> addr;
abs->personArray[ret].Addr = addr;
cout << "修改成功" << endl;
}
}
- 清空联系人
//清空所有联系人
void cleanPerson(Addressbooks* abs) {
abs->Size=0;
cout << "通讯录已清空" << endl;
}