#include<iostream>
using namespace std;
#include<string>
#define MAX 1000
struct people
{
string s_name;
int s_sex;
int s_age;
string s_number;
string s_address;
};
struct Addressbook
{
struct people peopleArray[MAX];
int m_size;
};
void addperson(Addressbook* abs)
{
if (abs->m_size == MAX)
{
cout << "通讯录里面的人员已满" << endl;
}
string name;
cout << "请输入需要添加的联系人" << endl;
cin >> name;
abs->peopleArray[abs->m_size].s_name = name;
int sex;
cout << "请输入添加的人的性别 "<<endl<<"1为男 2为女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->peopleArray[abs->m_size].s_sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
int age;
cout << "请输入添加的人的年龄 " << endl;
cin >> age;
abs->peopleArray[abs->m_size].s_age = age;
string address;
cout << "请输入添加的人的地址" << endl;
cin >> address;
abs->peopleArray[abs->m_size].s_address = address;
string number;
cout << "请输入添加的人的电话号" << endl;
cin >> number;
abs->peopleArray[abs->m_size].s_number = number;
abs->m_size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
void showperson(Addressbook* abs)
{
if (abs->m_size == 0)
{
cout << "里面为空" << endl;
}
else
{
for (int i = 0; i < abs->m_size; i++)
{
cout << "姓名:" << abs->peopleArray[i].s_name<<"\t";
cout << "年龄:" << abs->peopleArray[i].s_age << "\t" ;
cout << "性别:" << (abs->peopleArray[i].s_sex == 1?"男":"女") << "\t";
cout << "地址:" << abs->peopleArray[i].s_address << "\t";
cout << "电话:" << abs->peopleArray[i].s_number<<endl;
}
}
system("pause");
system("cls");
}
int isExist(Addressbook* abs, string name)
{
for (int i = 0; i < abs->m_size; i++)
{
if (abs->peopleArray[i].s_name == name)
{
return i;
}
else
{
return -1;
}
}
}
void deleteperson(Addressbook* abs)
{
string name;
cout << "请输入需要删除的名字" << endl;
cin >> name;
int ret = isExist(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
}
else
{
for (int i = ret; i < abs->m_size; i++)
{
abs->peopleArray[i] = abs->peopleArray[i + 1];
}
abs->m_size--;
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
void findpeople(Addressbook* abs)
{
string name;
cout << "请输入需要找的联系人" << endl;
cin >> name;
int ret = isExist(abs, name);
if (ret!=-1)
{
cout << "姓名:" << abs->peopleArray[ret].s_name << "\t";
cout << "年龄:" << abs->peopleArray[ret].s_age << "\t";
cout << "性别:" << abs->peopleArray[ret].s_sex << "\t";
cout << "电话:" << abs->peopleArray[ret].s_number << "\t";
cout << "地址:" << abs->peopleArray[ret].s_address << "\t";
}
else
{
cout << "查无此人";
}
system("pause");
system("cls");
}
void modifypeople(Addressbook* abs)
{
string name;
cout << "请输入需要修改的姓名" << endl;
cin >> name;
isExist(abs, name);
int ret = isExist(abs,name);
if (ret != -1)
{
string name;
cout << "请输入需要修改成的姓名" << endl;
cin >> name;
abs->peopleArray[ret].s_name = name;
int age;
cout << "请输入需要修改成的年龄" << endl;
cin >> age;
abs->peopleArray[ret].s_age = age;
int sex;
cout << "请输入添加的人的性别 " << endl << "1为男 2为女" << endl;
cin >> sex;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->peopleArray[abs->m_size].s_sex = sex;
break;
}
else
cout << "输入有误,请重新输入" << endl;
}
abs->peopleArray[ret].s_sex = sex;
int number;
cout << "请输入需要修改成的电话号码" << endl;
cin >> number;
abs->peopleArray[ret].s_number = number;
int address;
cout << "请输入需要修改成的地址" << endl;
cin >> address;
abs->peopleArray[ret].s_address = address;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void cleanPenple(Addressbook* abs)
{
abs->m_size == 0;
cout << "通讯录已经清空"<<endl;
system("pause");
system("cls");
}
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;
}
int main()
{
Addressbook abs;
int selece = 0;
abs.m_size = 0;
while (true)
{
showMenu();
cout << "请输入执行的编号" << endl;
cin >> selece;
cout << endl;
switch(selece)
{
case 1:
addperson(&abs);
break;
case 2:
showperson(&abs);
break;
case 3:
deleteperson(&abs);
break;
case 4:
findpeople(&abs);
break;
case 5:
modifypeople(&abs);
break;
case 6:
cleanPenple(&abs);
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
}