![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2e94f46b0354752853329536284d7047.png)
代码
#include <iostream>
#include <string>
using namespace std;
#define Max 1000
struct Person {
string m_name;
int m_sex;
int m_age;
string m_phone;
string m_addr;
};
struct Addrbooks {
Person personArr[Max];
int m_size;
};
void addPerson(Addrbooks* abs) {
if (abs->m_size == Max) {
cout << "通讯录满了" << endl;
return;
}
else {
string name;
cout << "请输入姓名: " << endl;
cin >> name;
abs->personArr[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->personArr[abs->m_size].m_sex = sex;
break;
}
}
int age;
cout << "请输入年龄:" << endl;
cin >> age;
abs->personArr[abs->m_size].m_age = age;
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
abs->personArr[abs->m_size].m_phone = phone;
string addr;
cout << "请输入地址:" << endl;
cin >> addr;
abs->personArr[abs->m_size].m_addr = addr;
abs->m_size++;
cout << "添加成功" << endl;
}
system("pause");
system("cls");
}
void showPerson(Addrbooks* abs) {
if (abs->m_size == 0) {
cout << "当前记录为空" << endl;
}
else {
for (int i = 0; i < abs->m_size; i++) {
cout << " 姓名:" << abs->personArr[i].m_name;
cout << " 性别:" << (abs->personArr[i].m_sex == 1 ? "男" : "女");
cout << " 年龄:" << abs->personArr[i].m_age;
cout << " 电话:" << abs->personArr[i].m_phone;
cout << " 地址:" << abs->personArr[i].m_addr << endl;
}
}
system("pause");
system("cls");
}
void delectPerson(Addrbooks* abs) {
string name;
int number = -1;
cout << "请输入姓名:" << endl;
cin >> name;
for (int i = 0; i < abs->m_size; i++) {
if (abs->personArr[i].m_name == name) {
number = i;
}
}
if (number == -1) {
cout << "查无此人" << endl;
}
else {
if (number == (abs->m_size - 1)) {
abs->m_size--;
}
else {
for (int i = number; i < (abs->m_size - 1); i++) {
abs->personArr[i] = abs->personArr[i + 1];
}
abs->m_size--;
}
cout << "删除成功!" << endl;
}
system("pause");
system("cls");
}
void findPerson(Addrbooks* abs) {
string name;
int number = -1;
cout << "请输入姓名:" << endl;
cin >> name;
for (int i = 0; i < abs->m_size; i++) {
if (abs->personArr[i].m_name == name) {
number = i;
}
}
if (number == -1) {
cout << "查无此人" << endl;
}
else {
cout << " 姓名:" << abs->personArr[number].m_name;
cout << " 性别:" << (abs->personArr[number].m_sex == 1 ? "男" : "女");
cout << " 年龄:" << abs->personArr[number].m_age;
cout << " 电话:" << abs->personArr[number].m_phone;
cout << " 地址:" << abs->personArr[number].m_addr << endl;
}
system("pause");
system("cls");
}
void modifyPerson(Addrbooks* abs) {
string name;
int number = -1;
cout << "请输入姓名:" << endl;
cin >> name;
for (int i = 0; i < abs->m_size; i++) {
if (abs->personArr[i].m_name == name) {
number = i;
}
}
if (number == -1) {
cout << "查无此人" << endl;
}
else {
string name;
cout << "请输入姓名: " << endl;
cin >> name;
abs->personArr[number].m_name = name;
int sex = 0;
cout << "请输入性别:" << endl;
cout << "1--男" << endl;
cout << "2--女" << endl;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2) {
abs->personArr[number].m_sex = sex;
break;
}
}
int age;
cout << "请输入年龄:" << endl;
cin >> age;
abs->personArr[number].m_age = age;
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
abs->personArr[number].m_phone = phone;
string addr;
cout << "请输入地址:" << endl;
cin >> addr;
abs->personArr[number].m_addr = addr;
cout << "修改成功!" << endl;
}
system("pause");
system("cls");
}
void cleanPerson(Addrbooks* abs) {
int choice;
cout << "是否真的要清空" << endl;
cout << "1--是 \t0--否" << endl;
cout << "请输入你的决定:" << endl;
cin >> choice;
if (choice) {
abs->m_size = 0;
cout << "清空成功!" << endl;
}
else {
cout << "终止清空!" << endl;
}
system("pause");
system("cls");
}
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;
cout << "请输入您的操作:";
}
int main() {
Addrbooks abs;
abs.m_size = 0;
int select = 0;
while (1)
{
showMenu();
cin >> select;
switch (select)
{
case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
delectPerson(&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:
cout << "错误输入" << endl;
break;
}
}
system("pause");
return 0;
}