#include<iostream> using namespace std; #define maxlength 1000 //定义联系人结构体 struct user { string uname;//姓名 string usex;//性别 int uage;//年龄 string utelephone;//联系电话 string uaddres;//家庭住址 int delete_sign = 1;//删除标识符(1存在0不存在) }; //输入函数(添加联系人) void cinUser(struct user userArr[]) { //添加联系人 int number = 0; //数组的下标(自定义) int addsign = 0;//添加标志 int age_number = 1; if (number < maxlength) { cout << "欢迎您来到添加页面" << endl; while (1) { cout << "请输入联系人的姓名:" << endl; cin >> userArr[number].uname; cout << "请输入联系人的性别(woman/man):" << endl; cin >> userArr[number].usex; cout << "请输入联系人的年龄:" << endl; cin >> userArr[number].uage; while (age_number) { if (userArr[number].uage < 0) { cout << "您输入的年龄错误,请您重新输入:" << endl; cin >> userArr[number].uage; } else { age_number = 0; } } cout << "请输入联系人的联系电话:(11位,都是数字)" << endl; cin >> userArr[number].utelephone; cout << "请输入联系人的家庭住址:" << endl; cin >> userArr[number].uaddres; cout << "如果您想要继续添加,选择1,否则选择0" << endl; cin >> addsign; if (addsign == 0) { return ; } number++; } } } //输出函数(显示联系人) void printUser(struct user userArr[], int len) { int printSign = 0;//输出的标志,如果没有一个联系人,输出空 for (int i = 0; i < len; i++) { if (userArr[i].delete_sign == 1 && userArr[i].uage > 0) {//用年龄来防止输出未增加联系人的地方 printSign++; cout << "联系人:\n\t姓名:" << userArr[i].uname << endl; cout << "\t性别:" << userArr[i].usex << endl; cout << "\t年龄:" << userArr[i].uage << endl; cout << "\t联系电话:" << userArr[i].utelephone << endl; cout << "\t家庭住址:" << userArr[i].uaddres << endl; } } if (printSign == 0) { cout << "您的联系人是空的" << endl; } else { cout << "您的联系人已经输出完毕" << endl; } } //删除函数 void deleteUser(struct user userArr[], int len, string deletename) { for (int i = 0; i < len; i++) { //char类型用strcmp,string类型用compare. int number = userArr[i].uname.compare(deletename); if (number == 0) { userArr[i].delete_sign = 0; cout << "您要删除的联系人已经删除" << endl; return; } } cout << "您要删除的联系人不存在" << endl; } //查找函数 void findUser(struct user userArr[], int len, string deletename) { for (int i = 0; i < len; i++) { //char类型用strcmp,string类型用compare. int number = userArr[i].uname.compare(deletename); if (number == 0 && userArr[i].delete_sign == 1) { cout << "联系人: " << userArr[i].uname << endl; cout << "\t性别:" << userArr[i].usex << endl; cout << "\t年龄:" << userArr[i].uage << endl; cout << "\t联系电话:" << userArr[i].utelephone << endl; cout << "\t家庭住址:" << userArr[i].uaddres << endl; return; } } cout << "您查找的联系人不存在" << endl; } //修改函数 void reviseUser(struct user userArr[], int len, string revisename) { int age_number = 1; for (int i = 0; i < len; i++) { int alinksign = userArr[i].uname.compare(revisename);//相同标志 if ( alinksign== 0) { cout << "欢迎您来到修改页面" << endl; cout << "请输入联系人的姓名:" << endl; cin >> userArr[i].uname; cout << "请输入联系人的性别(woman/man):" << endl; cin >> userArr[i].usex; cout << "请输入联系人的年龄:" << endl; cin >> userArr[i].uage; while (age_number) { if (userArr[i].uage < 0) { cout << "您输入的年龄错误,请您重新输入:" << endl; cin >> userArr[i].uage; } else { age_number = 0; } } cout << "请输入联系人的联系电话:(11位,都是数字)" << endl; cin >> userArr[i].utelephone; cout << "请输入联系人的家庭住址:" << endl; cin >> userArr[i].uaddres; return; } } cout << "您修改的联系人不存在" << endl; } //清空联系人 void deleteNullUser(struct user userArr[], int len) { for (int i = 0; i < len; i++) { userArr[i].delete_sign = 0; } cout << "联系人已经清空" << endl; } //退出通讯录 int quitUser() { cout << "欢迎您的下次使用" << endl; return 0; } //目录 void allUser() { cout << "*********************" << endl; cout << "*********目录********" << endl; cout << "*****1.添加联系人****" << endl; cout << "*****2.显示联系人****" << endl; cout << "*****3.删除联系人****" << endl; cout << "*****4.查找联系人****" << endl; cout << "*****5.修改联系人****" << endl; cout << "*****6.清空联系人****" << endl; cout << "*****7.退出通讯录****" << endl; cout << "*********************" << endl; } int main() { struct user userArr[2]; int len = sizeof(userArr)/sizeof(userArr[0]); int circlesign = 1;//循环标志 while (circlesign) { int choose = 0;//选择跳转标志 allUser();//目录 cout << "请您输入您的选择:(请输入选择前的数字)" << endl; cin >> choose; switch (choose) { case 1: { //定义一个输入数组(添加) cinUser(userArr); break; } case 2: { //显示通讯录中所有人的信息 cout << len << endl; printUser(userArr, len); break; } case 3: { //删除联系人 string deletename; cout << "请您输入您要删除的联系人的姓名:\n"; cin >> deletename; deleteUser(userArr, len, deletename); break; } case 4: { //查找联系人 string findname; cout << "请您输入您要查找的联系人的姓名:\n"; cin >> findname; findUser(userArr, len, findname); break; } case 5: { //修改联系人 string revisename; cout << "请您输入您要修改的联系人的姓名:\n"; cin >> revisename; reviseUser(userArr, len, revisename); break; } case 6: { //清空联系人 deleteNullUser(userArr, len); break; } case 7: { //退出通讯录 quitUser(); return 0; break; } default: cout << "您输入的选项不存在" << endl; } } }