重点梳理:
利用地址传递,可以修改实参原值。如何删除数组中的数据,后续索引号数组元素往前移。如何清空整个数组:逻辑清空。有一个计数成员维护的结构体,直接将计数成员清零的方式。
#includeusing namespace std; #include #define MAX 1000 // 宏常量 struct Person { string name; int sex; int age; string phone; string address; }; struct AddressBooks { struct Person personArray[MAX]; int M_size; }; 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; } int isExist(AddressBooks abs, string name) { for (int i = 0; i < abs.M_size; i++) { if (abs.personArray[i].name == name) { cout << name << "存在。" << endl; cout << "姓名: " << abs.personArray[i].name << "t性别: " << (abs.personArray[i].sex == 1 ? "男" : "女") << "t年龄: " << abs.personArray[i].age << "t电话: " << abs.personArray[i].phone << "t地址: " << abs.personArray[i].address << endl; return i; // 若是找到,则返回索引号。 } } return -1; // 若无,则返回-1; } void addPerson(AddressBooks* abs) { string name; cout << "请输入姓名:"; cin >> name; int index = isExist(*abs, name); if (index == -1) { if (abs->M_size == MAX) { cout << "通讯录已满, 无法添加!" << endl; return; } abs->personArray[abs->M_size].name = name; int sex = 0; while (true) { cout << "1----男" << endl; cout << "2----女" << endl; cout << "请输入性别:"; cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[abs->M_size].sex = sex; break; } cout << "输入有误:请重新输入。" << endl; } int age = 0; while (true) { cout << "请输入年龄:"; cin >> age; if (age > 0 && age <= 120) { abs->personArray[abs->M_size].age = age; break; } cout << "输入有误:请重新输入。" << endl; } string phone; while (true) { //手机号码正则表达式。 if (true) { cout << "请输入电话号码:"; cin >> phone; abs->personArray[abs->M_size].phone = phone; break; } cout << "输入有误,请输入11位有效数字。" << endl; } string address; cout << "请输入家庭地址:"; cin >> address; abs->personArray[abs->M_size].address = address; // 别忘记更新通讯录个数。 abs->M_size++; cout << "添加成功" << endl; system("pause"); // 请按任意键继续。 system("cls"); // 清屏操作。 //showMenu(); } } void showPerson(AddressBooks abs) { if (abs.M_size == 0) { cout << "当前通讯录记录为空" << endl; } else { string sex; for (int i = 0; i < abs.M_size; i++) { //if (abs.personArray[i].sex == 1) //{ // sex = "男"; //} //else //{ // sex = "女"; //} //cout << "姓名: " << abs.personArray[i].name // << "t性别: " << sex // << "t年龄: " << abs.personArray[i].age // << "t电话: " << abs.personArray[i].phone // << "t地址: " << abs.personArray[i].address // << endl; cout << "姓名: " << abs.personArray[i].name << "t性别: " << (abs.personArray[i].sex == 1 ? "男" : "女") << "t年龄: " << abs.personArray[i].age << "t电话: " << abs.personArray[i].phone << "t地址: " << abs.personArray[i].address << endl; } } system("pause"); system("cls"); // 清屏 } void deletePerson(AddressBooks* abs) { cout << "请输入您要删除的联系人姓名:"; string name; cin >> name; int res = isExist(*abs, name); if (res==-1) { cout << "查无此人。" << endl; return; } // 重点二:如果删除数组中的数据。后续数组元素往前移。 for (int i = res; i < abs->M_size; i++) { abs->personArray[i] = abs->personArray[i + 1]; } // 更新数目。 abs->M_size--; cout << name << "删除成功。" << endl; system("pause"); system("cls"); } // 检测联系人是否存在。 void findPerson(AddressBooks abs) { string name; cout << "请输入你要查找的姓名:"; cin >> name; int res = isExist(abs, name); if (res == -1) { cout << "查无此人。" << endl; } system("pause"); system("cls"); } // 修改联系人信息 void modifyPerson(AddressBooks* abs) { cout << "请输入你要修改的联系人姓名:"; string name; cin >> name; int res = isExist(*abs, name); if (res == -1) { cout << "查无此人,是否新建联系人?Y?/N?"; string willAdd; cin >> willAdd; if (willAdd=="Y") { addPerson(abs); } return; } int num = 0; while (num != 6) { cout << "请输入要修改的选项:1、姓名;2、性别;3、年龄;4、电话;5、地址;6、修改完成" << endl; cin >> num; switch (num) { case 1: { cout << "当前姓名为:" << abs->personArray[res].name << "。姓名修改为:"; string name; cin >> name; abs->personArray[res].name = name; cout << "姓名已修改为:" << name << endl; break; } case 2: { cout << "性别修改为:1----男;2----女:"; int sex; cin >> sex; abs->personArray[res].sex = sex; cout << "性别修改为:" << (sex == 1 ? "男" : "女") << endl; break; } // ....case3, 4, 5 default: break; } //system("cls"); } } // 好多重复代码,做好合理重构一遍。 // 清空联系人 void CleanPerson(AddressBooks* abs) { // 重点三:如何清空? // 逻辑清空:直接将通讯录数目置为0。 abs->M_size = 0; cout << "通讯录已清空。" << endl; } int main() { int select = 0; // 创建用户选择输入的变量。 AddressBooks abs; abs.M_size = 0; while (true) { showMenu(); cout << "请输入你的操作选项:"; cin >> select; cout << "您输入的选项是:" << select << endl; switch (select) { case 1: // 添加 // 重点一:利用地址传递,可以修改实参原值。 addPerson(&abs); break; case 2: // 显示 // 没必要地址,因为不希望被改变。 showPerson(abs); break; case 3: // 删除 // 如果case中,break之前有很多语句。那么应该用{}括起来,否则编译报错。 { deletePerson(&abs); } break; case 4: // 查找 findPerson(abs); break; case 5: // 修改 modifyPerson(&abs); break; case 6: // 清空 CleanPerson(&abs); break; case 0: // 退出 cout << "欢迎下次使用" << endl; system("pause"); return 0; break; default: break; } } }



