#include <iostream>
#include <string>
using namespace std;
#define MAX 1000
struct person
{
//姓名
string m_Name;
//性别:1代表男 2代表女
int m_Sex;
//年龄
int m_Age;
//电话
string m_Phone;
//地址
string m_Addr;
};
struct books
{
//通讯录中保存的联系人数字
struct person personArray[MAX];
//当前记录联系人个数
int m_size;
};
//添加联系人
void addPerson(struct books * bs)
{
//判断通讯录里人满没有
if (bs->m_size == MAX)
{
cout << "通讯录联系人已满" << endl;
}
else
{
//添加具体联系人
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
bs->personArray[bs->m_size].m_Name = name;
//性别
int sex;
cout << "请输入性别:" << endl;
cout << "1--男" << endl;
cout << "2--女" << endl;
while (1)
{
//如果输入的是1或2则退出循环,否则重新输入
cin >> sex;
if (sex == 1 || sex == 2)
{
bs->personArray[bs->m_size].m_Sex = sex;
break;
}
cout << "请重新输入真确的性别" << endl;
}
//年龄
int age;
cout << "请输入年龄:" << endl;
cin >> age;
bs->personArray[bs->m_size].m_Age = age;
//电话
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
bs->personArray[bs->m_size].m_Phone = phone;
//地址
string addr;
cout << "请输入地址:" << endl;
cin >> addr;
bs->personArray[bs->m_size].m_Addr = addr;
//通讯录里的人数
bs->m_size++;
cout << "添加联系人成功" << endl;
system("pause");
system("cls");
}
}
//显示联系人
void showPerson(struct books *bs)
{
//判断通讯录中人数是否为0,提升记录为空
if(bs->m_size == 0)
{
cout << "当前记录为空" << endl;
}
//不为0则显示联系人
else
{
for (int i = 0; i < bs->m_size; i++)
{
cout << "姓名:" << bs->personArray[i].m_Name << "\t";
cout << "性别:" << (bs->personArray[i].m_Sex == 1 ? "男" : "女")<< "\t";
cout << "年龄:" << bs->personArray[i].m_Age << "\t";
cout << "电话:" << bs->personArray[i].m_Phone << "\t";
cout << "地址:" << bs->personArray[i].m_Addr << "\t";
cout << "\t" << endl;
}
}
//按任意键继续
system("pause");
//清屏操作
system("cls");
}
//检查联系人是否存在,如果存在返回联系人所在数组中的具体位置,不存在返回-1
//参数一:通信录 参数二:对比人名
int isExist(struct books *bs ,string name)
{
for (int i = 0; i < bs->m_size; i++)
{
if (bs->personArray[i].m_Name == name)
{
return i;
}
}
return -1;
}
//删除指定联系人
void deletePerson(struct books *bs)
{
cout << "请输入你要删除的联系人" << endl;
string name;
cin >> name;
int ret = isExist(bs, name);
if (ret != -1)
{
//有这个人则进行删除
for (int i = ret; i < bs->m_size; i++)
{
//数据迁移
bs->personArray[i] = bs->personArray[i + 1];
}
bs->m_size--;//更新通讯录成员人数
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//查找指定联系人信息
void findPerson(struct books *bs)
{
string name;
cout << "请输入要查找联系人的姓名" << endl;
cin >> name;
int ret = isExist(bs,name);
if (ret != -1)
{
cout << "姓名:" << bs->personArray[ret].m_Name << "\t";
cout << "年龄:" << bs->personArray[ret].m_Age << "\t";
cout << "性别:" << bs->personArray[ret].m_Sex << "\t";
cout << "电话:" << bs->personArray[ret].m_Phone << "\t";
cout << "地址:" << bs->personArray[ret].m_Addr << "\t";
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//修改联系人信息
void modifyPerson(books *bs)
{
string name;
cout << "请输入要修改的联系人" << endl;
cin >> name;
int ret = isExist(bs, name);
if (ret != -1)
{
//姓名
string name;
cout << "请输入要修改的姓名" << endl;
cin >> name;
bs->personArray[ret].m_Name = name;
//性别
int sex = 0;
cout << "请输入要修改的性别" << endl;
cout << "1--男" << endl;
cout << "2--女" << endl;
while (1)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
bs->personArray[ret].m_Sex = sex;
break;
}
cout << "输入有误请重新输入" << endl;
}
//年龄
int age;
cout << "请输入要修改的年龄" << endl;
cin >> age;
bs->personArray[ret].m_Age = age;
//电话
string phone;
cout << "请输入修改的电话" << endl;
cin >> phone;
bs->personArray[ret].m_Phone = phone;
//地址
string addr;
cout << "请输入修改的地址" << endl;
cin >> addr;
bs->personArray[ret].m_Addr = addr;
cout << "修改成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//清空联系人
void clearnPerson(books *bs)
{
int num;
cout << "是否清空所以联系人" << endl;
cout << "点击1确认,0取消" << endl;
cin >> num;
if (num == 1)
{
bs->m_size = 0; //将当前联系人人数置为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;
}
int main()
{
int num = 0; //创建选择功能变量
struct books bs; //创建通讯录结构体变量
bs.m_size = 0; //初始化通讯录结构体里的元素
while (true)
{
showMenu();
cout << "请选择要使用的功能" << endl;
cin >> num;
switch (num)
{
case 1: //添加联系人
addPerson(&bs);
break;
case 2: //显示联系人
showPerson(&bs);
break;
case 3: //删除联系人
deletePerson(&bs);
break;
case 4: //查找联系人
findPerson(&bs);
break;
case 5: //修改联系人
modifyPerson(&bs);
break;
case 6: //清空联系人
clearnPerson(&bs);
break;
case 0: //退出通讯录
cout << "欢饮下次使用" << endl;
system("pause"); //再按一下退出
return 0;
break;
default:
cout << "请输入正确的选项" << endl;
break;
}
}
system("pause");
return 0;
}
11-04
8705
05-25
5万+
11-14
127
11-13
460