C++实现简易通讯录功能
目录
- 实现功能
- 一、定义通讯录和通讯录人员结构体
- 二、实现通讯录输入菜单
- 1.定义菜单函数:
- 2.主函数循环体中循环展示菜单信息
- 三、实现增删改查和清空通讯录功能
- 四、运行截图展示
【C++实现简易通讯录功能】
实现功能 提示:这里可以添加本文要记录的大概内容:
通过c++语法的学习,实现一个简易的通讯录功能,具体实现功能如下:
添加联系人:向通讯录中添加联系人,信息包括(姓名、性别、年龄、联系电话、家庭住址),做多记录1000人
显示联系人:显示通讯录中所有联系人信息
删除联系人:按照姓名进行删除联系人
查找联系人:按照姓名进行查找联系人
修改联系人:根据姓名重新修改联系人
清空联系人:清空通讯录中所有信息
脱出通讯录:退出当前使用的通讯录
一、定义通讯录和通讯录人员结构体 定义通讯录和通讯录内人员信息的结构体,通讯录最多容纳1000人信息。
联系人结构体:
//联系人结构体struct Person{string m_Name; //姓名int m_Sex; //1-男,2-女int m_Age; //年龄string m_Phone; //电话string m_Afddar; //通讯地址};
通讯录结构体:
struct AddaressBooks{struct Person personArray[MAX]; //通讯录中保存的人员数据int m_Size; //记录当前记录数};
二、实现通讯录输入菜单
1.定义菜单函数:
//菜单界面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; }
2.主函数循环体中循环展示菜单信息
int main() {//创建通讯录结构体变量AddaressBooks abs; //初始化通讯录中当前人员个数abs.m_Size = 0; int select = 0; //创建用户输入选择变量while (true) {showMenu(); cin >> select; switch (select){case 1: //1.添加联系人addPerson(&abs); break; case 2: //2.显示联系人showPerson(&abs); break; case 3: //3.删除联系人deletePerson(&abs); break; case 4: //4.查找联系人selectPerson(&abs); break; case 5: //5.修改联系人editPerson(&abs); break; case 6: //6.清空联系人cleanPerson(&abs); break; case 0: //0.退出通讯录 cout << "欢迎下次使用" << endl; system("pause"); return 0; break; default:break; }}}
三、实现增删改查和清空通讯录功能 实现具体的通讯录增删改查功能:
完整源码如下:
//封装函数显示通讯录界面 void showMenu()//在main中调用封装好的函数#include#includeusing namespace std; #define MAX 1000 //最大人数//联系人结构体struct Person{string m_Name; //姓名int m_Sex; //1-男,2-女int m_Age; //年龄string m_Phone; //电话string m_Afddar; //通讯地址}; struct AddaressBooks{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; }//1.添加联系人void addPerson(AddaressBooks* 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) {// 如果输入1或者2退出循环,如果有误,则重新输入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 addres; cout << "请输入家庭住址:" << endl; cin >> addres; abs->personArray[abs->m_Size].m_Afddar = addres; //更新通讯录人数abs->m_Size++; cout << "添加成功!" << endl; system("pause"); //请按任意键继续system("cls"); //清屏}}//显示联系人void showPerson(AddaressBooks* abs) {//判断通讯录中记录是否为0,如果不为0进行显示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_Afddar << endl; }}system("pause"); //请按任意键继续system("cls"); //清屏}//检测联系人是否存在,如果存在返回联系人所在数组的位置,不存在返回-1int isExist(AddaressBooks* abs,string name) {for (int i = 0; i < abs->m_Size; i++) {if (abs->personArray[i].m_Name == name) {return i; }}//没有找到返回-1return -1; }//删除联系人void deletePerson(AddaressBooks* abs) {cout << "请输入要删除的联系人姓名:" << endl; string name; cin >> name; int ret = isExist(abs, name); if (ret != -1) {//找到此人,进行删除操作for (int i = 0; i < abs->m_Size; i++) {//数据前移abs->personArray[i] = abs->personArray[i + 1]; }abs->m_Size--; //更新通讯录中数量cout << "删除成功!" << endl; }else {cout << "查无此人!" << endl; }system("pause"); //请按任意键继续system("cls"); //清屏}//查找联系人void selectPerson(AddaressBooks* 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_Afddar << endl; }else {cout << "查无此人!" << endl; }system("pause"); //请按任意键继续system("cls"); //清屏}//修改联系人void editPerson(AddaressBooks* 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) {// 如果输入1或者2退出循环,如果有误,则重新输入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 addres; cout << "请输入家庭住址:" << endl; cin >> addres; abs->personArray[ret].m_Afddar = addres; cout << "修改成功!" << endl; }else {cout << "查无此人!" << endl; }system("pause"); //请按任意键继续system("cls"); //清屏}//清空联系人void cleanPerson(AddaressBooks* abs) {abs->m_Size = 0; //当前记录的联系人数量置为0,做逻辑上清空cout << "通讯录已清空!:" << endl; system("pause"); //请按任意键继续system("cls"); //清屏}int main() {//创建通讯录结构体变量AddaressBooks abs; //初始化通讯录中当前人员个数abs.m_Size = 0; int select = 0; //创建用户输入选择变量while (true) {showMenu(); cin >> select; switch (select){case 1: //1.添加联系人addPerson(&abs); break; case 2: //2.显示联系人showPerson(&abs); break; case 3: //3.删除联系人deletePerson(&abs); break; case 4: //4.查找联系人selectPerson(&abs); break; case 5: //5.修改联系人editPerson(&abs); break; case 6: //6.清空联系人cleanPerson(&abs); break; case 0: //0.退出通讯录 cout << "欢迎下次使用" << endl; system("pause"); return 0; break; default:break; }}}
四、运行截图展示
文章图片
文章图片
文章图片
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- C++实现通讯录管理系统设计
- C++|走进Boost
- 基于OpenCV单目相机的快速标定--源码、工程、实现过程
- Spring|Spring Security权限管理实现接口动态权限控制
- C++类与对象深入之静态成员与友元及内部类详解
- C语言实现电器销售管理系统
- Kubernetes实现CI与CD配置教程
- C++实现简单版通讯录管理系统
- javascript实现悬浮跟随框缓动效果
- 基于JS实现动态跟随特效的示例代码