//菜单显示
void menu()
{
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;
}
case 0: //0、退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
功能描述:上限为1000人,联系人信息包括(姓名,性别,年龄,联系电话,家庭住址)
实现步骤:
//联系人结构体
struct Person
{
string name;
int sex;//1 男 2 女
int age;
string phone;
string addr;
};
//通讯录结构体
#define Max 1000
struct Addressbooks
{
struct Person Array[Max];//最大上限人数
int m_size;//通讯录中人数
};
Addressbooks abs;//创建通讯录结构体变量
abs.m_size = 0;//初始化人数
//添加联系人
void addPerson(Addressbooks* abs)
{
if (abs->m_size == Max)
{
cout << "联系人以达上限,无法添加" << endl;//判断通讯录是否满员
return;
}
else
{
//名字
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->Array[abs->m_size].name = name;
//性别
cout << "请输入性别:" << endl;
cout << "1代表男" << endl;
cout << "2代表女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->Array[abs->m_size].sex = sex;
break;
}
cout << "输入错误,请重新输入:" << endl;
}
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
cin >> age;
abs->Array[abs->m_size].age = age;
//联系电话
cout << "请输入联系电话:" << endl;
string number;
cin >> number;
abs->Array[abs->m_size].phone = number;
//家庭住址
cout << "请输入家庭住址:" << endl;
string addr;
cin >> addr;
abs->Array[abs->m_size].addr = addr;
//更新通讯录人数
abs->m_size++;
cout << "添加成功!" << endl;
system("pause");//按任意键继续操作
system("cls");//清屏操作
}
}
思路:判断当前通讯录人数是否为空,不为空,直接显示通讯录信息
//显示联系人
void showPerson(Addressbooks* abs)
{
//判断人数
if (abs->m_size == 0)
{
cout << "您还没有添加任何一个联系人!" << endl;
}
else
{
for (int i = 0; i < abs->m_size; i++)
{
cout << "姓名:" << abs->Array[i].name << "\t";
cout << "性别:" << (abs->Array[i].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->Array[i].age << "\t";
cout << "电话号码:" << abs->Array[i].phone << "\t";
cout << "家庭住址:" << abs->Array[i].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->Array[i].name == name)
{
return i;
}
}
return -1;
}
void deletePerson(Addressbooks* abs)
{
cout << "请输入需要删除的联系人:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
for (int i = ret; i < abs->m_size; i++)
{
//数据前移,通过覆盖需要删除的数据来实现删除功能
abs->Array[i] = abs->Array[i + 1];
}
abs->m_size--;//更新通讯录中的人员数
cout<< "删除成功!" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
功能描述:按照姓名查找指定联系人信息
实现步骤:
//查找指定联系人
void findPerson(Addressbooks* abs)
{
cout<< "请输入需要查找的姓名:" << endl;
string name;
cin >> name;
int ret = isExist(abs,name);
if (ret != -1)
{
cout << "姓名:" << abs->Array[ret].name << "\t";
cout << "性别:" <<(abs->Array[ret].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->Array[ret].age << "\t";
cout << "电话号码:" << abs->Array[ret].phone << "\t";
cout << "家庭住址:" << abs->Array[ret].addr<< endl;
}
else
{
cout<< "没有找到此人!" << endl;
}
system("pause");
system("cls");
}
实现思路:查找用户输入的联系人,查找成功进行修改操作,查找失败提示查无此人
//修改指定联系人信息
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->Array[ret].name = name;
//性别
cout << "请输入性别:" << endl;
cout << "1代表男" << endl;
cout << "2代表女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->Array[abs->m_size].sex = sex;
break;
}
cout << "输入错误,请重新输入:" << endl;
}
//年龄
int age = 0;
cout << "请输入年龄:" << endl;
cin >> age;
abs->Array[ret].age = age;
//电话
string number;
cout << "请输入电话号码:" << endl;
cin >> name;
abs->Array[ret].phone = number;
//家庭住址
string addr;
cout << "请输入家庭住址:" << endl;
cin >> addr;
abs->Array[ret].addr = addr;
cout << "修改成功!" << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
功能描述:情况通讯录中所有信息
实现步骤:
//清空所有联系人
void clean(Addressbooks* abs)
{
abs->m_size = 0;
cout << "通讯录已经清空" << endl;
system("pause");
system("cls");
}
#include
#include
using namespace std;
//联系人结构体
struct Person
{
string name;
int sex;//1 男 2 女
int age;
string phone;
string addr;
};
//通讯录结构体
#define Max 1000
struct Addressbooks
{
struct Person Array[Max];//最大上限人数
int m_size;//通讯录中人数
};
//添加联系人
void addPerson(Addressbooks* abs)
{
if (abs->m_size == Max)
{
cout << "联系人以达上限,无法添加" << endl;//判断通讯录是否满员
return;
}
else
{
//名字
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->Array[abs->m_size].name = name;
//性别
cout << "请输入性别:" << endl;
cout << "1代表男" << endl;
cout << "2代表女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->Array[abs->m_size].sex = sex;
break;
}
cout << "输入错误,请重新输入:" << endl;
}
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
cin >> age;
abs->Array[abs->m_size].age = age;
//练习电话
cout << "请输入联系电话:" << endl;
string number;
cin >> number;
abs->Array[abs->m_size].phone = number;
//家庭住址
cout << "请输入家庭住址:" << endl;
string addr;
cin >> addr;
abs->Array[abs->m_size].addr = addr;
//更新通讯录人数
abs->m_size++;
cout << "添加成功!" << endl;
system("pause");//按任意键继续操作
system("cls");//清屏操作
}
}
//显示联系人
void showPerson(Addressbooks* abs)
{
//判断人数
if (abs->m_size == 0)
{
cout << "您还没有添加任何一个联系人!" << endl;
}
else
{
for (int i = 0; i < abs->m_size; i++)
{
cout << "姓名:" << abs->Array[i].name << "\t";
cout << "性别:" << (abs->Array[i].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->Array[i].age << "\t";
cout << "电话号码:" << abs->Array[i].phone << "\t";
cout << "家庭住址:" << abs->Array[i].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->Array[i].name == name)
{
return i;
}
}
return -1;
}
void deletePerson(Addressbooks* abs)
{
cout << "请输入需要删除的联系人:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
for (int i = ret; i < abs->m_size; i++)
{
//数据前移,通过覆盖需要删除的数据来实现删除功能
abs->Array[i] = abs->Array[i + 1];
}
abs->m_size--;//更新通讯录中的人员数
cout<< "删除成功!" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//查找指定联系人
void findPerson(Addressbooks* abs)
{
cout<< "请输入需要查找的姓名:" << endl;
string name;
cin >> name;
int ret = isExist(abs,name);
if (ret != -1)
{
cout << "姓名:" << abs->Array[ret].name << "\t";
cout << "性别:" <<(abs->Array[ret].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->Array[ret].age << "\t";
cout << "电话号码:" << abs->Array[ret].phone << "\t";
cout << "家庭住址:" << abs->Array[ret].addr<< endl;
}
else
{
cout<< "没有找到此人!" << endl;
}
system("pause");
system("cls");
}
//修改指定联系人信息
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->Array[ret].name = name;
//性别
cout << "请输入性别:" << endl;
cout << "1代表男" << endl;
cout << "2代表女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->Array[abs->m_size].sex = sex;
break;
}
cout << "输入错误,请重新输入:" << endl;
}
//年龄
int age = 0;
cout << "请输入年龄:" << endl;
cin >> age;
abs->Array[ret].age = age;
//电话
string number;
cout << "请输入电话号码:" << endl;
cin >> name;
abs->Array[ret].phone = number;
//家庭住址
string addr;
cout << "请输入家庭住址:" << endl;
cin >> addr;
abs->Array[ret].addr = addr;
cout << "修改成功!" << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
//清空所有联系人
void clean(Addressbooks* abs)
{
abs->m_size = 0;
cout << "通讯录已经清空" << endl;
system("pause");
system("cls");
}
//菜单显示
void menu()
{
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)
{
menu();
cin >> select;
switch (select)
{
case 1: //1、添加联系人
addPerson(&abs);//地址传递可修饰实参
break;
case 2: // 2、显示联系人
showPerson(&abs);
break;
case 3: //3、删除联系人
deletePerson(&abs);
break;
case 4: //4、查找联系人
findPerson(&abs);
break;
case 5: //5、修改联系人
modifyPerson(&abs);
break;
case 6: //6、清空联系人
clean(&abs);
break;
case 0: //0、退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
}