c++—通讯录管理系统
Posted whp12-cqu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++—通讯录管理系统相关的知识,希望对你有一定的参考价值。
一、运用所学的结构体、地址指针等基础知识,完成通讯录管理系统
二、系统主要有以下6个功能:
1.添加联系人
2.显示联系人
3.删除联系人
4.查找联系人
5.修改联系人
6.清空联系人
#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; cout << "输入有误,请重新输入!" << endl; //年龄 cout << "请输入年龄: " << endl; int age = 0; while (true) cin >> age; if (age<101) abs->personarray[abs->m_size].m_age = age; break; cout << "输入有误,请重新输入!" << endl; //电话 cout << "请输入电话: " << endl; string phone=""; cin >> phone; abs->personarray[abs->m_size].m_phone = phone; //住址 cout << "请输入家庭住址: " << endl; string place=""; cin >> place; abs->personarray[abs->m_size].m_Addr = place; //更新通讯录人数 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.1检测联系人是否存在 int Exist(Addressbooks* abs,string name)//通讯录,对比姓名 for (int i = 0; i < abs->m_size; i++) if (abs->personarray[i].m_Name == name) return i; return -1; //3.2删除联系人 void Delteperson(Addressbooks* abs) cout << "请输入您要删除的联系人" << endl; string name; cin >> name; int ret = Exist(abs, name); if (ret!=-1) for (int i = ret; i < abs->m_size; i++) //数据前移 abs->personarray[i] = abs->personarray[i + 1]; abs->m_size--;//更新人员数量 cout << "删除成功" << endl; else cout << "查无此人!" << endl; system("pause"); system("cls"); //4.查找联系人 void findperson(Addressbooks* abs) cout << "请输入需要查找的联系人" << endl; string name; cin >> name; //判断是否存在 int ret = Exist(abs, name); if (ret!=-1) int i = ret; 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; else cout << "查无此人!" << endl; system("pause"); system("cls"); //5.修改联系人 void modifyperson(Addressbooks* abs) cout << "请输入需要修改的联系人" << endl; string name; cin >> name; //判断是否存在 int ret = Exist(abs, name); if (ret != -1) int i= ret; //显示要修改人信息 cout << "---------------------------------------------" << endl; 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; //根据不同需求修改联系人信息 int n = 0; cout << "---------------------------------------------" << endl; cout << " 请选择需要修改的内容选项: " << endl; cout << "---------------------------------------------" << endl; cout << "1--姓名;2--性别;3--年龄;4--电话;5--住址;6--全部修改;7--退出" << endl; while (n!=7) cin >> n; string me = ""; int sex=0; int age = 0; string phone = ""; string place = ""; switch (n) case 1: //姓名 cout << "请输入姓名: " << endl; cin >> me; abs->personarray[i].m_Name = me; cout << "修改成功" << endl; cout << "如要修改请继续选择其他选项,退出请按7" << endl; break; case 2: //性别 cout << "请输入性别: " << endl; cout << "1 --- 男" << endl; cout << "2 --- 女" << endl; while (true) cin >> sex; if (sex == 1 || sex == 2) abs->personarray[i].m_sex = sex; break; cout << "输入有误,请重新输入!" << endl; cout << "修改成功" << endl; cout << "如要修改请继续选择其他选项,退出请按7" << endl; break; case 3: //年龄 cout << "请输入年龄: " << endl; cin >> age; abs->personarray[i].m_age = age; cout << "修改成功" << endl; cout << "如要修改请继续选择其他选项,退出请按7" << endl; break; case 4: //电话 cout << "请输入电话: " << endl; cin >> phone; abs->personarray[i].m_phone = phone; cout << "修改成功" << endl; cout << "如要修改请继续选择其他选项,退出请按7" << endl; break; case 5: //住址 cout << "请输入家庭住址: " << endl; cin >> place; abs->personarray[i].m_Addr = place; cout << "修改成功" << endl; cout << "如要修改请继续选择其他选项,退出请按7" << endl; break; case 6: //姓名 cout << "请输入姓名: " << endl; cin >> name; abs->personarray[i].m_Name=name; //性别 cout << "请输入性别: " << endl; cout << "1 --- 男" << endl; cout << "2 --- 女" << endl; while (true) cin >> sex; if (sex == 1 || sex == 2) abs->personarray[i].m_sex = sex; break; cout << "输入有误,请重新输入!" << endl; //年龄 cout << "请输入年龄: " << endl; cin >> age; abs->personarray[i].m_age = age; //电话 cout << "请输入电话: " << endl; cin >> phone; abs->personarray[i].m_phone = phone; //住址 cout << "请输入家庭住址: " << endl; cin >> place; abs->personarray[i].m_Addr = place; cout << "修改成功" << endl; n = 7; break; default: break; else cout << "查无此人!" << endl; system("pause"); system("cls"); //6、清空所有联系人 void cleanperson(Addressbooks* abs) cout << "请问是否真的要清空通讯录? " << endl; cout << "1--确定" << endl; cout << "0--不清空" << endl; int b; cin >> b; while (b==1) abs->m_size = 0; cout << "通讯录已清空" << endl; break; system("pause"); system("cls"); int main() //创建通讯录结构体变量 Addressbooks abs; //当前人员个数 abs.m_size = 0; int select = 0;//创建用户选择输入变量 while (true) //除了选0,能一直使用 //菜单调用 showMenu(); cin >> select; switch (select) case 1: //1.添加联系人 addPerson(&abs); //地址传递 break; case 2: //2.显示联系人 showperson(&abs); break; case 3: //3.删除联系人 Delteperson(&abs); break; case 4: //4.查找联系人 findperson(&abs); break; case 5: //5.修改联系人 modifyperson(&abs); break; case 6: //6.清空联系人 cleanperson(&abs); break; case 0: //0.退出通讯录 cout << "欢迎下次使用" << endl; system("pause"); return 0; break; default: break; system("pause"); return 0;
0.退出通讯录
以上是关于c++—通讯录管理系统的主要内容,如果未能解决你的问题,请参考以下文章