马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <iostream>
#define MAX 1000
using namespace std;
//封装函数显示界面 如 void showMenu();
//在main中调用被封装好的函数
//设计联系人结构体
struct Person
{
string m_Name;
int m_Sex; //1是男 2是女
int m_Age;
string m_Phone;
string m_Stress;
};
//设计通讯录结构体
struct AddressBooks
{
//通讯录中人的联系人的数组
struct Person personArray[MAX];
//通讯录中当前记录的联系人的个数
int m_Size;
};
//显示菜单
void showMenu();
//1.添加联系人
void addPerson(struct AddressBooks* abs);
//2.显示所有联系人信息
void showPerson(struct AddressBooks* abs);
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:
break; //删除联系人
case 4:
break; //查找联系人
case 5:
break; //修改联系人
case 6:
break; //清空联系人
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break; //退出通讯录
default:
break;
}
}
//菜单调用
system("pause");
return 0;
}
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
{
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
//性别
int sex = 0;
cout << "请输入性别:" << endl;
cout << "1 ---- 男" << endl;
cout << "2 ---- 女" << endl;
while (true)
{
//如果是1或者2,可以退出循环,如果输入有误,必须重新输入
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//年龄
cout << "请输入年龄:" << endl;
int age;
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 address;
cin >> address;
abs->personArray[abs->m_Size].m_Stress = address;
//更新通讯录人数
abs->m_Size++;
cout << "添加成功!" << endl;
system("pause");
system("cls");
}
}
void showPerson(struct AddressBooks * abs)
{
//判断通讯录中人数是否为0,如果为零,提示记录为空,如果不为零,显示记录的联系人信息
if (abs->m_Size == 0)
{
cout << "当前的记录为空!" << endl;
}
else
{
for (int i = 0; i < abs->m_Size; i++)
{
cout << "姓名:" << abs->personArray[i].m_Name << "\t";
cout << "性别:" << abs->personArray[i].m_Sex << "\t";
cout << "年龄:" << abs->personArray[i].m_Age << "\t";
cout << "电话:" << abs->personArray[i].m_Phone << "\t";
cout << "住址:" << abs->personArray[i].m_Stress << endl;
}
}
system("pause");
system("cls");
}
|