#include<iostream>
using namespace std;
#include<string>
//创建通讯录存储最多的人数
#define MAX 1000
//创建联系人结构体
struct Person
{
string name;
int sex;
int age;
int phone;
string adder;
};
//创建通讯录结构体
struct Adressbook
{
struct Person personArray[MAX];
int size;
};
//创建展示栏
void printShow()
{
cout<<"************"<<endl;
cout<<"1.添加联系人"<<endl;
cout<<"2.显示联系人"<<endl;
cout<<"3.删除联系人"<<endl;
cout<<"4.查找联系人"<<endl;
cout<<"5.修改联系人"<<endl;
cout<<"6.清空联系人"<<endl;
cout<<"7.退出菜单"<< endl;
cout<<"************"<<endl;
}
//添加联系人
void addPerson(Adressbook * per)
{
if(per->size==MAX)
{
cout<<"通讯录已满"<<endl;
}
else
{
string name;
cout<<"请输入姓名"<<endl;
cin>>name;
per->personArray[per->size].name=name;
int sex;
while(true)
{
cout<<"请输入性别"<<endl;
cout<<"1 :男 "<<endl;
cout<<"0 :女 "<<endl;
cin>>sex;
if(sex==1||sex==0)
{
per->personArray[per->size].sex=sex;
break;
}
else
{
cout<<"输入错误请重新输入"<<endl;
}
}
int age;
cout<<"请输入年龄"<<endl;
cin>>age;
per->personArray[per->size].age=age;
int phone;
cout<<"请输入电话"<<endl;
cin>>phone;
per->personArray[per->size].phone=phone;
string adderss;
cout<<"请输入地址"<<endl;
cin>>adderss;
per->personArray[per->size].adder=adderss;
cout<<"输入成功"<<endl;
per->size++;
system("puase");
system("cls");
}
}
// 展示联系人
void showPerson(Adressbook * per)
{
if(per->size==0)
{
cout<<"当前目录为空"<<endl;
}
else
{
for(int i=0;i<=per->size;i++)
{
cout<<per->personArray[i].name<<"\t";
cout<<per->personArray[i].sex<<"\t";
cout<<per->personArray[i].age<<"\t";
cout<<per->personArray[i].phone<<"\t";
cout<<per->personArray[i].adder<<"\t";
}
}
system("pause");
system("cls");
}
// 获取联系人位置
int isExit(Adressbook * per,string name)
{
for(int i=0;i<per->size;i++)
{
if(name==per->personArray[i].name)
{
return i;
}
}
return -1;
}
// 删除联系人
void delect(Adressbook * per)
{
string name;
cout<<"输入你所要删除的用户姓名"<<endl;
cin>>name;
int key=isExit(per,name);
if(key!=-1)
{
for(int i=key;i<per->size;i++)
{
per->personArray[i]=per->personArray[i+1];
}
cout<<"删除成功"<<endl;
}
else
{
cout<<"查无此人"<<endl;
}
system("pause");
system("cls");
}
//查找联系人
void kselect(Adressbook * per)
{
string name;
cout<<"请输入查找联系人的姓名"<<endl;
cin>>name;
int ret=isExit(per,name);
if(ret!=-1)
{
cout<<per->personArray[ret].name<<"\t";
cout<<per->personArray[ret].sex<<"\t";
cout<<per->personArray[ret].age<<"\t";
cout<<per->personArray[ret].phone<<"\t";
cout<<per->personArray[ret].adder<<"\t";
}
else
{
cout<<"查无此人"<<endl;
}
system("pause");
system("cls");
}
//修改联系人
void modifyperson(Adressbook * per)
{
string name;
cout<<"输入你要修改人的姓名"<<endl;
cin>>name;
int ret=isExit(per,name);
if(ret!=-1)
{
string a_name;
cout<<"修改此人的姓名"<<endl;
cin>>a_name;
per->personArray[ret].name=a_name;
int sex;
while(true)
{
cout<<"请输入此人性别"<<endl;
cout<<"1--男 0--女"<<endl;
cin>>sex;
if(sex==1||sex==0)
{
per->personArray[ret].sex=sex;
break;
}
else
{
cout<<"输入格式错误,请重新输入"<<endl;
}
}
int age;
cout<<"请输入年龄"<<endl;
cin>>age;
per->personArray[ret].age=age;
int phone;
cout<<"请输入手机号"<<endl;
cin>>phone;
per->personArray[ret].phone=phone;
string address;
cout<<"请输入地址"<<endl;
cin>>address;
per->personArray[ret].adder=address;
cout<<"修改成功"<<endl;
}
else
{
cout<<"没有此人"<<endl;
}
system("pause");
system("cls");
}
//清空联系人
void cleanPerson(struct Adressbook *per)
{
per->size=0;
cout<<"通讯录已清空"<<endl;
system("pause");
system("cls");
}
// 设置执行主函数
int main()
{
int select=0;
struct Adressbook abs;
abs.size=0;
while(true)
{printShow();
cin>>select;
switch(select)
{
case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
delect(&abs);
break;
case 4:
kselect(&abs);
break;
case 5:
modifyperson(&abs);
break;
case 6:
cleanPerson(&abs);
break;
case 7:
cout<<"欢迎使用"<<endl;
return 0;
}
}
return 0;
}
#include<iostream> using namespace std;#include<string>#define MAX 1000struct Person{string name;int sex;int age;int phone;string adder;};struct Adressbook{struct Person personArray[MAX];int size; };
void printShow(){cout<<"************"<<endl;cout<<"1.添加联系人"<<endl;cout<<"2.显示联系人"<<endl;cout<<"3.删除联系人"<<endl;cout<<"4.查找联系人"<<endl;cout<<"5.修改联系人"<<endl;cout<<"6.清空联系人"<<endl;cout<<"7.退出菜单"<< endl;cout<<"************"<<endl;}void addPerson(Adressbook * per){if(per->size==MAX){cout<<"通讯录已满"<<endl;}else{string name;cout<<"请输入姓名"<<endl;cin>>name;per->personArray[per->size].name=name;int sex;while(true){cout<<"请输入性别"<<endl;cout<<"1 :男 "<<endl;cout<<"0 :女 "<<endl; cin>>sex;if(sex==1||sex==0){per->personArray[per->size].sex=sex;break; }else{cout<<"输入错误请重新输入"<<endl;}}int age;cout<<"请输入年龄"<<endl;cin>>age;per->personArray[per->size].age=age;int phone;cout<<"请输入电话"<<endl;cin>>phone;per->personArray[per->size].phone=phone;string adderss;cout<<"请输入地址"<<endl;cin>>adderss;per->personArray[per->size].adder=adderss;cout<<"输入成功"<<endl; per->size++;system("puase");system("cls");
} } void showPerson(Adressbook * per) { if(per->size==0) { cout<<"当前目录为空"<<endl; } else { for(int i=0;i<=per->size;i++) { cout<<per->personArray[i].name<<"\t"; cout<<per->personArray[i].sex<<"\t"; cout<<per->personArray[i].age<<"\t"; cout<<per->personArray[i].phone<<"\t"; cout<<per->personArray[i].adder<<"\t"; } } system("pause"); system("cls"); } int isExit(Adressbook * per,string name) { for(int i=0;i<per->size;i++) { if(name==per->personArray[i].name) { return i; } }return -1; } void delect(Adressbook * per){string name;cout<<"输入你所要删除的用户姓名"<<endl;cin>>name;int key=isExit(per,name); if(key!=-1){for(int i=key;i<per->size;i++){per->personArray[i]=per->personArray[i+1];}cout<<"删除成功"<<endl; }else{cout<<"查无此人"<<endl; }system("pause");system("cls");}void kselect(Adressbook * per){string name;cout<<"请输入查找联系人的姓名"<<endl;cin>>name;int ret=isExit(per,name);if(ret!=-1){cout<<per->personArray[ret].name<<"\t";cout<<per->personArray[ret].sex<<"\t";cout<<per->personArray[ret].age<<"\t";cout<<per->personArray[ret].phone<<"\t";cout<<per->personArray[ret].adder<<"\t"; }else{cout<<"查无此人"<<endl; }system("pause");system("cls"); }void modifyperson(Adressbook * per){string name;cout<<"输入你要修改人的姓名"<<endl;cin>>name;int ret=isExit(per,name);if(ret!=-1){string a_name;cout<<"修改此人的姓名"<<endl;cin>>a_name;per->personArray[ret].name=a_name;int sex;while(true){cout<<"请输入此人性别"<<endl;cout<<"1--男 0--女"<<endl; cin>>sex;if(sex==1||sex==0){per->personArray[ret].sex=sex; break; }else{cout<<"输入格式错误,请重新输入"<<endl;}} int age;cout<<"请输入年龄"<<endl;cin>>age;per->personArray[ret].age=age; int phone;cout<<"请输入手机号"<<endl;cin>>phone;per->personArray[ret].phone=phone;string address;cout<<"请输入地址"<<endl;cin>>address;per->personArray[ret].adder=address;cout<<"修改成功"<<endl; }else{cout<<"没有此人"<<endl; }system("pause");system("cls");}
void cleanPerson(struct Adressbook *per){per->size=0;cout<<"通讯录已清空"<<endl;system("pause");system("cls");} int main(){int select=0;struct Adressbook abs;abs.size=0;while(true){printShow(); cin>>select;switch(select){case 1:addPerson(&abs);break;case 2:showPerson(&abs);break;case 3:delect(&abs);break;case 4:kselect(&abs);break;case 5:modifyperson(&abs);break;case 6:cleanPerson(&abs);break;case 7:cout<<"欢迎使用"<<endl; return 0;}}
return 0;}