#include<string>
#include<iostream>
using namespace std;
#define MAX 1000
struct Person
{
string m_name;
int m_sex;
int m_age;
string m_phone;
string m_addr;
};
struct addressbook
{
struct Person perbook[MAX];
int num;
};
//caidan
void showlist()
{
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;
}
//1.tianjia
void addperson(struct addressbook *p)
{
if(p->num>=MAX)
return;
cout<<"请输入姓名:"<<endl;
cin>>p->perbook[p->num].m_name;
cout<<"请输入性别:"<<endl;
cout<<"1------男"<<endl;
cout<<"2------女"<<endl;
int sex;
while(true){
cin>>sex;
if(sex==1||sex==2)
break;
}
p->perbook[p->num].m_sex=sex;
cout<<"请输入年龄:"<<endl;
int age;
while(true)
{
cin>>age;
if(age>0&&age<=100)
break;
}
p->perbook[p->num].m_age=age;
cout<<"请输入联系电话:"<<endl;
cin>>p->perbook[p->num].m_phone;
cout<<"请输入家庭地址:"<<endl;
string address;
cin>>address;
p->perbook[p->num].m_addr=address;
p->num++;
cout<<"添加成功!"<<endl;
system("pause");
system("cls");
}
//2.xianshi
void showallperson(struct addressbook *p)
{
if(p->num==0)
cout<<"当前记录为空!"<<endl;
else
for(int i=0;i<p->num;i++)
{
cout<<"姓名 "<< p->perbook[i].m_name;
cout<<" 性别 "<< p->perbook[i].m_sex;
cout<<" 年龄 "<< p->perbook[i].m_age;
cout<<" 联系电话 "<< p->perbook[i].m_phone;
cout<<" 家庭住址 "<< p->perbook[i].m_addr<<endl;
}
system("pause");
system("cls");
}
//3.shanchu
int isexit(struct addressbook *p,string name)
{
for(int i=0;i<p->num;i++)
{
if(p->perbook[i].m_name==name)
return i;
}
return -1;
}
void deleteperson(struct addressbook *p)
{
string name;
cout<<"请输入药删除的联系人::"<<endl;
cin>>name;
int sign=isexit(p, name);
if(sign==-1)
{
cout<<"查无此人!"<<endl;
}
else
{
if(sign!=p->num-1)
{
for(int i=sign;i<p->num-1;i++)
p->perbook[i] = p->perbook[i+1];
}
p->num--;
cout<<"删除成功!"<<endl;
}
system("pause");
system("cls");
}
//4.chazhao
void findperson(struct addressbook *p)
{
cout<<"请输入你要查找的联系人:"<<endl;
string name;
cin>>name;
int sign=isexit(p, name);
if(sign==-1)
cout<<"查无此人!"<<endl;
else
{
cout<<"姓名 "<< p->perbook[sign].m_name;
cout<<" 性别 "<< p->perbook[sign].m_sex;
cout<<" 年龄 "<< p->perbook[sign].m_age;
cout<<" 联系电话 "<< p->perbook[sign].m_phone;
cout<<" 家庭住址 "<< p->perbook[sign].m_addr<<endl;
cout<<"查询成功!"<<endl;
}
system("pause");
system("cls");
}
//xiugai
void changeperson(struct addressbook *p)
{
cout<<"请输入你要修改的联系人:"<<endl;
string name;
cin>>name;
int sign=isexit(p, name);
if(sign==-1)
cout<<"查无此人!"<<endl;
else
{
cout<<"请输入姓名:"<<endl;
cin>>p->perbook[sign].m_name;
cout<<"请输入性别:"<<endl;
cout<<"1------男"<<endl;
cout<<"2------女"<<endl;
int sex;
while(true){
cin>>sex;
if(sex==1||sex==2)
break;
}
p->perbook[sign].m_sex=sex;
cout<<"请输入年龄:"<<endl;
int age;
while(true)
{
cin>>age;
if(age>0&&age<=100)
break;
}
p->perbook[sign].m_age=age;
cout<<"请输入联系电话:"<<endl;
cin>>p->perbook[sign].m_phone;
cout<<"请输入家庭地址:"<<endl;
string address;
cin>>address;
p->perbook[sign].m_addr=address;
}
system("pause");
system("cls");
}
//6.qingkong
void cleanaddressbook(struct addressbook *p)
{
p->num=0;
cout<<"通讯录已经清空"<<endl;
system("pause");
system("cls");
}
int main()
{
struct addressbook abs;
abs.num=0;
int select=0;
while(1)
{
showlist();
cin>>select;
switch (select) {
case 1:
addperson(&abs);
break;
case 2:
showallperson(&abs);
break;
case 3:
deleteperson(&abs);
break;
case 4:
findperson(&abs);
break;
case 5:
break;
case 6:
break;
case 0:
cout<<"欢迎再次使用!"<<endl;
system("pause");
return 0;
break;
default:
cout<<"请输入正确的数字!"<<endl;
break;
}
}
return 0;
}