#include<iostream>
using namespace std;
#include<string>
//#define MAXSIZE 1000
int const MAXSIZE = 1000;
struct Person
{
string Name;
int m_Sex;
int m_Age;
string m_Phone;
string m_Addr;
};
struct Addressbooks
{
Person* personArray;
int m_Size;
};
void showMEnu();
void InitPerson(Addressbooks* abs);
void AddPerson(Addressbooks* abs);
void ShowPerson(Addressbooks abs);
int isExist(Addressbooks abs, string name, int age);
int isExist2(Addressbooks abs, string name);
void DeletePerson(Addressbooks* abs);
void FindPerson(Addressbooks abs);
void FixPerson(Addressbooks* abs);
void ClearPerson(Addressbooks* abs);
void showMEnu()
{
cout << "1.添加联系人" << endl;
cout << "2.显示联系人" << endl;
cout << "3.删除联系人" << endl;
cout << "4.查找联系人" << endl;
cout << "5.修改联系人" << endl;
cout << "6.清空联系人" << endl;
cout << "0.退出通讯录" << endl;
}
void InitPerson(Addressbooks *abs)
{
abs->personArray = new Person[MAXSIZE]; //数据存放到堆区
abs->m_Size = 0; //通讯录人数初始为0
}
void AddPerson(Addressbooks* abs)
{
if (abs->m_Size==MAXSIZE)
{
cout << "通讯录已满" << endl;
return;
}
else
{
cout << "请输入姓名" << endl;
cin>>abs->personArray[abs->m_Size].Name;
cout << "请输入性别" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
int sex;
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;
while (true)
{
cin >> age;
if (age<=100&&age>=0)
{
abs->personArray[abs->m_Size].m_Age = age;
break;
}
cout << "您输入的有误,请重新输入" << endl;
}
cout << "请输入联系电话" << endl;
cin>>abs->personArray[abs->m_Size].m_Phone;
cout << "请输入家庭住址" << endl;
cin>>abs->personArray[abs->m_Size].m_Addr ;
cout << "您输入成功" << endl;
abs->m_Size++;
system("pause");
system("cls");
}
}
void ShowPerson(Addressbooks abs)
{
if (abs.m_Size==0)
{
cout << "没有联系人记录" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < abs.m_Size; i++)
{
cout << "姓名:" << abs.personArray[i].Name<< "\t";
cout << "性别:" << (abs.personArray[i].m_Sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs.personArray[i].m_Age << "\t";
cout << "住址:" << abs.personArray[i].m_Addr << "\t";
cout << "电话号码:" << abs.personArray[i].m_Phone
<< endl;
}
system("pause");
system("cls");
}
int isExist(Addressbooks abs, string name,int age)
{
for (int i=0;i<abs.m_Size;i++)
{
if (abs.personArray[i].Name == name && abs.personArray[i].m_Age==age)
return i + 1;
}
return 0;
}
int isExist2(Addressbooks abs, string name)
{
for (int i = 0; i < abs.m_Size; i++)
{
if (abs.personArray[i].Name == name)
return i + 1;
}
return 0;
}
void DeletePerson(Addressbooks *abs)
{
string name;
cout << "请输入要删除的联系人" << endl;
cin >> name;
int age;
cout << "请输入要删除的联系人年龄" << endl;
cin >> age;
int ret=isExist(*abs, name,age);
if (ret == 0)
{
cout << "查无此人" << endl;
}
else
{
for (int i=ret-1;i<abs->m_Size;i++)
{
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_Size--;
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
void FindPerson(Addressbooks abs)
{
string name;
cout << "请输入要查找的人的姓名" << endl;
cin >> name;
int ret = isExist2(abs, name);
if (ret)
{
cout << "姓名:" << abs.personArray[ret-1].Name << "\t";
cout << "性别:" << (abs.personArray[ret-1].m_Sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs.personArray[ret-1].m_Age << "\t";
cout << "住址:" << abs.personArray[ret-1].m_Addr << "\t";
cout << "电话号码:" << abs.personArray[ret-1].m_Phone
<< endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void FixPerson(Addressbooks *abs)
{
string name;
cout << "请输入要输入的联系人姓名" << endl;
cin >> name;
int age;
cout << "请输入要输入的联系人的年龄" << endl;
cin >> age;
int ret = isExist(*abs, name, age);
if (ret)
{
cout << "请输入姓名" << endl;
cin >> abs->personArray[ret-1].Name;
cout << "请输入性别" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
int sex;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[ret-1].m_Sex = sex;
break;
}
cout << "您输入的有误,请重新输入" << endl;
}
cout << "请输入年龄" << endl;
int age = 0;
while (true)
{
cin >> age;
if (age <= 100 && age >= 0)
{
abs->personArray[ret-1].m_Age = age;
break;
}
cout << "您输入的有误,请重新输入" << endl;
}
cout << "请输入联系电话" << endl;
cin >> abs->personArray[ret-1].m_Phone;
cout << "请输入家庭住址" << endl;
cin >> abs->personArray[ret-1].m_Addr;
cout << "您输入成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void ClearPerson(Addressbooks *abs)
{
if (abs->m_Size==0)
{
cout << "通讯录已经清空" << endl;
}
else
{
int ret;
cout << "当前数据人数为: " << abs->m_Size << "是否要清空?(1:是 0:否)" << endl;
cin >> ret;
if (ret)
{
abs->m_Size = 0;
cout << "通讯录已经清空" << endl;
}
else
{
cout << "保留数据" << endl;
}
}
system("pause");
system("cls");
}
int main()
{
Addressbooks abs;
InitPerson(&abs);
int select = 0;
while (true)
{
showMEnu();
cin >> select;
switch (select)
{
case 1:
AddPerson(&abs);
break;
case 2:
ShowPerson(abs);
break;
case 3:
/*{
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret)
cout << "找到此人" << endl;
else
cout << "查无此人" << endl;
}*/
DeletePerson(&abs);
break;
case 4:
FindPerson(abs);
break;
case 5:
FixPerson(&abs);
break;
case 6:
ClearPerson(&abs);
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
cout << "输入错误,请重新输入" << endl;
system("pause");
system("cls");
break;
}
}
return 0;
}