1、系统需求
功能描述:用户选择功能的界面
菜单功能效果图如下图:
步骤:
1、封装函数显示该界面
2、在main函数中调用封装好的函数
示例代码如下:
#include
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;
}
int main()
{
//菜单的调用
showmenu();
system("pause");
return 0;
}
功能描述:退出通讯录系统
思路:根据用户不同的选择,进入不同的功能,对整个架构进行构建
当用户选择0时,执行退出,选择其他先不做操作,也不会退出系统
效果图如下图:
示例代码如下:
#include
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;
}
int main()
{
int select;
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;
}
}
}
效果图如下:
示例代码如下:
#include
using namespace std;
#include
#define MAX 1000
//设计联系人结构体
struct person
{
string m_name; //姓名
int m_sex; //性别
int m_age; //年龄
string m_phone; //电话
string m_addr; //住址
};
//定义通讯录结构体
struct addressbooks
{
//通讯录中保存的联系人数组
struct person personarray[MAX];
//通讯录中当前记录联系人个数
int m_size;
};
//1、添加联系人
void addperson(addressbooks* 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---男" << endl;
cout << "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 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()
{
//创建通讯录结构体变量
addressbooks abs;
//初始化通讯录中当前的人数个数
abs.m_size = 0;
int select=0;
while (true)
{
//菜单的调用
showmenu();
cin >> select;
switch (select)
{
case 1: //1、添加联系人
addperson(&abs); //利用地址可以传递实参
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;
}
}
}
示例代码如下 :
#include
using namespace std;
#include
#define MAX 1000
//设计联系人结构体
struct person
{
string m_name; //姓名
int m_sex; //性别
int m_age; //年龄
string m_phone; //电话
string m_addr; //住址
};
//定义通讯录结构体
struct addressbooks
{
//通讯录中保存的联系人数组
struct person personarray[MAX];
//通讯录中当前记录联系人个数
int m_size;
};
//1、添加联系人
void addperson(addressbooks* 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---男" << endl;
cout << "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"); //清屏
}
}
//2、显示所有联系人
void showperson(addressbooks* abs)
{
//判断通讯录中人数是否为0,如果为0,提示记录为空,如果不为0,显示信息
if (abs->m_size == 0)
{
cout << "当前记录为空" << endl;
}
else
{
for (int i = 0; i < abs->m_size; i++)
{
cout << "姓名:" << abs->personarray[i].m_name << endl;
cout << "性别:" << abs->personarray[i].m_sex << endl;
cout << "年龄:" << abs->personarray[i].m_age << endl;
cout << "电话:" << abs->personarray[i].m_phone << endl;
cout << "家庭住址:" << abs->personarray[i].m_addr << 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()
{
//创建通讯录结构体变量
addressbooks 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、删除联系人
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;
}
}
}
在上面效果图中:有两个问题
1、每一个人的信息如果都竖排显示的话,会导致一个页面显示不全,不方便查看
2、性别显示的是1和2
故做一下修改
for (int i = 0; i < abs->m_size; i++)
{
cout << "姓名:" << abs->personarray[i].m_name << "\t";
cout << "性别:" << abs->personarray[i].m_sex << "\t";
cout << "年龄:" << abs->personarray[i].m_age << "\t";
cout << "电话:" << abs->personarray[i].m_phone << "\t";
cout << "家庭住址:" << abs->personarray[i].m_addr << endl;
}
cout << "性别:" << (abs->personarray[i].m_sex ==1?"男":"女" )<< "\t";
修改后,效果如下:
删除后通讯录中的信息
效果图如图所示:
//4、查找指定联系人信息
void findperson(addressbooks* 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");
}
效果图如下所示:
模块函数代码如下:
//5、修改指定联系人信息
void modifyperson(addressbooks* 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;
//性别
cout << "请输入性别:" << endl;
cout << "1---男" << endl;
cout << "2----女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personarray[ret].m_sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//年龄
int age = 0;;
cout << "请输入年龄:" << endl;
cin >> age;
abs->personarray[ret].m_age = age;
//电话
int phone = 0;
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");
}
函数代码如下:
void cleanperson(addressbooks* abs)
{
abs->m_size = 0; //使通讯录中的联系人数重置为0,逻辑清空即可
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
10、完整代码如下:
#include
using namespace std;
#include
#define MAX 1000
//设计联系人结构体
struct person
{
string m_name; //姓名
int m_sex; //性别
int m_age; //年龄
string m_phone; //电话
string m_addr; //住址
};
//定义通讯录结构体
struct addressbooks
{
//通讯录中保存的联系人数组
struct person personarray[MAX];
//通讯录中当前记录联系人个数
int m_size;
};
//1、添加联系人
void addperson(addressbooks* 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---男" << endl;
cout << "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"); //清屏
}
}
//2、显示所有联系人
void showperson(addressbooks* abs)
{
//判断通讯录中人数是否为0,如果为0,提示记录为空,如果不为0,显示信息
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(addressbooks* abs, string name)
{
for (int i = 0; i < abs->m_size; i++)
{
//找到用户输入的姓名了
if (abs->personarray[i].m_name == name)
{
return i; //找到了返回下标
}
}
return -1; //没找到返回-1
}
//3、删除联系人
void deletperson(addressbooks* abs)
{
cout << "请输入您要删除的联系人" << endl;
string name;
cin >> name;
int ret=isExist(abs, name); //ret两种结果-1没查到,其他查到
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;
}
}
//4、查找指定联系人信息
void findperson(addressbooks* 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");
}
//5、修改指定联系人信息
void modifyperson(addressbooks* 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;
//性别
cout << "请输入性别:" << endl;
cout << "1---男" << endl;
cout << "2----女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personarray[ret].m_sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//年龄
int age = 0;;
cout << "请输入年龄:" << endl;
cin >> age;
abs->personarray[ret].m_age = age;
//电话
int phone = 0;
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");
}
//6、清空联系人
void cleanperson(addressbooks* abs)
{
abs->m_size = 0; //使通讯录中的联系人数重置为0,逻辑清空即可
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()
{
//创建通讯录结构体变量
addressbooks 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、删除联系人
deletperson(&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;
}
}
}