一、实现目录
1、添加联系人
void addPerson(Adressbooks * abs)
2、显示联系人
void showPerson(Adressbooks * abs)
3、删除联系人
void deletePerson(Adressbooks * abs)
4、查找联系人
void findPerson(Adressbooks * abs)
5、修改联系人
void modifyPerson(Adressbooks * abs)
6、清空联系人
void cleanPerson(Adressbooks * abs)
7、退出通讯录
return 0; break;
二、头文件模块
#include"iostream"
#include"string"
#define MAX 1000
说明:
第一个头文件包含c++基本输入输出流文件;
第二个头文件包含字符串处理,体现在通讯录成员的姓名;
第三个头文件宏定义此通讯录最大容纳量;
三、自定义结构体
1.设计通讯录结构体
struct Adressbooks
{
Person personArray[MAX]; //通讯录中保存的联系人数组
int m_size; //通讯录中保存人的个数
};
2.设计通讯录中成员的结构体
struct Person
{
string m_Name;
int m_Sex; //1、男 2、女
int m_Age;
string m_Phone;
string m_Addr; //地址
};
四、代码块
#include"iostream"
#include"string"
#define MAX 1000
using namespace std;
//菜单界面
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;
}
//设计联系人结构体
struct Person
{
string m_Name;
int m_Sex; //1、男 2、女
int m_Age;
string m_Phone;
string m_Addr; //地址
};
//设计通讯录结构体
struct Adressbooks
{
Person personArray[MAX]; //通讯录中保存的联系人数组
int m_size; //通讯录中保存人的个数
};
//添加联系人
void addPerson(Adressbooks * abs)
{
//判断通讯录是否已满
if (abs->m_size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
//添加姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[abs->m_size].m_Name = name;
//添加性别
cout << "请输入性别:" << endl;
cout << "1——男 2——女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->m_size].m_Sex = sex;
break;
}
cout << "请重新输入" << endl;
}
//添加年龄
cout << "请输入年龄:" << endl;
int age = 0;
cin >> age;
abs->personArray[abs->m_size].m_Age = age;
//添加电话
cout << "请输入联系电话:" << endl;
string phone;
cin >> phone;
abs->personArray[abs->m_size].m_Phone = phone;
//添加地址
cout << "请输入家庭地址:" << endl;
string address;
cin >> address;
abs->personArray[abs->m_size].m_Addr = address;
//更新通讯录中的人数
abs->m_size++;
cout << "添加成功!!!" << endl;
}
system("pause"); //请按任意键继续
system("cls"); //清屏
}
//显示联系人
void showPerson(Adressbooks * abs)
{
if (abs->m_size == 0)
{
cout << "当前的记录为空!" << endl;
}
else
{
for (int i = 0; i < abs->m_size; i++)
{
cout << "姓名:" << abs->personArray[i].m_Name << "\t";
cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女")<< "\t";
cout << "年龄:" << abs->personArray[i].m_Age << "\t";
cout << "电话:" << abs->personArray[i].m_Phone << "\t";
cout << "地址:" << abs->personArray[i].m_Addr << endl;
}
}
system("pause");
system("cls");
}
//删除联系人
//检测联系人是否存在 存在则返回该联系人在数组中的地址 不存在返回-1
//参数1——通讯录 参数2——欲删除人姓名
int isExist(Adressbooks * abs, string name)
{
for (int i = 0; i < abs->m_size; i++)
{
if (abs->personArray[i].m_Name == name)
{
return i; //存在,显示编号
}
}
return -1; //遍历结束仍未找到此人
}
void deletePerson(Adressbooks * abs)
{
cout << "请输入要删除的联系人:" << endl;
string name;
cin >> name;
//ret = -1 ——未查到 ret != -1 ——找到
int ret=isExist(abs, name);
if (ret != -1)
{
for (int i = ret; i < abs->m_size; i++)
{
//数据前移
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_size--; //更新通讯录人数
cout << "删除成功!" << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
//查找联系人
void findPerson(Adressbooks * abs)
{
cout << "请输入要查找的联系人:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
cout << "姓名:" << abs->personArray[ret].m_Name << "\t";
cout << "性别:" << abs->personArray[ret].m_Sex << "\t";
cout << "年龄:" << abs->personArray[ret].m_Age << "\t";
cout << "电话:" << abs->personArray[ret].m_Phone << "\t";
cout << "地址:" << abs->personArray[ret].m_Addr << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
//修改联系人
void modifyPerson(Adressbooks * abs)
{
cout << "请输入要修改的联系人:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
//姓名修改
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[ret].m_Name = name;
//修改性别
int sex = 0;
cout << "行输入性别:" << endl;
cout << "1——男 2——女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[ret].m_Sex = sex;
break;
}
cout << "请重新输入!" << endl;
}
//年龄修改
int age;
cout << "请输入年龄:" << endl;
cin >> age;
abs->personArray[ret].m_Age = age;
//修改电话
string phone;
cout << "请输入电话号码:" << endl;
cin >> phone;
abs->personArray[ret].m_Phone = phone;
//修改地址
string address;
cout << "请输入家庭地址:" << endl;
cin >> address;
abs->personArray[ret].m_Addr = address;
cout << "修改成功!!" << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
//清空通讯录
//将通讯录的联系人数重置为0,逻辑清空
void cleanPerson(Adressbooks * abs)
{
int choice = 0;
cout << "请再次确认是否清空:(1——确认清空 2——我再想想)!" << endl;
while (true)
{
cin >> choice;
if (choice == 1 )
{
abs->m_size = 0;
cout << "通讯录已清空!" << endl;
break;
}
else if (choice == 2)
{
cout << "那就在想想吧!" << endl;
break;
}
cout << "请重新输入:" << endl;
}
system("pause");
system("cls");
}
int main()
{
//创建一个通讯录结构体变量
Adressbooks abs;
//初始化通讯录当前人员个数
abs.m_size = 0;
int select = 0; //用户选择输入变量
while (true)
{
showMenu();
cin >> select;
switch (select)
{
case 1: //1、添加联系人
addPerson(&abs); //地址传递,改变实参
break;
case 2: //2、显示联系人
showPerson(&abs);
break;
case 3: //3、删除联系人
/*{
cout << "请输入欲删除人的姓名:" << endl;
string name;
cin >> name;
if (isExist(&abs, name) == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "找到此人" << endl;
}
}*/
deletePerson(&abs);
break;
case 4: //4、查找联系人
findPerson(&abs);
break;
case 5: //5、修改联系人
modifyPerson(&abs);
break;
case 6: //6、清空联系人
cleanPerson(&abs);
break;
case 0: //0、退出通讯录
cout << "欢迎下次使用!" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
五、总结
1、查找、修改、删除联系人前均应先判断此联系人是否存在;
2、删除联系人只需将此联系人的后一位信息数据进行前移,达到覆盖的目的;
3、清空通讯录我们只需要做到逻辑清空,将它的容量改为0即可。