通讯录是一个可以记录亲人、好友信息的工具。
本教程主要利用C++来实现一个通讯录管理系统系统中需要实现的功能如下:
添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
显示联系人:显示通讯录中所有联系人信息;
删除联系人:按照姓名进行删除指定联系人;
查找联系人:按照姓名查看指定联系人信息;
修改联系人: 按照姓名重新修改指定联系人;
清空联系人:清空通讯录中所有信息;
退出通讯录:退出当前使用的通讯录;
#include <iostream> #include <string> #define MAX 1000//最大人数 using namespace std; //菜单功能 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;//通讯录中人员的个数 }; //添加联系人 void addPerson(Addressbooks *p) { if (p->m_Size == MAX) { cout << "无法添加联系人" << endl; return; } string name; cout << "请输入姓名:" << endl; cin >> name; int sex; cout << "请输入性别:" << endl; cout << "1 男" << endl; cout << "2 女" << endl; while (true) { cin >> sex; if (sex == 1 || sex == 2) { break; } else { cout << "输入有误,请重新输" << endl; } } int age; cout << "请输入年龄:" << endl; cin >> age; string phone; cout << "请输入电话:" << endl; cin >> phone; string address; cout << "请输入地址:" << endl; cin >> address; p->personArray[p->m_Size] = {name, sex, age, phone, address}; p->m_Size++; cout << "添加成功" << endl; system("pause"); //请按任意键继续 system("cls"); //清屏操作 } //显示联系人 void showPerson(Addressbooks *abs) { if (abs->m_Size == 0) { cout << "当前记录为空" << endl; return; } for (int i = 0; i < abs->m_Size; ++i) { Person tem = abs->personArray[i]; cout << "姓名:" << tem.m_name << "\t" << " 年龄:" << tem.m_age << "\t" << " 性别:" << (tem.m_Sex == 1 ? "男" : "女") << "\t" << " 电话:" << tem.m_Phone << "\t" << " 地址:" << tem.m_Addr << endl; } system("pause"); //请按任意键继续 system("cls"); //清屏操作 } //检测联系人是否存在 如果存在 返回位置 否则返回-1; int isExist(Addressbooks *p, string name) { if (p->m_Size == 0) { return -1; } for (int i = 0; i < p->m_Size; ++i) { if (p->personArray[i].m_name == name) { return i; } } return -1; } //删除指定联系人 void deletePerson(Addressbooks *p) { cout << "请输入删除联系人的姓名:" << endl; string name; cin >> name; int ret = isExist(p, name); if (ret == -1) { cout << "查无此人" << endl; } else { for (int i = ret; i < p->m_Size; ++i) { p->personArray[i] = p->personArray[i + 1]; } p->personArray[p->m_Size - 1] = {}; p->m_Size--; cout << "删除成功" << endl; } system("pause"); //请按任意键继续 system("cls"); //清屏操作 } //查找联系人 void findPerson(Addressbooks *p) { cout << "请输入您要查找的联系人" << endl; string name; cin >> name; int ret = isExist(p, name); if (ret != -1) { Person tem = p->personArray[ret]; cout << "姓名:" << tem.m_name << "\t" << " 年龄:" << tem.m_age << "\t" << " 性别:" << (tem.m_Sex == 1 ? "男" : "女") << "\t" << " 电话:" << tem.m_Phone << "\t" << " 地址:" << tem.m_Addr << endl; } else { cout << "查无此人" << endl; } system("pause"); //请按任意键继续 system("cls"); //清屏操作 } //修改联系人 void modifyPerson(Addressbooks *p) { cout << "请输入您要修改的联系人" << endl; string name; cin >> name; int ret = isExist(p, name); if (ret != -1) { string name; cout << "请输入姓名:" << endl; cin >> name; int sex; cout << "请输入性别:" << endl; cout << "1 男" << endl; cout << "2 女" << endl; while (true) { cin >> sex; if (sex == 1 || sex == 2) { break; } else { cout << "输入有误,请重新输" << endl; } } int age; cout << "请输入年龄:" << endl; cin >> age; string phone; cout << "请输入电话:" << endl; cin >> phone; string address; cout << "请输入地址:" << endl; cin >> address; p->personArray[ret] = {name, sex, age, phone, address}; cout << "修改成功" << endl; } else { cout << "查无此人" << endl; } system("pause"); //请按任意键继续 system("cls"); //清屏操作 } //清空联系人 void cleanPerson(Addressbooks *p) { p->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: //修改联系人 modifyPerson(&abs); break; case 6: //清空联系人 cleanPerson(&abs); break; case 0: //退出通讯录 cout << "欢迎下次使用" << endl; return 0; break; default: break; } } return 0; }