#include <iostream>
using namespace std;
#include <string>
// 1 展示 菜单界面 void showMenu()
void showMenu()
{
cout << "****** 1、添加联系人 ******" << endl;
cout << "****** 2、显示联系人 ******" << endl;
cout << "****** 3、删除联系人 ******" << endl;
cout << "****** 4、查找联系人 ******" << endl;
cout << "****** 5、修改联系人 ******" << endl;
cout << "****** 6、清空联系人 ******" << endl;
cout << "****** 7、退出通讯录 ******" << endl;
}
int main()
{
// 1 菜单界面
showMenu();
system("pause");
return 0;
}
1、设计联系人结构体
2、设计通讯录结构体
3、主函数中创建通讯录
4、封装添加联系人函数
5、测试添加联系人功能
// 添加联系人
void addperson(address_book *address_book_1)
{
// 1、判断通讯录是否满,是:提示,否:添加
if (address_book_1->num == MAX)
{
cout << "通讯录已满" << endl;
return;
}
else
{
// 添加联系人:姓名、性别、年龄、电话、住址
//姓名
string name;
cout << "请输入姓名" << endl;
cin >> name;
address_book_1->personArray[address_book_1->num].name = name;
//性别
int sex = 0;
while (true)
{
cout << "请输入性别" << endl;
cout << "1--男" << endl;
cout << "2--女" << endl;
cin >> sex;
// 输入有误 重新输入
if (sex == 1 || sex == 2)
{
address_book_1->personArray[address_book_1->num].sex = sex;
break;
}
else
{
cout << "输入有误 重新输入" << endl;
}
}
//年龄
int age = 0;
cout << "请输入年龄" << endl;
cin >> age;
address_book_1->personArray[address_book_1->num].age = age;
//电话
cout << "请输入电话" << endl;
string phone;
cin >> phone;
address_book_1->personArray[address_book_1->num].phone = phone;
//住址
cout << "请输入住址" << endl;
string addr;
cin >> addr;
address_book_1->personArray[address_book_1->num].addr = addr;
address_book_1->num++;
cout << "添加成功,现在人数为:" << address_book_1->num << endl;
// 清屏
system("pause");//请按任意键继续
system("cls");//清屏操作
}
}
// 显示联系人
void showperson(address_book *address_book_1)
{
// 判断通讯录人数是否为0,为0提示记录为空,不为0显示联系人
if (address_book_1->num == 0)
{
cout << "当前无联系人" << endl;
}
else
{
cout << "当前人数为 = " << address_book_1->num << endl;
for (int i = 0; i < address_book_1->num; i++)
{
cout << endl;
cout << "第" << i << "人"<< "\t";
cout << "name:" << address_book_1->personArray[i].name << "\t";
cout << "sex:" << (address_book_1->personArray[i].sex ==1 ? "男":"女") << "\t"; // 判断
cout << "age:" << address_book_1->personArray[i].age << "\t";
cout << "phone:" << address_book_1->personArray[i].phone << "\t";
cout << "addr:" << address_book_1->personArray[i].addr << endl;
}
}
// 清屏
system("pause");//请按任意键继续
system("cls");//清屏操作
}
// 删除联系人
int deleteperson_index(address_book* address_book_1, string name)
{
//先判断输入的联系人是否存在
//存在:返回联系人所在数组中的位置
for (int i = 0; i < address_book_1->num; i++)
{
if (address_book_1->personArray[i].name == name)
{
return i;
}
}
//不存在:返回 -1
return -1;
}
void deleteperson(address_book * address_book_1, int name_index)
{
for (int i = name_index; i < address_book_1->num; i++)
{
address_book_1->personArray[i] = address_book_1->personArray[i + 1];
}
address_book_1->num--; //相当于删除了最后一个人
cout << "删除成功" << endl;
// 清屏
system("pause");//请按任意键继续
system("cls");//清屏操作
}
// 查找联系人信息
void findperson(address_book* address_book_1)
{
cout << "请输入要查找联系人的姓名" << endl;
string name;
cin >> name;
int k=0;
for (int i = 0; i < address_book_1 ->num; i++)
{
if (address_book_1->personArray[i].name == name)
{
k = 1;
cout << "name:"<< address_book_1->personArray[i].name <<"\t";
cout << "sex:" << address_book_1->personArray[i].sex << "\t";
cout << "age:" << address_book_1->personArray[i].age << "\t";
cout << "phone:" << address_book_1->personArray[i].phone << "\t";
cout << "addr:" << address_book_1->personArray[i].addr << endl;
}
}
if (k == 0)
{
cout << "查无此人" << endl;
}
// 清屏
system("pause");//请按任意键继续
system("cls");//清屏操作
}
// 修改联系人信息
void alter(address_book* address_book_1)
{
cout << "请输入修改联系人的 姓名" << endl;
string name;
cin >> name;
int name_index;
name_index = person_index(address_book_1, name);
int sex;
int age;
string phone;
string addr;
cout << "请输入修改联系人的 姓名" << endl;
cin >> name;
cout << "请输入修改联系人的 性别" << endl;
cin >> sex;
cout << "请输入修改联系人的 年龄" << endl;
cin >> age;
cout << "请输入修改联系人的 电话" << endl;
cin >> phone;
cout << "请输入修改联系人的 地址" << endl;
cin >> addr;
address_book_1->personArray[name_index].name = name;
address_book_1->personArray[name_index].sex = sex;
address_book_1->personArray[name_index].age = age;
address_book_1->personArray[name_index].phone = phone;
address_book_1->personArray[name_index].addr = addr;
// 清屏
system("pause");//请按任意键继续
system("cls");//清屏操作
}
// 清空联系人
void cleanperson(address_book* address_book_1)
{
// 只是逻辑上的清楚,但没有清空存储空间,后面添加的数据会覆盖 清空之前的数据
address_book_1->num = 0;
cout << "已清空联系人" << endl;
// 清屏
system("pause");//请按任意键继续
system("cls");//清屏操作
};
#include <iostream>
using namespace std;
#include <string>
// 1 展示 菜单界面 void showMenu()
void showMenu()
{
cout << "****** 1、添加联系人 ******" << endl;
cout << "****** 2、显示联系人 ******" << endl;
cout << "****** 3、删除联系人 ******" << endl;
cout << "****** 4、查找联系人 ******" << endl;
cout << "****** 5、修改联系人 ******" << endl;
cout << "****** 6、清空联系人 ******" << endl;
cout << "****** 0、退出通讯录 ******" << endl;
}
int main()
{
int select = 0; //创建用户选择输入的变量
// 1 菜单界面
while (true)
{
showMenu();
cin >> select;
switch (select)
{
case 1: //1、添加联系人
break;
case 2://2、显示联系人
break;
case 3://3、删除联系人
break;
case 4://4、查找联系人
break;
case 5://5、修改联系人
break;
case 6://6、清空联系人
break;
case 0://0、退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
全部代码
#include <iostream>
using namespace std;
#include <string>
#define MAX 1000
// 1 展示 菜单界面 void showMenu()
void showMenu()
{
cout << "****** 1、添加联系人 ******" << endl;
cout << "****** 2、显示联系人 ******" << endl;
cout << "****** 3、删除联系人 ******" << endl;
cout << "****** 4、查找联系人 ******" << endl;
cout << "****** 5、修改联系人 ******" << endl;
cout << "****** 6、清空联系人 ******" << endl;
cout << "****** 0、退出通讯录 ******" << endl;
}
// 联系人结构体
struct person
{
//姓名,性别,年龄,电话,住址
string name;
int sex;
int age;
string phone;
string addr;
};
// 通讯录结构体
struct address_book
{
//人数、人员列表
int num;
struct person personArray[MAX];
};
// 添加联系人
void addperson(address_book *address_book_1)
{
// 1、判断通讯录是否满,是:提示,否:添加
if (address_book_1->num == MAX)
{
cout << "通讯录已满" << endl;
return;
}
else
{
// 添加联系人:姓名、性别、年龄、电话、住址
//姓名
string name;
cout << "请输入姓名" << endl;
cin >> name;
address_book_1->personArray[address_book_1->num].name = name;
//性别
int sex = 0;
while (true)
{
cout << "请输入性别" << endl;
cout << "1--男" << endl;
cout << "2--女" << endl;
cin >> sex;
// 输入有误 重新输入
if (sex == 1 || sex == 2)
{
address_book_1->personArray[address_book_1->num].sex = sex;
break;
}
else
{
cout << "输入有误 重新输入" << endl;
}
}
//年龄
int age = 0;
cout << "请输入年龄" << endl;
cin >> age;
address_book_1->personArray[address_book_1->num].age = age;
//电话
cout << "请输入电话" << endl;
string phone;
cin >> phone;
address_book_1->personArray[address_book_1->num].phone = phone;
//住址
cout << "请输入住址" << endl;
string addr;
cin >> addr;
address_book_1->personArray[address_book_1->num].addr = addr;
address_book_1->num++;
cout << "添加成功,现在人数为:" << address_book_1->num << endl;
// 清屏
system("pause");//请按任意键继续
system("cls");//清屏操作
}
}
// 显示联系人
void showperson(address_book *address_book_1)
{
// 判断通讯录人数是否为0,为0提示记录为空,不为0显示联系人
if (address_book_1->num == 0)
{
cout << "当前无联系人" << endl;
}
else
{
cout << "当前人数为 = " << address_book_1->num << endl;
for (int i = 0; i < address_book_1->num; i++)
{
cout << endl;
cout << "第" << i+1 << "人"<< "\t";
cout << "name:" << address_book_1->personArray[i].name << "\t";
cout << "sex:" << (address_book_1->personArray[i].sex ==1 ? "男":"女") << "\t"; // 判断
cout << "age:" << address_book_1->personArray[i].age << "\t";
cout << "phone:" << address_book_1->personArray[i].phone << "\t";
cout << "addr:" << address_book_1->personArray[i].addr << endl;
}
}
// 清屏
system("pause");//请按任意键继续
system("cls");//清屏操作
}
// 删除联系人
int person_index(address_book* address_book_1, string name)
{
//先判断输入的联系人是否存在
//存在:返回联系人所在数组中的位置
for (int i = 0; i < address_book_1->num; i++)
{
if (address_book_1->personArray[i].name == name)
{
return i;
}
}
//不存在:返回 -1
return -1;
}
void deleteperson(address_book * address_book_1, int name_index)
{
for (int i = name_index; i < address_book_1->num; i++)
{
address_book_1->personArray[i] = address_book_1->personArray[i + 1];
}
address_book_1->num--; //相当于删除了最后一个人
cout << "删除成功" << endl;
// 清屏
system("pause");//请按任意键继续
system("cls");//清屏操作
}
// 查找联系人信息
void findperson(address_book* address_book_1)
{
cout << "请输入要查找联系人的姓名" << endl;
string name;
cin >> name;
int k = 0;
for (int i = 0; i < address_book_1->num; i++)
{
if (address_book_1->personArray[i].name == name)
{
k = 1;
cout << "name:" << address_book_1->personArray[i].name << "\t";
cout << "sex:" << address_book_1->personArray[i].sex << "\t";
cout << "age:" << address_book_1->personArray[i].age << "\t";
cout << "phone:" << address_book_1->personArray[i].phone << "\t";
cout << "addr:" << address_book_1->personArray[i].addr << endl;
}
}
if (k == 0)
{
cout << "查无此人" << endl;
}
// 清屏
system("pause");//请按任意键继续
system("cls");//清屏操作
};
// 修改联系人信息
void alter(address_book* address_book_1)
{
cout << "请输入修改联系人的 姓名" << endl;
string name;
cin >> name;
int name_index;
name_index = person_index(address_book_1, name);
int sex;
int age;
string phone;
string addr;
cout << "请输入修改联系人的 姓名" << endl;
cin >> name;
cout << "请输入修改联系人的 性别" << endl;
cin >> sex;
cout << "请输入修改联系人的 年龄" << endl;
cin >> age;
cout << "请输入修改联系人的 电话" << endl;
cin >> phone;
cout << "请输入修改联系人的 地址" << endl;
cin >> addr;
address_book_1->personArray[name_index].name = name;
address_book_1->personArray[name_index].sex = sex;
address_book_1->personArray[name_index].age = age;
address_book_1->personArray[name_index].phone = phone;
address_book_1->personArray[name_index].addr = addr;
// 清屏
system("pause");//请按任意键继续
system("cls");//清屏操作
};
// 清空联系人
void cleanperson(address_book* address_book_1)
{
// 只是逻辑上的清楚,但没有清空存储空间,后面添加的数据会覆盖 清空之前的数据
address_book_1->num = 0;
cout << "已清空联系人" << endl;
// 清屏
system("pause");//请按任意键继续
system("cls");//清屏操作
};
int main()
{
address_book address_book_1;
address_book_1.num = 0;
int select = 0; //创建用户选择输入的变量
// 1 菜单界面
while (true)
{
showMenu();
cin >> select;
switch (select)
{
case 1: //1、添加联系人
addperson( &address_book_1); //地址传递才能真正的修改 实参
break;
case 2://2、显示联系人
showperson(&address_book_1);
break;
case 3://3、删除联系人
//先判断输入的联系人是否存在,存在:返回联系人所在数组中的位置,不存在返回 -1
{ //case 中有大量代码块,要用 { } 括起来。
cout << "请输入要删除 联系人的姓名" << endl;
string name;
cin >> name;
int name_index;
name_index = person_index(&address_book_1, name);
if (name_index == -1)
{
cout << "查无此人" << endl;
}
else
{
deleteperson(&address_book_1, name_index);
}
}
break;
case 4://4、查找联系人
findperson(&address_book_1);
break;
case 5://5、修改联系人
alter(&address_book_1);
break;
case 6://6、清空联系人
cleanperson(&address_book_1);
break;
case 0://0、退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}