功能实现
#include<iostream>
#include<cstring>
using namespace std;
#define MAX 1000
struct Contact{
string name;
string gender;
int age;
string phone;
string address;
};
struct con{
struct Contact p[MAX];
int n=0;//通讯录人数
};
void init(){
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;
}
void add(struct con * c){
if(c->n>=MAX){
cout<<"联系人已满,添加失败!!!"<<endl;
}
int gen;
cout<<"请输入姓名:";
cin>>c->p[c->n].name;
cout<<"请输入性别(1--男,2--女):";
cin>>gen;
if(gen==1){
c->p[c->n].gender="男";
}else{
c->p[c->n].gender="女";
}
cout<<"请输入年龄:";
cin>>c->p[c->n].age;
cout<<"请输入联系电话:";
cin>>c->p[c->n].phone;
cout<<"请输入家庭住址:";
cin>>c->p[c->n].address;
cout<<"添加成功"<<endl;
c->n++;
}
void show(struct con * c){
int i;
if(c->n==0){
cout<<"联系人为空"<<endl;
}else{
for(i=0;i<c->n;i++)
cout<<"姓名:"<<c->p[i].name<<"\t性别:"<<c->p[i].gender<<"\t年龄:"
<<c->p[i].age<<"\t电话:"<<c->p[i].phone<<"\t家庭住址:"<<c->p[i].address<<endl; }
}
void dele(struct con * c){
int flag=0,j,i;
string dename;
if(c->n==0){
cout<<"联系人为空"<<endl;
flag=2;
}else{
cout<<"请输入你要删除的联系人:"<<endl;
cin>>dename;
for(i=0;i<c->n;i++){
if(c->p[i].name==dename){
flag=1;
j=i;
for(;j<c->n-1;j++){
c->p[j]=c->p[j+1];
}
c->n--;
break;
}
}}
if(flag==0){
cout<<"查无此人"<<endl;
}else if(flag==1){
cout<<"删除成功"<<endl;
}
}
void find(struct con *c){
int i;
string finame;
int flag=0;
if(c->n==0){
cout<<"联系人为空"<<endl;
flag=2;
}else{
cout<<"请输入你要查询的人"<<endl;
cin>>finame;
for(i=0;i<c->n;i++){
if(c->p[i].name==finame){
flag=1;
cout<<"姓名:"<<c->p[i].name<<"\t性别:"<<c->p[i].gender<<"\t年龄:"
<<c->p[i].age<<"\t电话:"<<c->p[i].phone<<"\t家庭住址:"<<c->p[i].address<<endl;
break;
}
}}
if(flag==0){
cout<<"查无此人"<<endl;
}
}
void edit(struct con *c){
int i,gen,flag=0;
string edname;
if(c->n==0){
cout<<"联系人为空"<<endl;
flag=2;
}else{
cout<<"请输入要修改的联系人"<<endl;
cin>>edname;
for(i=0;i<c->n;i++){
if(c->p[i].name==edname){
cout<<"请输入姓名:";
cin>>c->p[i].name;
cout<<"请输入性别(1--男,2--女):";
cin>>gen;
if(gen==1){
c->p[i].gender="男";
}else{
c->p[i].gender="女";
}
cout<<"请输入年龄:";
cin>>c->p[i].age;
cout<<"请输入联系电话:";
cin>>c->p[i].phone;
cout<<"请输入家庭住址:";
cin>>c->p[i].address;
cout<<"修改成功"<<endl;
flag=1;
break;
}
} }
if(flag==0){
cout<<"查无此人"<<endl;
}
}
void Clear(struct con *c){
int i;
for(i=0;i<c->n;i++){
c->p[i].name=" ";
c->p[i].gender=" ";
c->p[i].age=0;
c->p[i].phone=" ";
c->p[i].address=" ";
}
cout<<"清空成功"<<endl;
c->n=0;
}
int main(){
struct con c;
c.n=0;
int n;
int flag=0;
while(1){
init();
cin>>n;
switch(n){
case 1:add(&c);system("pause");system("cls");break;
case 2:show(&c); system("pause");system("cls");break;
case 3:dele(&c); system("pause");system("cls");break;
case 4:find(&c); system("pause");system("cls");break;
case 5:edit(&c); system("pause");system("cls");break;
case 6:Clear(&c); system("pause");system("cls");break;
case 0:cout<<"退出成功"<<endl;; system("pause");flag=1;break;
default:cout<<"输出错误!!!"<<endl;system("pause");system("cls");break;
}
if(flag==1){
break;
}
}
return 0;
}