参考于https://www.bilibili.com/video/BV1et411b73Z
实现功能:
(1)添加联系人
(2)显示联系人
(3)删除联系人
(4)查找联系人
(5)修改联系人
(6)清空联系人
(7)退出通讯录
系统设计
(1)系统显示界面如图所示:
(2)添加联系人功能如下所示:
(3)显示联系人功能如下:
(4)删除联系人功能如下所示:
删除现有联系人
如果删除没有的联系人
(5)查找联系人功能如下:
(6)修改联系人信息功能如下:
(7)清空联系人功能如下:
代码实现
(1)显示界面
//显示界面
void display()
{
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;
}
(2)添加联系人
//定义一个联系人结构体
struct person{
string name;
int sex;
int age;
string telphone;
string address;
};
//定义通讯录结构体
struct addressbook{
person arr[Max];
int person_num;
};
//添加联系人
void insert_person(addressbook *addressbook2)
{
if (addressbook2->person_num >= Max)
{
cout << "通讯录已经满了,无法添加" << endl;
return;
}
else
{
//添加数据
string name="meng";
cout << "请输入姓名:" << endl;
cin >> name;
addressbook2->arr[addressbook2->person_num].name = name;
cout << "请输入性别:" << endl;
cout << "1--------男" << endl;
cout << "2--------女" << endl;
int sex=0;
while (1)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
addressbook2->arr[addressbook2->person_num].sex = sex;
break;
}
else
{
cout << "请重新输入:" << endl;
}
}
cout << "请输入年龄:" << endl;
int age=0;
cin >> age;
addressbook2->arr[addressbook2->person_num].age = age;
cout << "请输入联系电话:" << endl;
string telephone;
cin >> telephone;
addressbook2->arr[addressbook2->person_num].telphone = telephone;
cout << "请输入家庭:" << endl;
string address;
cin >> address;
addressbook2->arr[addressbook2->person_num].address = address;
addressbook2->person_num++;
cout << "添加成功" << endl;
system("pause");//按任意键继续
system("cls");//清屏操作
}
}
//显示联系人
void display_person(addressbook *addressbook2)
{
if (addressbook2->person_num == 0)
{
cout << "通讯录为空" << endl;
}
else
{
for (int i = 0; i < addressbook2->person_num; i++)
{
cout << "姓名:" << addressbook2->arr[i].name << endl;
cout << "性别:" << addressbook2->arr[i].sex << endl;
cout << "年龄:" << addressbook2->arr[i].age << endl;
cout << "电话:" << addressbook2->arr[i].telphone << endl;
cout << "住址:" << addressbook2->arr[i].address << endl;
}
}
system("pause");
system("cls");
}
运行结果:
联系人的信息都是换行输出,非常不美观,并且性别栏为1或者2,表达信息不直接,为此,我们进行完善
//显示联系人
void display_person(addressbook *addressbook2)
{
if (addressbook2->person_num == 0)
{
cout << "通讯录为空" << endl;
}
else
{
for (int i = 0; i < addressbook2->person_num; i++)
{
cout << "姓名:" << addressbook2->arr[i].name << "\t";
if (addressbook2->arr[i].sex == 1)
{
cout << "性别:男" << "\t";
}
else
{
cout << "性别:女" << "\t";
}
cout << "年龄:" << addressbook2->arr[i].age << "\t";
cout << "电话:" << addressbook2->arr[i].telphone << "\t";
cout << "住址:" << addressbook2->arr[i].address << endl;
}
}
system("pause");
system("cls");
}
//判断联系人是否存在
int isexit_person(addressbook *addressbook2)
{
cout << "请输入要删除的人的名字" << endl;
string name;
cin >> name;
//遍历整个数组,进行一一对比
for (int i = 0; i < addressbook2->person_num; i++)
{
if (addressbook2->arr[i].name == name)
{
return i;
}
}
return -1;
system("pause");
//system("cls");
}
//删除联系人
void delete_person(addressbook *addressbook2)
{
if (isexit_person(addressbook2) == -1)
{
cout << "查无此人" << endl;
}
else
{
for (int i = isexit_person(addressbook2); i < addressbook2->person_num;i++)
{
addressbook2->arr[i] = addressbook2->arr[i + 1];
}
addressbook2->person_num = addressbook2->person_num - 1;
cout << "删除成功" << endl;
}
}
//判断要查找的联系人是否存在
int isexit_person2(addressbook *addressbook2,string name)
{
//遍历整个数组,进行一一对比
for (int i = 0; i < addressbook2->person_num; i++)
{
if (addressbook2->arr[i].name == name)
{
return i;
}
}
return -1;
system("pause");
//system("cls");
}
//查找联系人
void find_person(addressbook *addressbook2)
{
cout << "请输入要查找的人的名字" << endl;
string name;
cin >> name;
if (isexit_person2(addressbook2,name) == -1)
{
cout << "此联系人不存在" << endl;
}
else
{
int i = isexit_person2(addressbook2,name);
cout << "姓名:" << addressbook2->arr[i].name << "\t";
if (addressbook2->arr[i].sex == 1)
{
cout << "性别:男" << "\t";
}
else
{
cout << "性别:女" << "\t";
}
cout << "年龄:" << addressbook2->arr[i].age << "\t";
cout << "电话:" << addressbook2->arr[i].telphone << "\t";
cout << "住址:" << addressbook2->arr[i].address << endl;
cout << "查找成功" << endl;
}
}
//修改菜单
void display_modify()
{
cout << "**************************" << endl;
cout << "**** 1 修改联系人姓名 ****" << endl;
cout << "**** 2 修改联系人性别 ****" << endl;
cout << "**** 3 修改联系人年龄 ****" << endl;
cout << "**** 4 修改联系人电话 ****" << endl;
cout << "**** 5 修改联系人住址 ****" << endl;
cout << "**************************" << endl;
}
//修改联系人
void modify_person(addressbook *addressbook2)
{
cout << "请输入要修改的联系人的名字" << endl;
string name;
cin >> name;
//判断要修改的联系人是否存在
if (isexit_person2(addressbook2, name) == -1)
{
cout << "此联系人不存在" << endl;
}
else
{
int j = isexit_person2(addressbook2, name);
display_modify();
cout << "请输入要修改的选项" << endl;
int num1;
cin >> num1;
switch (num1)
{
case 1:
{
cout << "请输入新姓名" << endl;
string name1;
cin >> name1;
addressbook2->arr[j].name = name1;
break;
}
case 2:
{
cout << "请输入新性别" << endl;
int sex;
cin >> sex;
addressbook2->arr[j].sex = sex;
break;
}
case 3:
{
cout << "请输入新年龄" << endl;
int age;
cin >> age;
addressbook2->arr[j].age = age;
break;
}
case 4:
{
cout << "请输入新电话" << endl;
string telphone;
cin >> telphone;
addressbook2->arr[j].telphone = telphone;
break;
}
case 5:
{
cout << "请输入新住址" << endl;
string address;
cin >> address;
addressbook2->arr[j].address = address;
break;
}
}
}
}
//清空联系人
void clear_person(addressbook *addressbook2)
{
for (int i = 0; i < addressbook2->person_num; i++)
{
if (addressbook2->arr[i].name != " ")
{
addressbook2->arr[i] = addressbook2->arr[Max-i-2];
}
}
addressbook2->person_num = addressbook2->person_num - 1;
cout << "通讯录已经清空" << endl;
system("pause");
//system("cls");
}
采用删除的理念
cout << "欢迎下次使用" << endl;
system("pause");//请按任意键继续
return 0;
default:
break;
完整代码见以下链接
https://mp.csdn.net/console/upDetailed