/*******************************************************************************
此小项目涵盖了C++的基本内容,是对基础知识的一次检验,也是C++第一阶段的结束标志
我们将开启第二阶段的学习,不要放弃,一起努力!
author:HCC
time:2021/5/22
致敬:袁隆平爷爷,袁老于2021/5/22 13:07分在湘雅医院逝世,无比哀痛,愿袁老走好!
********************************************************************************/
#include<iostream>
using namespace std;
#include<string>
#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;
};
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(struct Addressbooks* abs)
{
if (abs->m_Size == MAX)
{
cout << "通讯录已满,无法添加" << endl;
return ;
}
else
{
cout << "请输入姓名:";
string name;
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
cout << "请输入性别:" << endl;
cout << "1==男 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 << "请输入年龄:";
cin >> abs->personArray[abs->m_Size].m_Age;
cout << endl;
cout << "请输入电话号码:";
cin >> abs->personArray[abs->m_Size].m_Phone;
cout << endl;
cout << "请输入地址:";
cin >> abs->personArray[abs->m_Size].m_Addr;
cout << endl;
abs->m_Size++;
cout << "添加成功" << endl;
system("pause");//请按任意键继续
system("cls");//清屏
}
}
//显示联系人
void showPerson(struct Addressbooks* abs)
{
if (abs->m_Size == 0)
{
cout << "通讯录为空" << endl;
return ;
}
else
{
for (int i = 0; i < abs->m_Size; i++)
{
cout << "姓名: " << abs->personArray[i].m_Name << "\t" << "年龄: " << abs->personArray[i].m_Age << "\t"
<< "性别: " << ((abs->personArray[i].m_Sex == 1) ? "男" : "女") << "\t" << "电话号码: " << abs->personArray[i].m_Phone
<< "\t"<< "住址: " << abs->personArray[i].m_Addr << endl;
}
}
}
//判断联系人是否存在
int isExist(struct Addressbooks* abs, string name)
{
for (int i = 0; i < abs->m_Size; i++)
{
if (abs->personArray[i].m_Name == name)
{
return i;
}
}
cout << "查无此人" << endl;
return -1;
}
//删除联系人
void deletePerson(struct Addressbooks* abs)
{
cout << "请输入要删除联系人的姓名:";
string name;
cin >> name;
int ret = isExist(abs,name);
if (ret != -1)
{
for (int i = ret; i < abs->m_Size;i++)
{
abs->personArray[i] = abs->personArray[i + 1];//数据整体迁移,相当于删除
}
cout << "删除成功" << endl;
abs->m_Size--;
}
system("pause");//请按任意键继续
system("cls");//清屏
}
//查找联系人
void findPerson(struct Addressbooks* abs)
{
cout << "请输入要查找联系人的姓名:";
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)//该联系人存在
{
cout << "姓名: " << abs->personArray[ret].m_Name << "\t" << "年龄: " << abs->personArray[ret].m_Age << "\t"
<< "性别: " << ((abs->personArray[ret].m_Sex == 1) ? "男" : "女") << "\t" << "电话号码: " << abs->personArray[ret].m_Phone
<< "\t" << "住址: " << abs->personArray[ret].m_Addr << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏
}
//修改联系人
void modifyPerson(struct Addressbooks* abs)
{
cout << "请输入要修改联系人的姓名:";
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)//该联系人存在
{
cout << "被修改人姓名修改为:";
cin>>abs->personArray[ret].m_Name;
cout << "请输入性别:" << endl;
cout << "1==男 2==女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[ret].m_Sex = sex;
break;
}
cout << "输入错误,请重新输入!" << endl;
}
cout << "请输入年龄:";
cin >> abs->personArray[ret].m_Age;
cout << endl;
cout << "请输入电话号码:";
cin >> abs->personArray[ret].m_Phone;
cout << endl;
cout << "请输入地址:";
cin >> abs->personArray[ret].m_Addr;
cout << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏
}
void clearPerson(struct Addressbooks* abs)
{
cout << "你确定要清空所有联系人吗?(是/否)"<<endl;
string tmp;
cin >> tmp;
if (tmp == "是")
{
abs->m_Size = 0;//逻辑上清零
cout << "通讯录已清空" << endl;
system("pause");//请按任意键继续
system("cls");//清屏
}
else
return;
}
int main()
{
int select = 0;
struct Addressbooks abs;
abs.m_Size = 0;
while (true)
{
showMenu();
cout << "\n\n";
cout << "请输入一个提示的整数:" << endl;
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.清空联系人
clearPerson(&abs);
break;
case 0: //0.退出通讯录
cout << "欢迎下次使用!" << endl;
system("pause");
return 0;
break;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
cout << "\n\n";
}
system("pause");
return 0;
}