需求:
1、通讯录的增删改查基本操作
2、清空通讯录
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>
#define MAX 1000
using namespace std;
//通讯录管理系统
// 人员结构体
struct Person
{
string P_name;
int P_sex;//1、男,2、女
int P_age;
string P_address;
int P_number;
};
//通讯录结构体
struct Address
{
struct Person Per[MAX];
int Address_num;//记录通讯录人数
};
//菜单界面
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;
}
//添加人员函数
void addPerson(Address* abs)
{
//先判断是否满员
if (abs->Address_num == MAX)
{
cout << "通讯录已经满了!" << endl;
return;
}
else
{
//输入姓名
string name;
cout << "请输入人员姓名: ";
cin >> name;
abs->Per[abs->Address_num].P_name = name;
//输入性别
int sex = 0;
cout << "请输入人员性别(1--男,2--女): ";
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->Per[abs->Address_num].P_sex = sex;
break;
}
cout << "输入有误,请重新输入:";
}
//输入年龄
int age;
cout << "请输入人员年龄: ";
cin >> age;
abs->Per[abs->Address_num].P_age = age;
//输入手机号
int phnum;
cout << "请输入人员手机号: ";
cin >> phnum;
abs->Per[abs->Address_num].P_number = phnum;
//输入家庭住址
string address;
cout << "请输入家庭住址: ";
cin >> address;
abs->Per[abs->Address_num].P_address = address;
}
abs->Address_num++;
cout << "添加成功!" << endl;
system("pause");
system("cls");
}
//显示人员信息函数
void printPerson(Address* abs)
{
if (abs -> Address_num == 0)
{
cout << "当前通讯录为空!"<<endl;
return;
}
else
{
for (int i = 0; i < abs -> Address_num; i++)
{
cout << "姓名:" << abs->Per[i].P_name
<< " 性别:" << (abs->Per[i].P_sex==1?"男":"女")
<< " 年龄:" << abs->Per[i].P_age
<< " 手机号:" << abs->Per[i].P_number
<< " 家庭住址:" << abs->Per[i].P_address<<endl;
}
}
system("pause");
system("cls");
}
//以人名判断该人员是否存在函数
//参数1 通讯录 参数2 对比的姓名
int isExist(Address* abs, string name)
{
for (int i = 0; i < abs->Address_num; i++)
{
if (abs->Per[i].P_name == name)
{
return i;//找到了 返回该人员在数组中的下标编号
}
}
return -1;
}
//定义删除人员函数
void deletePerson(Address* abs)
{
string name;
cin >> name;
//接收“存在函数”的返回值
int ret = isExist(abs,name);
if (ret != -1)
{
for (int i = 0; i < abs->Address_num; i++)
{
abs->Per[i] = abs->Per[i + 1];
}
abs->Address_num--;
cout << "删除成功!" << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
//定义以人员姓名查找人员函数
void findPerson(Address* abs)
{
string name;
cout << "请输入想查找的人员姓名:";
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
cout << "姓名:" << abs->Per[ret].P_name
<< " 性别:" << (abs->Per[ret].P_sex == 1 ? "男" : "女")
<< " 年龄:" << abs->Per[ret].P_age
<< " 手机号:" << abs->Per[ret].P_number
<< " 家庭住址:" << abs->Per[ret].P_address << endl;
}
else
{
cout << "查无此人!" << endl;
}
}
//定义修改人员信息函数
void changePerson(Address* abs)
{
string name;
cout << "请输入想查找的人员姓名:";
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
cout << "请输入想修改的人员姓名:";
cin >> name;
abs->Per[ret].P_name = name;
cout << "请输入想修改的人员性别:";
int sex;
while (true)
{
cin >> sex;
if (sex == 1 ||sex== 2)
{
abs->Per[ret].P_sex = sex;
break;
}
cout << "输入的性别有误,请重新输入" << endl;
}
cout << "请输入想修改的人员年龄:";
int age;
cin >> age;
abs->Per[ret].P_age= age;
cout << "请输入想修改的人员手机号:";
int number;
cin >> number;
abs->Per[ret].P_number = number;
cout << "请输入想修改的人员地址:";
string address;
cin >> address;
abs->Per[ret].P_address = address;
}
}
//定义清空函数
void clear(Address *abs)
{
abs->Address_num = 0;
cout << "通讯录已清空!" << endl;
system("pause");
system("cls");
}
int main()
{
//菜单调用
int select = 0;
Address abs;
abs.Address_num = 0;
while (true)
{
showmenu();
cin >> select;
switch (select)
{
case 1://添加联系人
addPerson(&abs);
break;
case 2://显示联系人
printPerson(&abs);
break;
case 3://修改联系人
changePerson(&abs);
break;
case 4://删除联系人
cout << "请输入想删除的人员姓名: ";
deletePerson(&abs);
break;
case 5://查找联系人
findPerson(&abs);
break;
case 6://清空联系人
clear(&abs);
break;
case 0://退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}