#include <iostream>
using namespace std;
#include <string>
#define Max 2000
//封装函数显示界面
void showMenu() {
cout << "############################" << endl;
cout << "####\t1、添加联系人\t####" << endl;
cout << "####\t2、显示联系人\t####" << endl;
cout << "####\t3、删除联系人\t####" << endl;
cout << "####\t4、查找联系人\t####" << endl;
cout << "####\t5、修改联系人\t####" << endl;
cout << "####\t6、清空联系人\t####" << endl;
cout << "####\t0、退出通讯录\t####" << endl;
cout << "############################"<< endl;
}
//创建联系人的结构体
struct Person {
string m_Name; //姓名
int m_Sex; //性别
int m_Age; //年龄
string m_Phone; //电话
string m_Addr; //地址
};
//创建通讯录的结构体
struct Addressboks {
//通讯录中保持的联系人数组
Person personArray[Max];
//通讯录中当前记录的联系人个数
int m_Size;
};
void add_Person(Addressboks* abs) {
//判断通讯录是否满
if (abs->m_Size >= Max) {
cout << "通讯录已满,无法添加!" << endl;
}
else
{
//添加具体联系人
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
//性别
int sex = 0;
cout << "请输入性别(1为男性,2为女性):" << endl;
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 adder;
cin >> adder;
abs->personArray[abs->m_Size].m_Addr = adder;
//更新人数
abs->m_Size++;
cout << "添加联系人成功!" << endl;
system("pause");//请按任意键继续
system("cls");//清屏操作
}
}
//显示联系人
void show_Person(Addressboks* abs) {
//判断通讯录是否为0
if (abs->m_Size == 0) {
cout << "当前通讯录人数为0!" << 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
int isExit(Addressboks* 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(Addressboks* abs) {
cout << "请输入您要删除的联系人:" << endl;
string name;
cin >> name;
int ret = isExit(abs, name);
if (ret != -1) {
for (int i = 0; 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(Addressboks* abs) {
cout << "请输入要查找的联系人的姓名:" << endl;
string name;
cin >> name;
int ret = isExit(abs, name);
if (ret != -1) {
cout << "姓名:" << abs->personArray[ret].m_Name << "\t";
cout << "性别:" << (abs->personArray[ret].m_Sex == 1 ? "男" : "女") << "\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 modiyPerson(Addressboks* abs) {
cout << "请输入要修改的联系人:" << endl;
string name;
cin >> name;
int ret = isExit(abs, name);
if (ret != -1) {
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[ret].m_Name = name;
//性别
int sex;
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 addr;
cout << "请输入家庭地址:" << endl;
cin >> phone;
abs->personArray[ret].m_Addr = addr;
}
else {
cout << "未到此人!" << endl;
}
}
//清空联系人
void clearPerson(Addressboks* abs) {
cout << "是否真的清空通讯录!(请输入Y:确定,N:再想想)"<< endl;
string f;
cin >> f;
if (f == "Y") {
abs->m_Size = 0;//将当前记录联系人数量置为0,做逻辑上的清空
cout << "通讯录以清空!" << endl;
}
}
int main() {
int select = 0;//创建用户选择的变量
//创建通讯录结构体变量
Addressboks abs;
abs.m_Size = 0;//初始化通讯录中人的个数
while (true)
{
//菜单的调用
showMenu();
cin >> select;
switch (select) {
case 1://1、添加联系人
add_Person(&abs);//利用地址传递,可以修改实参
break;
case 2://2、显示联系人
show_Person(&abs);
break;
case 3: //3、删除联系人
deletePerson(&abs);
break;
case 4://4、查找联系人
findPerson(&abs);
break;
case 5://5、修改联系人
modiyPerson(&abs);
break;
case 6://6、清空联系人
break;
case 0://0、退出通讯录
cout << "欢迎下次使用!!!" << endl;
return 0;
default:
break;
}
}
}
|