通讯录管理系统
系统需求:
通讯录是一个可以记录亲人、好友信息的工具。本文主要利用C++来实现一个通讯录管理系统系统中需要实现的功能如下:
添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
显示联系人:显示通讯录中所有联系人信息
删除联系人:按照姓名进行删除指定联系人
查找联系人:按照姓名查看指定联系人信息
修改联系人:按照姓名重新修改指定联系人
清空联系人:清空通讯录中所有信息
退出通讯录:退出当前使用的通讯录
1.结构体定义
// 联系人结构体
struct Person
{string m_name;int m_sex; // 1:男 2:女int m_age;string m_phone;string m_addr;
};// 通讯录的结构体
struct Addressbooks
{// 当前保存联系人的数组struct Person personArray[MAX];// 当前保存联系人的个数int m_size;
};
2.添加函数
// 1、添加联系人
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;}else{cout << "请重新输入:" << endl;}}// 年龄cout << "请输入联系人年龄:" << endl;int age = 0;cin >> age;abs->personArray[abs->m_size].m_age = age;// 电话cout << "请输入联系人电话:" << endl;string phone;cin >> phone;abs->personArray[abs->m_size].m_phone = phone;// 地址cout << "请输入联系人地址:" << endl;string addr;cin >> addr;abs->personArray[abs->m_size].m_addr = addr;// 更新通讯录人数abs->m_size++;cout << "添加成功!" << endl;system("pause");system("cls");}
}
3.显示函数
// 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_addr << endl;}}system("pause");system("cls");
}
4.删除函数
// 3、删除联系人// 检测联系人是否存在
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;
}void delatePerson(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->m_size; i++){abs->personArray[i] = abs->personArray[i + 1];}abs->m_size--;cout << "删除成功" << endl;}system("pause");system("cls");
}
5.查找函数
//4、查找联系人
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].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_addr << endl;}system("pause");system("cls");
}
6.修改函数
//5、修改联系人
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].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;}else{cout << "请重新输入:" << endl;}}// 年龄cout << "请输入联系人年龄:" << endl;int age = 0;cin >> age;abs->personArray[ret].m_age = age;// 电话cout << "请输入联系人电话:" << endl;string phone;cin >> phone;abs->personArray[ret].m_phone = phone;// 地址cout << "请输入联系人地址:" << endl;string addr;cin >> addr;abs->personArray[ret].m_addr = addr;cout << "修改成功!" << endl;}system("pause");system("cls");
}
7.主函数调用
int main()
{// 创建通讯录结构体变量Addressbooks abs;// 初始化通讯录中人员个数abs.m_size = 0;int select = 0;while (true){showMenu();cout << "请选择你要实现的功能:" << endl;cin >> select;switch (select){case 1: // 1、添加联系人addPerson(&abs); // 地址传递可以修饰实参break;case 2: // 2、显示联系人showPerson(&abs);break;case 3: // 3、删除联系人delatePerson(&abs);break;case 4: // 4、查找联系人findPerson(&abs);break;case 5: // 5、修改联系人modifyPerson(&abs);break;case 6: // 6、清空联系人clearPerson(&abs);break;case 0: // 0、退出通讯录cout << "欢迎下次使用!" << endl;system("pause");return 0;break;default:break;}}system("pause");return 0;
}
总代码汇总:
#include<iostream>
using namespace std;
#include<string>
#define MAX 1000// 菜单界面
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;
}// 联系人结构体
struct Person
{string m_name;int m_sex; // 1:男 2:女int m_age;string m_phone;string m_addr;
};// 通讯录的结构体
struct Addressbooks
{// 当前保存联系人的数组struct Person personArray[MAX];// 当前保存联系人的个数int m_size;
};// 1、添加联系人
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;}else{cout << "请重新输入:" << endl;}}// 年龄cout << "请输入联系人年龄:" << endl;int age = 0;cin >> age;abs->personArray[abs->m_size].m_age = age;// 电话cout << "请输入联系人电话:" << endl;string phone;cin >> phone;abs->personArray[abs->m_size].m_phone = phone;// 地址cout << "请输入联系人地址:" << endl;string addr;cin >> addr;abs->personArray[abs->m_size].m_addr = addr;// 更新通讯录人数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_addr << endl;}}system("pause");system("cls");
}// 3、删除联系人// 检测联系人是否存在
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;
}void delatePerson(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->m_size; i++){abs->personArray[i] = abs->personArray[i + 1];}abs->m_size--;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 << "查无此人" << endl;}else{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_addr << endl;}system("pause");system("cls");
}//5、修改联系人
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].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;}else{cout << "请重新输入:" << endl;}}// 年龄cout << "请输入联系人年龄:" << endl;int age = 0;cin >> age;abs->personArray[ret].m_age = age;// 电话cout << "请输入联系人电话:" << endl;string phone;cin >> phone;abs->personArray[ret].m_phone = phone;// 地址cout << "请输入联系人地址:" << endl;string addr;cin >> addr;abs->personArray[ret].m_addr = addr;cout << "修改成功!" << endl;}system("pause");system("cls");
}//6、清空联系人
void clearPerson(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();cout << "请选择你要实现的功能:" << endl;cin >> select;switch (select){case 1: // 1、添加联系人addPerson(&abs); // 地址传递可以修饰实参break;case 2: // 2、显示联系人showPerson(&abs);break;case 3: // 3、删除联系人delatePerson(&abs);break;case 4: // 4、查找联系人findPerson(&abs);break;case 5: // 5、修改联系人modifyPerson(&abs);break;case 6: // 6、清空联系人clearPerson(&abs);break;case 0: // 0、退出通讯录cout << "欢迎下次使用!" << endl;system("pause");return 0;break;default:break;}}system("pause");return 0;
}