C++通讯录
对象new不出来 人气:0一、确定结构体
通讯录里应该存有联系人的信息,包括姓名、性别、电话、地址等等,通讯录也应该有长度,存的联系人要有上限。所以我们这样确定结构体:
#define Max 1000 struct person { string m_Name; string m_Sex;//规定 1 为男 2为女 int m_age; string m_phone; string m_Address; }; struct addressBooks { struct person personArray[Max];//通讯录中保存的联系人数组 int m_size = 0;//通讯录中人员个数 };
结构体 addressBooks 中定义联系人数组最大为1000,同时初始化人联系人为0。还有一点值得注意,被嵌套的结构体person 需要在 addressBooks前创建,避免出现未定义的情况。
二、简易菜单
要做通讯录管理系统,就要首先确定系统的功能。所以我确定了通讯录的增、删、改、查、显示、清空和退出 七个功能,代码上简单编写一个无返回值(void)的函数即可。
void showMenu()//菜单功能 { cout << "\t*************************" << endl; cout << "\t***** 1、添加联系人 *****" << endl; cout << "\t***** 2、显示联系人 *****" << endl; cout << "\t***** 3、查找联系人 *****" << endl; cout << "\t***** 4、修改联系人 *****" << endl; cout << "\t***** 5、删除联系人 *****" << endl; cout << "\t***** 6、清空通讯录 *****" << endl; cout << "\t***** 0、退出通讯录 *****" << endl; cout << "\t*************************" << endl; }
tips:这里“\t”会让光标向后跳8个字符的位置,可以理解为将菜单“居中”显示,稍微美观一点。
三、为通讯录添加功能
代码展示:
//主函数完整代码 int main() { //创建通讯录结构体变量 addressBooks abc; while (1) { showMenu(); int select = 0; cout << "请选择你的操作:"; cin >> select; switch (select) { case 1://添加联系人 addPerson(&abc); break; case 2://显示联系人 showPerson(&abc); break; case 3://查找联系人 { findPerson(&abc); } break; case 4://修改联系人 modifyPerson(&abc); break; case 5://删除联系人 { deletePerson(&abc); }//case 语句 里的代码多的话就用{}括起来,不报错 break; case 6://清空通讯录 clearPerson(&abc); break; case 0://退出通讯录 cout << "欢迎下次使用,祝您生活愉快" << endl; return 0; break; default: cout << "请合理输入操作数0~6:" << endl; cin >> select; break; } } }
首先创建通讯录结构体变量,while(1) 是个死循环,除非我们选择“0”功能,运行return 0 语句,否则不会结束循环,这也是我们可以重复选择功能的根本原因。流程控制语句switch case 语句无需多讲,只要注意每段语句结束加上break就行了。然后注意这段代码都是采用地址传递的方法,其实我更喜欢引用传递,不过是当时不太了解引用传递,关于函数传参的区别可以参考博主的这篇文章详解函数传参的三种方式
四、各功能与实现详解
功能之添加联系人
void addPerson(addressBooks* abc)//添加联系人 { if (abc->m_size >= Max) { cout << "通讯录已满,添加失败" << endl; } else { string name; cout << "添加联系人名字:" << endl; cin >> name; abc->personArray[abc->m_size].m_Name = name; int sex = 0; cout << "联系人性别为:" << endl; cout << "1---男\n" << "2---女\n"; cin >> sex; while (1) { if (sex == 1 || sex == 2) { if (sex == 1) abc->personArray[abc->m_size].m_Sex = "男"; else abc->personArray[abc->m_size].m_Sex = "女"; break; } else cout << "输入有误,请在数字 1和2中选择" << endl; cin >> sex; } int age = 0; cout << "联系人年龄为:" << endl; cin >> age; abc->personArray[abc->m_size].m_age = age; string phoneNumber = "0"; cout << "联系人电话:" << endl; cin >> phoneNumber; abc->personArray[abc->m_size].m_phone = phoneNumber; string address = "0"; cout << "联系人地址为:" << endl; cin >> address; abc->personArray[abc->m_size].m_Address = address; cout << "添加成功" << endl; //更新通讯录人数 abc->m_size++; system("pause"); system("cls"); } }
代码详解
首先判断通讯录当前联系人数量,如果大于最大值停止添加联系人;然后作一个输入流来输入联系人信息,利用联系人数组填入联系人信息;姓名属性我们简单做了一个范围选择,只允许输入1和0并给出提示1代表男性;倒数第二行 abc->m_size++,更新联系人数量;最后有两个系统函数 system("pause") 和 system("cls"),分别时“按任意键继续...”和清空屏幕函数,让我们的通讯录更加稳定和美观。
功能之显示联系人
void showPerson(addressBooks* abc) { if (abc->m_size == 0) cout << "当前记录为空" << endl; else { for (int i = 0; i < abc->m_size; i++) { cout << "联系人:\t" << abc->personArray[i].m_Name << "\t性别: " << abc->personArray[i].m_Sex<< "\t年龄: " << abc->personArray[i].m_age << "\t电话: " << abc->personArray[i].m_phone << "\t地址: " << abc->personArray[i].m_Address << endl; } } system("pause"); system("cls"); }
代码详解
首先判断有无联系人,没有就输出“当前记录为空”,然后利用一重for 循环输出所有联系人的信息,最后也是加上暂停函数和清空屏幕函数使运行界面美观。
功能之查找联系人
判定函数和实现查找
int isExit(addressBooks* abc, string name)//判定通讯录是否有此人 { for (int i = 0; i < abc->m_size; i++) { if (abc->personArray[i].m_Name == name) return i; } return -1; } void findPerson(addressBooks* abc) { cout << "输入要查找联系人的名字:" << endl; string name = "0"; cin >> name; int i = isExit(abc, name); if (i != -1) { cout << "联系人:\t" << abc->personArray[i].m_Name << "\t性别: " << abc->personArray[i].m_Sex << "\t年龄: " << abc->personArray[i].m_age << "\t电话: " << abc->personArray[i].m_phone << "\t地址: " << abc->personArray[i].m_Address << endl; } else { cout << "查无此人" << endl; } system("pause"); system("cls"); }
代码详解
isExit()函数用来返回查找联系人在数组中的下标,如果没有该名字就返回 -1;结合findPerson() 函数,如果找到该联系人则输出所有信息,找不到则输出“查无此人”,最后也是老套路,使用暂停和清空屏幕函数。
功能之修改联系人
void modifyPerson(addressBooks* abc) { cout << "输入要修改的联系人名字" << endl; string name = "0"; cin >> name; int v = isExit(abc, name); if (v != -1) { string name; cout << "更改后的名字为:" << endl; cin >> name; abc->personArray[v].m_Name = name; int sex = 0; cout << "更改后性别为:" << endl; cout << "1---男\n" << "2---女\n"; cin >> sex; while (1) { if (sex == 1 || sex == 2) { if (sex == 1) abc->personArray[v].m_Sex = "男"; else abc->personArray[v].m_Sex = "女"; break; } else cout << "输入有误,请在数字 1和2中选择" << endl; cin >> sex; } int age = 0; cout << "更改后年龄为:" << endl; cin >> age; abc->personArray[v].m_age = age; string phoneNumber = "0"; cout << "更改后电话号码:" << endl; cin >> phoneNumber; abc->personArray[v].m_phone = phoneNumber; string address = "0"; cout << "更改后地址为:" << endl; cin >> address; abc->personArray[v].m_Address = address; cout << "更改成功" << endl; } else { cout << "查无此人" << endl; } system("pause"); system("cls"); }
代码详解
这个功能函数其实很好理解,无非就是判定函数和添加联系人功能的结合,这里就不做详细介绍了。
功能之删除联系人
void deletePerson(addressBooks* abc) { if (abc->m_size == 0) { cout << "当前记录为空" << endl; } else { cout << "输入要删除的联系人:" << endl; string name = "0"; cin >> name; int v = isExit(abc, name); if (v == -1) { cout << "查无此人" << endl; } else if (v != -1) { for (int i = v; i < abc->m_size; i++) { abc->personArray[i] = abc->personArray[i + 1]; } abc->m_size--; cout << "删除成功" << endl; } } system("pause"); system("cls"); }
代码详解
首先判断有无联系人,没有就输出“当前记录为空”,再利用判断函数判断是否有此人,如果存在,那我们也是利用一重循环,让 i 等于返回的数组下标,条件是 i < abc->m_size,注意最好不要写"<=",因为我们需要将数组中的元素前移,只需要abc->m_size - 1 个长度就行了。最后将整个联系人数量减一,完成删除操作。
功能之清空通讯录
void clearPerson(addressBooks* abc) { abc->m_size = 0; cout << "通讯录已清空" << endl; system("pause"); system("cls"); }
将联系人数组置为零,即可清空通讯录。
四、源码
//Txl.h #include<iostream> using namespace std; #define Max 1000 struct person { string m_Name; string m_Sex;//规定 1 为男 2为女 int m_age; string m_phone; string m_Address; }; struct addressBooks { struct person personArray[Max];//通讯录中保存的联系人数组 int m_size = 0;//通讯录中人员个数 }; void addPerson(addressBooks* abc); //添加联系人 void showPerson(addressBooks* abc); //显示联系人 int isExit(addressBooks* abc, string name); //遍历通讯录 void deletePerson(addressBooks* abc); //删除联系人 void findPerson(addressBooks* abc); //查找联系人 void modifyPerson(addressBooks* abc); //修改联系人 void clearPerson(addressBooks* abc); //清空通讯录 //Txl.cpp #include"Txl.h" void addPerson(addressBooks* abc)//添加联系人 { if (abc->m_size >= Max) { cout << "通讯录已满,添加失败" << endl; } else { string name; cout << "添加联系人名字:" << endl; cin >> name; abc->personArray[abc->m_size].m_Name = name; int sex = 0; cout << "联系人性别为:" << endl; cout << "1---男\n" << "2---女\n"; cin >> sex; while (1) { if (sex == 1 || sex == 2) { if (sex == 1) abc->personArray[abc->m_size].m_Sex = "男"; else abc->personArray[abc->m_size].m_Sex = "女"; break; } else cout << "输入有误,请在数字 1和2中选择" << endl; cin >> sex; } int age = 0; cout << "联系人年龄为:" << endl; cin >> age; abc->personArray[abc->m_size].m_age = age; string phoneNumber = "0"; cout << "联系人电话:" << endl; cin >> phoneNumber; abc->personArray[abc->m_size].m_phone = phoneNumber; string address = "0"; cout << "联系人地址为:" << endl; cin >> address; abc->personArray[abc->m_size].m_Address = address; cout << "添加成功" << endl; //更新通讯录人数 abc->m_size++; system("pause"); system("cls"); } } void showPerson(addressBooks* abc) { if (abc->m_size == 0) cout << "当前记录为空" << endl; else { for (int i = 0; i < abc->m_size; i++) { cout << "联系人:\t" << abc->personArray[i].m_Name << "\t性别: " << abc->personArray[i].m_Sex<< "\t年龄: " << abc->personArray[i].m_age << "\t电话: " << abc->personArray[i].m_phone << "\t地址: " << abc->personArray[i].m_Address << endl; } } system("pause"); system("cls"); } int isExit(addressBooks* abc, string name)//判定通讯录是否有此人 { for (int i = 0; i < abc->m_size; i++) { if (abc->personArray[i].m_Name == name) return i; } return -1; } void findPerson(addressBooks* abc) { cout << "输入要查找联系人的名字:" << endl; string name = "0"; cin >> name; int i = isExit(abc, name); if (i != -1) { cout << "联系人:\t" << abc->personArray[i].m_Name << "\t性别: " << abc->personArray[i].m_Sex << "\t年龄: " << abc->personArray[i].m_age << "\t电话: " << abc->personArray[i].m_phone << "\t地址: " << abc->personArray[i].m_Address << endl; } else { cout << "查无此人" << endl; } system("pause"); system("cls"); } void deletePerson(addressBooks* abc) { if (abc->m_size == 0) { cout << "当前记录为空" << endl; } else { cout << "输入要删除的联系人:" << endl; string name = "0"; cin >> name; int v = isExit(abc, name); if (v == -1) { cout << "查无此人" << endl; } else if (v != -1) { for (int i = v; i < abc->m_size; i++) { abc->personArray[i] = abc->personArray[i + 1]; } abc->m_size--; cout << "删除成功" << endl; } } system("pause"); system("cls"); } void modifyPerson(addressBooks* abc) { cout << "输入要修改的联系人名字" << endl; string name = "0"; cin >> name; int v = isExit(abc, name); if (v != -1) { string name; cout << "更改后的名字为:" << endl; cin >> name; abc->personArray[v].m_Name = name; int sex = 0; cout << "更改后性别为:" << endl; cout << "1---男\n" << "2---女\n"; cin >> sex; while (1) { if (sex == 1 || sex == 2) { if (sex == 1) abc->personArray[v].m_Sex = "男"; else abc->personArray[v].m_Sex = "女"; break; } else cout << "输入有误,请在数字 1和2中选择" << endl; cin >> sex; } int age = 0; cout << "更改后年龄为:" << endl; cin >> age; abc->personArray[v].m_age = age; string phoneNumber = "0"; cout << "更改后电话号码:" << endl; cin >> phoneNumber; abc->personArray[v].m_phone = phoneNumber; string address = "0"; cout << "更改后地址为:" << endl; cin >> address; abc->personArray[v].m_Address = address; cout << "更改成功" << endl; } else { cout << "查无此人" << endl; } system("pause"); system("cls"); } void clearPerson(addressBooks* abc) { abc->m_size = 0; cout << "通讯录已清空" << endl; system("pause"); system("cls"); } //函数声明,可加可不加,这里加上为了更直观的表现出来 void addPerson(addressBooks* abc); //添加联系人 void showPerson(addressBooks* abc); //显示联系人 int isExit(addressBooks* abc, string name); //遍历通讯录 void deletePerson(addressBooks* abc); //删除联系人 void findPerson(addressBooks* abc); //查找联系人 void modifyPerson(addressBooks* abc); //修改联系人 void clearPerson(addressBooks* abc); //清空通讯录
五、运行效果
运行效果图
还有更多功能就不展示了,你们可以复制源码后自己操作,挺有趣的喔
生成可执行程序
我们在编译器的上面选择Release,并启动调试,然后这个项目对应的文件夹会自动生成Release文件夹,点开就是第二个图的内容,然后双击就可以执行了,实测分享给朋友也可以用哦
结语
加载全部内容