C++通讯录小功能
糯米团子鸭 人气:0思路:
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.创建结构体
3.1创建联系人结构体
3.2创建通讯录结构体
//定义联系人结构体 //姓名、电话号码、邮箱、地址 struct person { string name; string number; string Email; string address; }; // 定义通讯录结构体 struct contacts { int people_num; struct person personarr[MAX];//子结构体:联系人信息 };
4.添加联系人
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; }
加载全部内容