cout << " 性别:" << (abs->personArray[i].m_Sex == 1 ? "男":"女");
string sex;
sex==1 ? " 男”:"女";
#include<iostream>
using namespace std;
#include<string>
#define MAX 1000
//设计联系人结构体
struct Person
{
string m_Name;
int m_Sex; //1.男 2.女
int m_Age;
string m_phone;
string m_Addr;
};
//设计通讯录结构体
struct Addressbooks
{
struct Person personArray[MAX]; //通讯录联系人
int m_Size; //通讯录人数
};
void showMenu()
{
cout << "\t-------欢迎来到通讯录管理系统-------" << endl;
cout << "\t\t1.添加联系人" << endl;
cout << "\t\t2.显示联系人" << endl;
cout << "\t\t3.删除联系人" << endl;
cout << "\t\t4.查找联系人" << endl;
cout << "\t\t5.修改联系人" << endl;
cout << "\t\t6.清空联系人" << endl;
cout << "\t\t0.退出通讯录" << endl;
cout << "\t——————————————--------" << endl;
}
void addPerson(struct Addressbooks *abs)
{
if (abs->m_Size > 1000)
{
cout << "当前通讯录已满!" << endl;
return;
}
cout << "请输入新联系人姓名:";
string name;
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
cout << "请输入新联系人性别(1.男 / 2.女): ";
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
else
{
cout << "输入错误,请重新输入! 输入:" ;
}
}
cout << "请输入新联系人年龄:";
int age;
cin >> age;
abs->personArray[abs->m_Size].m_Age = age;
cout << "请输入新联系人电话:";
string phone;
cin >> phone;
abs->personArray[abs->m_Size].m_phone = phone;
cout << "请输入新联系人地址:";
string adder;
cin >> adder;
abs->personArray[abs->m_Size].m_Addr = adder;
abs->m_Size++;
cout << "添加成功!" << endl;
system("pause");
system("cls");
}
void showPerson(const struct Addressbooks* abs)
{
if (abs->m_Size == 0)
{
cout << "当前通讯录为空!" << endl;
}
else {
for (int i = 0; i < abs->m_Size; i++)
{
cout << "姓名:" << abs->personArray[i].m_Name;
//if (abs->personArray[i].m_Sex == 1)
//{
// cout << " 性别: 男";
//}
//if(abs->personArray[i].m_Sex ==2)
//{
// cout << " 性别: 女";
//}
cout << " 性别:" << (abs->personArray[i].m_Sex == 1 ? "男":"女");
cout << " 年龄:" << abs->personArray[i].m_Age;
cout << " 电话:" << abs->personArray[i].m_phone;
cout << " 住址:" << abs->personArray[i].m_Addr << endl;
}
}
system("pause");
system("cls");
}
//检测联系人是否存在
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; //此人存在,并返回在数组中的下标
}
}
return -1; //此人不存在
}
void deletePerson(struct Addressbooks* abs)
{
cout << "请输入的要删除的联系人姓名:";
string name;
cin >> name;
int j = isExist(abs, name);
if (j != (-1))
{
for (int i = j; 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(struct Addressbooks* abs)
{
cout << "请输入要查找人的姓名:";
string name;
cin >> name;
int i = isExist(abs, name);
if (i != (-1))
{
cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女");
cout << " 年龄:" << abs->personArray[i].m_Age;
cout << " 电话:" << abs->personArray[i].m_phone;
cout << " 住址:" << abs->personArray[i].m_Addr << endl;
}
else
{
cout << "当前通讯录中没有此人!" << endl;
}
system("pause");
system("cls");
}
void modifyPerson(struct Addressbooks* abs)
{
cout << "请输入要修改的联系人的姓名:";
string name;
cin >> name;
int i = isExist(abs,name);
if (i != (-1))
{
//性别 年龄 电话 住址
cout << "请输入修改的性别:";
int sex;
cin >> sex;
abs->personArray[i].m_Sex = sex;
cout << "请输入修改的年龄:";
int age;
cin >> age;
abs->personArray[i].m_Age = age;
cout << "请输入修改的电话:";
string phone;
cin >> phone;
abs->personArray[i].m_phone =phone;
cout << "请输入修改的住址:";
string adder;
cin >> adder;
abs->personArray[i].m_Addr =adder;
}
else
{
cout << "当前通讯录没有此人!" << endl;
}
system("pause");
system("cls");
}
void clearPerson(struct Addressbooks* abs)
{
cout << "确认要清空联系人吗?" << endl;
cout << "确认:请输入1" << endl;
cout << "否:请输入2" << endl;
int num = 0;
cin >> num;
if (num == 1)
{
abs->m_Size = 0;
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}
int main()
{
int select = 0;
//创建通讯录结构体变量
struct Addressbooks abs;
abs.m_Size = 0;
while (true)
{
showMenu();
cout << "请进行选择:";
cin >> select;
switch (select)
{
case 1: //1.添加联系人
addPerson(&abs);
break;
case 2: //2.显示联系人
showPerson(&abs);
break;
case 3: //3.删除联系人
//{
// cout << "请输入查找人的姓名:"; //测试isExist()函数
// string name;
// cin >> name;
// isExist(&abs, name);
//}
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;
return 0;
break;
}
}
system("pause");
return 0;
}