黑马程序员里面的程序,照着写的
#include<iostream>
using namespace std;
#include<string>
#include<cmath>
#define maxsize 1000
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;
}
struct people
{
string name;
int age;
string sex;
string number;
string lives;
};
struct addressbook
{
int max;
people addressarray[maxsize];
};
void add_address(addressbook* a)
{
if(a->max==maxsize) {cout<<"记录已满!!"<<endl;return ;}
else
{
people p;
string name;int age;string number;string lives;string sex;
cout<<"请输入姓名:"<<endl;
cin>>name;
a->addressarray[a->max].name=name;
cout<<"请输入年龄:"<<endl;
cin>>age;
a->addressarray[a->max].age=age;
while(true)
{
cout<<"请输入性别:(“男”或“女”)"<<endl;
cin>>sex;
if(sex=="男"||sex=="女") {a->addressarray[a->max].sex=sex;break;}
else {cout<<"输入错误"<<endl;}
}
cout<<"请输入电话号码:"<<endl;
cin>>number;
a->addressarray[a->max].number=number;
cout<<"请输入家庭住址:"<<endl;
cin>>lives;
a->addressarray[a->max].lives=lives;
a->max++;
cout<<"添加成功!!"<<endl;
}
system("pause");
system("cls");
};
void show_person(addressbook* a)
{
if(a->max==0) {cout<<"记录为空!!"<<endl;system("pause");system("cls");return ;}
else
{
for(int i=0;i<a->max;i++)
{cout<<"姓名:"<<a->addressarray[i].name
<<" 年龄:"<<a->addressarray[i].age
<<" 性别:"<<a->addressarray[i].sex
<<" 电话号码:"<<a->addressarray[i].number
<<" 家庭住址:"<<a->addressarray[i].lives<<endl;}
}
system("pause");
system("cls");
};
int check_person(addressbook* a,string name)
{
for(int i=0;i<a->max;i++)
{
if(a->addressarray[i].name==name) {return i;}
}
return -1;
}
void delete_person(addressbook* a,string name)
{
int j=check_person(a,name);
if(j!=-1)
{
for(int i=j;i<a->max;i++)
{
a->addressarray[i]=a->addressarray[i+1];
}
cout<<"删除成功!!"<<endl;
a->max--;
}
else {cout<<"查无此人!!"<<endl;};
system("pause");
system("cls");
};
void clear_addressbook(addressbook* a)
{
int check;
cout<<"确定是否清空联系人?(输入0否定,输入1肯定)"<<endl;
cin>>check;
if(check==1) {a->max=0;cout<<"联系人已清空!!"<<endl;}
else {cout<<"已取消操作!!"<<endl;}
system("pause");
system("cls");
};
int main()
{
int select=0;
addressbook abs;
abs.max=0;
while(true)
{
showmenu();
cout<<"请输入选项:"<<endl;
cin>>select;
switch(select)
{case 1:add_address(&abs);
break;
case 2:show_person(&abs);
break;
case 3:
{
string name;
cout<<"请输入要删除的联系人姓名:"<<endl;
cin>>name;
delete_person(&abs,name);
}
break;
case 4:
{
cout<<"请输入要查找的联系人姓名:"<<endl;
string name;
cin>>name;
int ret=check_person(&abs,name);
if(ret!=-1)
{
cout<<"姓名:"<<abs.addressarray[ret].name
<<" 年龄:"<<abs.addressarray[ret].age
<<" 性别:"<<abs.addressarray[ret].sex
<<" 电话号码:"<<abs.addressarray[ret].number
<<" 家庭住址:"<<abs.addressarray[ret].lives<<endl;
}
else {cout<<"查无此人!!"<<endl;}
system("pause");
system("cls");
}
break;
case 5:
{
cout<<"请输入要查找的联系人姓名:"<<endl;
string name;
cin>>name;
int ret=check_person(&abs,name);
if(ret!=-1)
{
string name;int age;string number;string lives;string sex;
cout<<"请输入姓名:"<<endl;
cin>>name;
abs.addressarray[ret].name=name;
cout<<"请输入年龄:"<<endl;
cin>>age;
abs.addressarray[ret].age=age;
while(true)
{
cout<<"请输入性别:(“男”或“女”)"<<endl;
cin>>sex;
if(sex=="男"||sex=="女") {abs.addressarray[ret].sex=sex;break;}
else {cout<<"输入错误"<<endl;}
}
cout<<"请输入电话号码:"<<endl;
cin>>number;
abs.addressarray[ret].number=number;
cout<<"请输入家庭住址:"<<endl;
cin>>lives;
abs.addressarray[ret].lives=lives;
}
else {cout<<"查无此人!!"<<endl;}
system("pause");
system("cls");
}
break;
case 6:clear_addressbook(&abs);
break;
case 0:system("pause");return 0;
break;
default:cout<<"输入错误!!"<<endl;system("pause");system("cls");
break;
};
}
}