#include
#include
#define Max 1000
using namespace std;
//1、显示菜单界面
void showMenu()
{
cout << "***************************" << endl;
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;
cout << "***************************" << endl;
}
//设计联系人结构体
struct person
{
string m_Name;
int m_Sex; //输入1代表女,0代表男,
int m_Age; //年龄
string m_Phone; //电话号码
string m_Addr; //地址
};
//设计通讯录结构体
struct addressbooks
{
//通讯录中保存联系人数组
struct person personArray[Max];
//通讯录当中记录的联系人个数
int m_Size;
};
//显示联系人
void showPerson(addressbooks* abs)
{
//判断通讯录中人数是否为0
if (abs->m_Size == 0)
{
cout << "记录为空" << endl;
}
else
{
for (int i=0;im_Size;i++)
{
cout << "姓名:" << abs->personArray[i].m_Name << "t";
cout << "年龄:" << abs->personArray[i].m_Age << "t";
cout << "性别:"<< (abs->personArray[i].m_Sex == 1 ? "男":"女") << "t";
cout << "地址:" << abs->personArray[i].m_Addr << "t";
cout << "电话号码:" << abs->personArray[i].m_Phone << endl;
}
}
system("pause");
system("cls");//清理屏幕的效果
}
//添加联系人
void addPerson(addressbooks * abs)
{
//判断通讯录是否满了
if (abs->m_Size == Max)
{
cout << "通讯录已满,无法继续添加" << endl;
return;
}
else
{
//添加具体联系人
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[abs->m_Size].m_Name=name;
//电话
cout << "请输入电话号码:" << endl;
string phone;
cin >> phone;
abs->personArray[abs->m_Size].m_Phone = phone;
//性别
cout << "请输入性别:" << endl;
cout << "1------男" << endl;
cout << "0------女" << endl;
int sex=0;
while (true)
{
//如果输入的是1或者0,可以退出循环,否则重新输入
cin >> sex;
if (sex == 1 || sex == 0)
{
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//年龄
cout << "请输入年龄:"<> age;
abs->personArray[abs->m_Size].m_Age = age;
//住址
cout << "请输入家庭地址:" << endl;
string address;
cin >> address;
abs->personArray[abs->m_Size].m_Addr = address;
//更新
abs->m_Size++;
cout << "添加成功"<m_Size; i++)
{
//找到用户输入的姓名了
if (abs->personArray[i].m_Name == name)
{
return i;
}
}
return -1;
}
//删除
void deletePerson(addressbooks* abs)
{
cout << "请输入要删除的联系人" << endl;
string name;
cin >> name;
int ret = isExit(abs, name);
if (ret != -1)
{
for (int i = ret; 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(addressbooks* 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_Age << "t";
cout << "性别:" << (abs->personArray[ret].m_Sex == 1 ? "男" : "女") << "t";
cout << "地址:" << abs->personArray[ret].m_Addr << "t";
cout << "电话号码:" << abs->personArray[ret].m_Phone << endl;
}
else
{
cout << "查无此人" <> name;
int ret = isExit(abs, name);
if (ret != -1)//找到指定的联系人
{
//姓名
string name;
cout << "请输入要修改的人的姓名" << endl;
cin >> name;
abs->personArray[ret].m_Name = name;
//性别
cout << "请输入性别:" << endl;
cout << "1----男" << endl;
cout << "0----女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 0)
{
abs->personArray[ret].m_Sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//年龄
cout << "请输入年龄:" << endl;
int age = 0;
cin >> age;
abs->personArray[ret].m_Age = age;
//电话
cout << "请输入联系电话:" << endl;
string phone;
cin >> phone;
abs->personArray[ret].m_Phone = phone;
//家庭住址
cout << "请输入家庭住址:" << endl;
string addr;
cin >> addr;
abs->personArray[ret].m_Addr = addr;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");//清理屏幕的效果
}
//清空所有联系人
void cleanPerson(addressbooks *abs)
{
abs->m_Size = 0;//将当前记录的联系人的数量重置为零,做逻辑清空操作
cout << "清空成功" << endl;
system("pause");
system("cls");//清理屏幕的效果
}
int main()
{
//创建通讯录结构体变量
addressbooks abs;
//初始化通讯录中当前人员个数
abs.m_Size = 0;
//加人
int select = 0;//创建用户选择输入的变量
while (true) {
//菜单的调用
showMenu();
cin >> select;
switch (select)
{
case 1: //添加联系人
addPerson(&abs); //利用地址传递可以修饰实参
break;
case 2: //显示联系人
showPerson(&abs);
break;
case 3: //删除联系人
{//在case里面声明要加括号
cout << "请输入一个删除的人的名字:" << endl;
string name;
cin >> name;
if (isExit(&abs, name) == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "找到此人" << endl;
}
}
break;
case4: //查找联系人
findPerson(&abs);
break;
case 5: //修改联系人
modifyPerson(&abs);
break;
case 6: //清空联系人
cleanPerson(&abs);
break;
case 0: //退出通讯录 功能
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
}