第一个代码取消注释的函数可直接运行,下面附加函数分文件编写后的代码(头文件和函数源文件)
函数的分文件编写(作用:让代码结构更加清晰)
函数分文件的编写一般有四个步骤
1.创建后缀名为.h的头文件
2.创建后缀名为.cpp的源文件
3.在头文件中写函数的声明
4.在源文件中写函数的定义
//封装函数显示界面 例如:void show()
//在main函数中调用封装好的函数
#include <iostream>
using namespace std;
#include "通讯录.h"
//下边注释掉函数可通过函数分文件来实现让代码更清晰
/*//菜单界面
void show()
{
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_address;
};
//通讯录结构体
#define MAX 1000//最大人数;注意define后面不写 分号
struct books
{
struct person personArray[MAX];//保存联系人的数组
int size;//当前联系人数量
};
//添加联系人
void addPerson(books* book)
{
//先判断通讯录是否已满;
if (book->size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
book->personArray[book->size].m_name = name;
cout << "请输入性别:" << endl;
cout << "1------男" << endl;
cout << "2------女" << endl;
//性别
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
book->personArray[book->size].m_sex = sex;
break;
}
else
{
cout << "输入有误,请重新输入!" << endl;
}
}
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age>0&&age<150)
{
book->personArray[book->size].m_age = age;
break;
}
else
{
cout << "输入有误,请重新输入!" << endl;
}
}
//电话
cout << "请输入电话:" << endl;
string phone;
cin >> phone;
book->personArray[book->size].m_phone = phone;
cout << "请输入家庭住址:" << endl;
string address;
cin >> address;
book->personArray[book->size].m_address = address;
book->size++;//更新通讯录人数
cout << "添加成功!" << endl;
system("pause");//请按任意键继续
system("cls");//清屏操作
}
}
//显示所有联系人
void showPerson(books * book)
{
//判断通讯录是否为空
if (0 == book->size)
{
cout << "当前通讯录为空" << endl;
}
else
{
for (int i = 0;i < book->size;i++)
{
cout << "姓名:" << book->personArray[i].m_name << "\t";
cout << "性别:" << (book->personArray[i].m_sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << book->personArray[i].m_age << "\t";
cout << "电话:" << book->personArray[i].m_phone << "\t";
cout << "住址:" << book->personArray[i].m_address <<endl;
}
cout << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//删除联系人(按照姓名)
//检测联系人是否存在,如果存在,返回联系人所在数组中的位置,不存在返回-1
int Exist(books* book, string name)//参数一 通讯录 参数二 对比姓名
{
for (int i = 0;i < book->size;i++)
{
//找到用户输入的姓名
if (book->personArray[i].m_name == name)
{
return i;//如果找到返回联系人所在数组中的位置
}
}
return -1;//如果遍历结束没找到返回-1;
}
void deletePerson(books *book)
{
cout << "请输入要删除的联系人姓名:" << endl;
string name;
cin >> name;
int ret = (Exist(book, name));
//注意此处book(地址)和case5中注释掉的book(自定义的数据类型)的不同
if (ret != -1)
{
for (int i = ret;i < book->size;i++)
{
book->personArray[i] = book->personArray[i + 1];
}
book->size--;
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//查找联系人
void findPerson(books* book)
{
cout << "请输入要查找的联系人姓名" << endl;
string name;
cin >> name;
int ret = (Exist(book, name));
if (ret != -1)
{
cout << "姓名:" << book->personArray[ret].m_name << "\t";
cout << "性别:" << (book->personArray[ret].m_sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << book->personArray[ret].m_age << "\t";
cout << "电话:" << book->personArray[ret].m_phone << "\t";
cout << "住址:" << book->personArray[ret].m_address << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//修改联系人
void modifyPerson(books* book)
{
cout << "请输入要修改的联系人姓名" << endl;
string name;
cin >> name;
int ret = (Exist(book, name));
if (ret != -1)
{
cout << "******************" << endl;
cout << "***** 1.姓名 *****" << endl;
cout << "***** 2.性别 *****" << endl;
cout << "***** 3.年龄 *****" << endl;
cout << "***** 4.电话*****" << endl;
cout << "***** 5.住址 *****" << endl;
cout << "******************" << endl;
int select = 0;
cin >> select;
switch (select)
{
case 1:
{
//姓名
string name;
cout << "请输入修改后的姓名:" << endl;
cin >> name;
book->personArray[ret].m_name = name;
break;
}
case 2:
{
cout << "请输入性别:" << endl;
cout << "1------男" << endl;
cout << "2------女" << endl;
//性别
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
book->personArray[ret].m_sex = sex;
break;
}
else
{
cout << "输入有误,请重新输入!" << endl;
}
}
}
case 3:
{
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age > 0 && age < 150)
{
book->personArray[ret].m_age = age;
break;
}
else
{
cout << "输入有误,请重新输入!" << endl;
}
}
}
case 4:
{
//电话
cout << "请输入电话:" << endl;
string phone;
cin >> phone;
book->personArray[ret].m_phone = phone;
}
case 5:
{
cout << "请输入家庭住址:" << endl;
string address;
cin >> address;
book->personArray[ret].m_address = address;
}
default:
break;
}
}
else
{
cout << "查无此人" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//清空联系人
void cleanPerson(books* book)
{
book->size = 0;
cout << "通讯录已清空" << endl; //将联系人数量置0
system("pause");//请按任意键继续
system("cls");//清屏操作
}*/
int main()
{
struct books book;//创建通讯录结构体变量
book.size = 0;//初始化通讯录中人员个数
int select = 0;
while (true)
{
show();
cin >> select;
switch (select)
{
case 1: //1.添加联系人
addPerson(&book);//用地址传递,值传递不会修改实参
break;
case 2: //2.显示联系人
showPerson(&book);
break;
case 3: //3.修改联系人
modifyPerson(&book);
break;
case 4: //4.查找联系人
findPerson(&book);
break;
case 5: //5.删除联系人
{
deletePerson(&book);
/*cout << "请输入要删除的联系人姓名:" << endl;
string name;
cin >> name;
if (Exist(&book, name)==-1)
{
cout << "查无此人" << endl;
}
else
{
cout << "查到此人" << endl;
}*/
break;
}
case 6: //6.清空联系人
cleanPerson(&book);
break;
case 0: //0.退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
cout << "请输入有效数字选项!" << endl;
system("pause");//请按任意键继续
system("cls");//清屏操作
break;
}
}
system("pause");
return 0;
}``
函数分写封装的函数源文件
#include "通讯录.h"
//菜单界面
void show()
{
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;
}
//添加联系人
void addPerson(books* book)
{
//先判断通讯录是否已满;
if (book->size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
book->personArray[book->size].m_name = name;
cout << "请输入性别:" << endl;
cout << "1------男" << endl;
cout << "2------女" << endl;
//性别
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
book->personArray[book->size].m_sex = sex;
break;
}
else
{
cout << "输入有误,请重新输入!" << endl;
}
}
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age > 0 && age < 150)
{
book->personArray[book->size].m_age = age;
break;
}
else
{
cout << "输入有误,请重新输入!" << endl;
}
}
//电话
cout << "请输入电话:" << endl;
string phone;
cin >> phone;
book->personArray[book->size].m_phone = phone;
cout << "请输入家庭住址:" << endl;
string address;
cin >> address;
book->personArray[book->size].m_address = address;
book->size++;//更新通讯录人数
cout << "添加成功!" << endl;
system("pause");//请按任意键继续
system("cls");//清屏操作
}
}
//显示所有联系人
void showPerson(books* book)
{
//判断通讯录是否为空
if (0 == book->size)
{
cout << "当前通讯录为空" << endl;
}
else
{
for (int i = 0;i < book->size;i++)
{
cout << "姓名:" << book->personArray[i].m_name << "\t";
cout << "性别:" << (book->personArray[i].m_sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << book->personArray[i].m_age << "\t";
cout << "电话:" << book->personArray[i].m_phone << "\t";
cout << "住址:" << book->personArray[i].m_address << endl;
}
cout << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//删除联系人(按照姓名)
//检测联系人是否存在,如果存在,返回联系人所在数组中的位置,不存在返回-1
int Exist(books* book, string name)//参数一 通讯录 参数二 对比姓名
{
for (int i = 0;i < book->size;i++)
{
//找到用户输入的姓名
if (book->personArray[i].m_name == name)
{
return i;//如果找到返回联系人所在数组中的位置
}
}
return -1;//如果遍历结束没找到返回-1;
}
void deletePerson(books* book)
{
cout << "请输入要删除的联系人姓名:" << endl;
string name;
cin >> name;
int ret = (Exist(book, name));
//注意此处book(地址)和case5中注释掉的book(自定义的数据类型)的不同
if (ret != -1)
{
for (int i = ret;i < book->size;i++)
{
book->personArray[i] = book->personArray[i + 1];
}
book->size--;
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//查找联系人
void findPerson(books* book)
{
cout << "请输入要查找的联系人姓名" << endl;
string name;
cin >> name;
int ret = (Exist(book, name));
if (ret != -1)
{
cout << "姓名:" << book->personArray[ret].m_name << "\t";
cout << "性别:" << (book->personArray[ret].m_sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << book->personArray[ret].m_age << "\t";
cout << "电话:" << book->personArray[ret].m_phone << "\t";
cout << "住址:" << book->personArray[ret].m_address << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//修改联系人
void modifyPerson(books* book)
{
cout << "请输入要修改的联系人姓名" << endl;
string name;
cin >> name;
int ret = (Exist(book, name));
if (ret != -1)
{
cout << "******************" << endl;
cout << "***** 1.姓名 *****" << endl;
cout << "***** 2.性别 *****" << endl;
cout << "***** 3.年龄 *****" << endl;
cout << "***** 4.电话*****" << endl;
cout << "***** 5.住址 *****" << endl;
cout << "******************" << endl;
int select = 0;
cin >> select;
switch (select)
{
case 1:
{
//姓名
string name;
cout << "请输入修改后的姓名:" << endl;
cin >> name;
book->personArray[ret].m_name = name;
break;
}
case 2:
{
cout << "请输入性别:" << endl;
cout << "1------男" << endl;
cout << "2------女" << endl;
//性别
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
book->personArray[ret].m_sex = sex;
break;
}
else
{
cout << "输入有误,请重新输入!" << endl;
}
}
}
case 3:
{
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age > 0 && age < 150)
{
book->personArray[ret].m_age = age;
break;
}
else
{
cout << "输入有误,请重新输入!" << endl;
}
}
}
case 4:
{
//电话
cout << "请输入电话:" << endl;
string phone;
cin >> phone;
book->personArray[ret].m_phone = phone;
}
case 5:
{
cout << "请输入家庭住址:" << endl;
string address;
cin >> address;
book->personArray[ret].m_address = address;
}
default:
break;
}
}
else
{
cout << "查无此人" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//清空联系人
void cleanPerson(books* book)
{
book->size = 0;
cout << "通讯录已清空" << endl; //将联系人数量置0
system("pause");//请按任意键继续
system("cls");//清屏操作
}
头文件
#include <iostream>
using namespace std;
//自定义联系人结构体
struct person
{
string m_name;
int m_sex;//1男 2女
int m_age;
string m_phone;
string m_address;
};
//通讯录结构体
#define MAX 1000//最大人数;注意define后面不写 分号
struct books
{
struct person personArray[MAX];//保存联系人的数组
int size;//当前联系人数量
};
//声明
void show();
//添加联系人
void addPerson(books* book);
//显示联系人
void showPerson(books* book);
//判断某人是否在通讯录中(按名字判断)
int Exist(books* book, string name);
//删除联系人
void deletePerson(books* book);
//查找联系人
void findPerson(books* book);
//修改联系人
void modifyPerson(books* book);
//清空联系人
void cleanPerson(books* book);