代码如下喵~
`#include <bits/stdc++.h>
using namespace std;
#define MAX 1000 // 创建宏常量指最多1000
struct Person { // 设置联系人结构体
string name; // 姓名
int sex; // 性别
int age; // 年龄
int num; // 号码
string addr; // 住址
};
struct Address { // 定义地址结构体
Person people[MAX]; // 保存联系人的数组
int size; // 当前联系人个数
};
// 显示菜单
void menu() {
cout << endl;
cout << "*********************\n";
cout << "*** 1、添加联系人 ***\n";
cout << "*** 2、显示联系人 *** \n";
cout << "*** 3、删除联系人 ***\n";
cout << "*** 4、查找联系人 ***\n";
cout << "*** 5、修改联系人 ***\n";
cout << "*** 6、清空联系人 ***\n";
cout << "*** 0、退出通讯录 ***\n";
cout << "*********************\n";
}
// 添加联系人
void addPerson(Address* ads) {
if (ads->size == MAX) {
cout << "操作失败,原因:人数已满\n";
return;
string name;
cout << "请输入联系人姓名\n";
cin >> name;
ads->people[ads->size].name = name;
int sex = 0;
cout << "请输入联系人性别\n";
cout << "1. 男性\n";
cout << "2. 女性\n";
cout << "3. 无性\n";
cout << "4. 双性\n";
while (true) {
cin >> sex;
if (sex < 1 || sex > 4) {
cout << "错误:性别无效\n";
}
else {
ads->people[ads->size].sex = sex;
break;
}
}
int age;
cout << "请输入联系人的年龄\n";
while (true) {
cin >> age;
if (age <= 0) {
cout << "错误:年龄无效\n";
} else {
ads->people[ads->size].age = age;
break;
}
}
int num;
cout << "请输入联系人的号码\n";
cin >> num;
ads->people[ads->size].num = num;
string addr;
cout << "请输入联系人住址\n";
cin >> addr;
ads->people[ads->size].addr = addr;
ads->size++; // 更新当前通讯录联系人数
cout << "成功添加联系人\n";
}
// 显示联系人
void show(Address* ads) {
if (ads->size == 0) {
cout << "错误:没有联系人添加,将返回至菜单\n";
return;
}
for (int i = 0; i < ads->size; i++) {
cout << "姓名: " << ads->people[i].name << endl;
cout << "性别: " << ads->people[i].sex << endl;
cout << "年龄: " << ads->people[i].age << endl;
cout << "号码: " << ads->people[i].num << endl;
cout << "住址: " << ads->people[i].addr << endl;
}
}
// 删除联系人
void deletee(Address* ads) {
if (ads->size == 0) {
cout << "没有联系人可以删除。\n";
return;
}
string name;
cout << "请输入要删除的联系人姓名:";
cin >> name;
for (int i = 0; i < ads->size; i++) {
if (ads->people[i].name == name) {
for (int j = i; j < ads->size - 1; j++) {
ads->people[j] = ads->people[j + 1]; // 向前移动联系人
}
ads->size--; // 更新联系人数量
cout << "成功删除联系人:" << name << endl;
return;
}
}
cout << "未找到联系人:" << name << endl;
}
// 查找联系人
void find(Address* ads) {
if (ads->size == 0) {
cout << "没有联系人可以查找。\n";
return;
}
string name;
cout << "请输入要查找的联系人姓名:";
cin >> name;
for (int i = 0; i < ads->size; i++) {
if (ads->people[i].name == name) {
cout << "找到联系人:" << endl;
cout << "姓名: " << ads->people[i].name << endl;
cout << "性别: " << ads->people[i].sex << endl;
cout << "年龄: " << ads->people[i].age << endl;
cout << "号码: " << ads->people[i].num << endl;
cout << "住址: " << ads->people[i].addr << endl;
return;
}
}
cout << "未找到联系人:" << name << endl;
}
// 修改联系人
void change(Address* ads) {
if (ads->size == 0) {
cout << "没有联系人可以修改。\n";
return;
}
string name;
cout << "请输入要修改的联系人姓名:";
cin >> name;
for (int i = 0; i < ads->size; i++) {
if (ads->people[i].name == name) {
cout << "找到联系人,输入新的信息:\n";
cout << "新姓名:";
cin >> ads->people[i].name;
cout << "新性别:";
cin >> ads->people[i].sex;
cout << "新年龄:";
cin >> ads->people[i].age;
cout << "新号码:";
cin >> ads->people[i].num;
cout << "新住址:";
cin >> ads->people[i].addr;
cout << "成功修改联系人:" << name << endl;
return;
}
}
cout << "未找到联系人:" << name << endl;
}
// 清空联系人
void vod(Address* ads) {
ads->size = 0; // 重置联系人数量
cout << "已清空所有联系人。\n";
}
int main() {
Address ads; // 创建地址结构体实例
ads.size = 0; // 将当前联系人个数初始化为0
int a = 0;
while (true) {
menu(); // 调用菜单函数
cin >> a;
switch (a) {
case 1:
addPerson(&ads); // 用ads指针的地址传递函数指针
break;
case 2:
show(&ads);
break;
case 3:
deletee(&ads);
break;
case 4:
find(&ads);
break;
case 5:
change(&ads);
break;
case 6:
vod(&ads);
break;
case 0:
int z;
cout << "确定此操作吗,如确定,请按7,如返回请按任意键\n";
cin >> z;
if (z == 7) {
cout << "欢迎下次使用\n";
return 0; // 退出程序
} else {
continue; // 返回菜单
}
default:
cout << "无效的选项,请重新选择。\n";
break;
}
}
}
喵~