这篇文章主要介绍“c++如何实现通讯录小功能”,在日常操作中,相信很多人在C++如何实现通讯录小功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++如何实现通讯录小功能”的疑惑有所帮助!接下来,请跟着小编
这篇文章主要介绍“c++如何实现通讯录小功能”,在日常操作中,相信很多人在C++如何实现通讯录小功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++如何实现通讯录小功能”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
1.显示菜单栏
void menu() { 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() { int select = 0; cin >> select; switch (select) { case 0://退出通讯录 cout << "欢迎下次使用" << endl; system("pause"); return 0; break; } system("pause"); return 0;}
3.创建结构体
1创建联系人结构体
3.2创建通讯录结构体
//定义联系人结构体//姓名、电话号码、邮箱、地址struct person { string name; string number; string Email; string address;};// 定义通讯录结构体struct contacts { int people_num; struct person personarr[MAX];//子结构体:联系人信息};
添加联系人
void addperson(struct contacts* p) { if (p->people_num == MAX ) { cout << "通讯录已满" << endl; } else {//添加联系人信息 string name, number, Email, address; cout << "请输入姓名:" << endl; cin >> name; cout << "请输入电话:" << endl; cin >> number; cout << "请输入邮箱:" << endl; cin >> Email; cout << "请输入地址:" << endl; cin >> address; p->personarr[p->people_num].name = name; p->personarr[p->people_num].number = number; p->personarr[p->people_num].Email = Email; p->personarr[p->people_num].address = address; p->people_num++; cout << "添加成功!" << endl; }}
5.删除联系人
判断联系人是否存在
int existperson(struct contacts* p,string name) { for (int i = 0; i < p->people_num; i++) { if ( p->personarr[i].name == name ) { return i; } } return -1;}
若存在,获取联系人在通讯录位置,将position后面的都往前移动一个位置,覆盖之前的值
//删除联系人void delperson(struct contacts* p,int position) { while (position < (p->people_num)) { p->personarr[position] = p->personarr[position + 1]; position++; } p->people_num--; cout << "删除成功!" << endl;}
6.修改
检查要修改联系人是否存在,并获取当前位置
void modifyperson(struct contacts* p, int position) { string number, Email, address; cout << "请输入修改电话:" << endl; cin >> number; cout << "请输入修改邮箱:" << endl; cin >> Email; cout << "请输入修改地址:" << endl; cin >> address; p->personarr[position].number = number; p->personarr[position].Email = Email; p->personarr[position].address = address; cout << "修改成功!" << endl;}
7.查找
void searchperson(struct contacts* p, int position) { cout << "姓名:" << p->personarr[position].name << " " << "电话:" << p->personarr[position].number << " " << "邮箱:" << p->personarr[position].Email << " " << "地址:" << p->personarr[position].address << " ";}
8.显示通讯录
void showcontact(struct contacts* p) { for (int i = 0; i < (p->people_num); i++) { cout <<"姓名:" << p->personarr[i].name << " " <<"电话:" << p->personarr[i].number << " " <<"邮箱:" << p->personarr[i].Email << " " <<"地址:" << p->personarr[i].address << " "<< endl; } }
9.清空通讯录
void cleancontact(struct contacts* p) { p->people_num = 0; cout << "已清空!" << endl;}
#include<iOStream>using namespace std;#define MAX 200 //1.菜单栏显示void menu() { 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.定义结构体//2.1定义联系人结构体//姓名、电话号码、邮箱、地址struct person { string name; string number; string Email; string address;};//2.2 定义通讯录结构体struct contacts { int people_num; struct person personarr[MAX];//子结构体:联系人信息}; //3.添加联系人void addperson(struct contacts* p) { if (p->people_num == MAX ) { cout << "通讯录已满" << endl; } else {//添加联系人信息 string name, number, Email, address; cout << "请输入姓名:" << endl; cin >> name; cout << "请输入电话:" << endl; cin >> number; cout << "请输入邮箱:" << endl; cin >> Email; cout << "请输入地址:" << endl; cin >> address; p->personarr[p->people_num].name = name; p->personarr[p->people_num].number = number; p->personarr[p->people_num].Email = Email; p->personarr[p->people_num].address = address; p->people_num++; cout << "添加成功!" << endl; }}//4.删除联系人//4.1检测联系人是否存在int existperson(struct contacts* p,string name) { for (int i = 0; i < p->people_num; i++) { if ( p->personarr[i].name == name ) { return i; } } return -1;}//删除联系人void delperson(struct contacts* p,int position) { while (position < (p->people_num)) { p->personarr[position] = p->personarr[position + 1]; position++; } p->people_num--; cout << "删除成功!" << endl;}//5.修改联系人void modifyperson(struct contacts* p, int position) { string number, Email, address; cout << "请输入修改电话:" << endl; cin >> number; cout << "请输入修改邮箱:" << endl; cin >> Email; cout << "请输入修改地址:" << endl; cin >> address; p->personarr[position].number = number; p->personarr[position].Email = Email; p->personarr[position].address = address; cout << "修改成功!" << endl;} //6.查找联系人void searchperson(struct contacts* p, int position) { cout << "姓名:" << p->personarr[position].name << " " << "电话:" << p->personarr[position].number << " " << "邮箱:" << p->personarr[position].Email << " " << "地址:" << p->personarr[position].address << " ";} //7.显示通讯录void showcontact(struct contacts* p) { for (int i = 0; i < (p->people_num); i++) { cout <<"姓名:" << p->personarr[i].name << " " <<"电话:" << p->personarr[i].number << " " <<"邮箱:" << p->personarr[i].Email << " " <<"地址:" << p->personarr[i].address << " "<< endl; } }//8.清空联系人void cleancontact(struct contacts* p) { p->people_num = 0; cout << "已清空!" << endl;} int main() { //创建通讯录结构体变量 struct contacts c; //初始化通讯录当前联系人个数 c.people_num = 0; int select = 0; string name; while (1) { menu(); cin >> select; switch (select) { case 1:// 添加联系人 addperson(&c); system("pause"); break; case 2:// 删除联系人 cout << "请输入删除联系人的姓名:"; cin >> name; if (existperson(&c,name)==-1) { cout << "该联系人不存在!"; } else{ delperson(&c, existperson(&c, name)); } system("pause"); break; case 3:// 修改联系人 cout << "请输入要修改联系人的姓名:"; cin >> name; if (existperson(&c,name) == -1) { cout << "该联系人不存在!"; } else { modifyperson(&c, existperson(&c, name)); } system("pause"); break; case 4:// 查找联系人 cout << "请输入查找联系人的姓名:"; cin >> name; if (existperson(&c,name) == -1) { cout << "该联系人不存在!"; } else { searchperson(&c, existperson(&c, name)); } system("pause"); break; case 5:// 显示通讯录 showcontact(&c); system("pause"); break; case 6:// 清空联系人 cleancontact(&c); system("pause"); break; case 0://退出通讯录 cout << "欢迎下次使用" << endl; system("pause");//暂停批文件的处理 return 0; break; } system("cls");//清屏 } system("pause"); return 0;}
到此,关于“C++如何实现通讯录小功能”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
--结束END--
本文标题: C++如何实现通讯录小功能
本文链接: https://www.lsjlt.com/news/341501.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0