利用C++如何做个通讯录,包括添加通讯录,删除通讯录,显示,查找,存盘,退出

更新时间:01-22 教程 由 情の殇 分享

C++实现通讯录管理系统

本文实例为大家分享了C++通讯录管理系统的具体代码,供大家参考,具体内容如下

#include

#include

usingnamespacestd;

#defineMAX1000

structPerson

{

stringm_Name;

intm_Sex;

intm_Age;

stringm_Phone;

stringm_Addr;

};

structAddressbooks

{

structPersonpersonArray[MAX];

intm_Size;

};

voidaddPerson(Addressbooks*abs)

{

if(abs->m_Size==MAX)

{

cout<<"通讯录已满,无法添加!"<

return;

}

else

{

stringname;

cout<<"请输入姓名:"<

cin>>name;

abs->personArray[abs->m_Size].m_Name=name;

cout<<"请输入性别:"<

cout<<"1---男"<

cout<<"2---女"<

intsex=0;

while(true)

{

cin>>sex;

if(sex==1||sex==2)

{

abs->personArray[abs->m_Size].m_Sex=sex;

break;

}

cout<<"输入有误,请重新输入!"<

}

cout<<"请输入年龄:"<

intage=0;

cin>>age;

abs->personArray[abs->m_Size].m_Age=age;

cout<<"请输入联系电话:"<

stringphone;

cin>>phone;

abs->personArray[abs->m_Size].m_Phone=phone;

cout<<"请输入家庭住址:"<

stringaddress;

cin>>address;

abs->personArray[abs->m_Size].m_Addr=address;

abs->m_Size++;

cout<<"添加成功"<

system("pause");

system("cls");

}

}

voidshowPerson(Addressbooks*abs)

{

if(abs->m_Size==0)

{

cout<<"当前记录为空"<

}

else

{

for(inti=0;im_Size;i++)

{

cout<<"姓名:"

cout<<"性别:"<<(abs->personArray[i].m_Sex==1?"男":"女")<<"\t";

cout<<"年龄:"

cout<<"电话:"

cout<<"住址:"

}

}

system("pause");

system("cls");

}

intisExist(Addressbooks*abs,stringname)

{

for(inti=0;im_Size;i++)

{

if(abs->personArray[i].m_Name==name)

{

returni;

}

}

return-1;//没找到

}

//删除联系人

voiddeletePerson(Addressbooks*abs)

{

cout<<"请输入要删除的联系人:"<

stringname;

cin>>name;

intret=isExist(abs,name);

if(ret!=-1)

{

for(inti=ret;im_Size;i++)

{

abs->personArray[i]=abs->personArray[i+1];

}

abs->m_Size--;

cout<<"删除成功!"<

}

system("pause");

system("cls");

}

//查找联系人

voidfindPerson(Addressbooks*abs)

{

cout<<"请输入要查找的联系人:"<

stringname;

cin>>name;

intret=isExist(abs,name);

if(ret!=-1)

{

cout<<"姓名:"

cout<<"性别:"<<(abs->personArray[ret].m_Sex==1?"男":"女")<<"\t";

cout<<"年龄:"

cout<<"电话:"

cout<<"住址:"

}

else

{

cout<<"查无此人"<

}

system("pause");

system("cls");

}

//修改联系人

voidmodifyPerson(Addressbooks*abs)

{

cout<<"请输入要修改的联系人:"<

stringname;

cin>>name;

intret=isExist(abs,name);

if(ret!=-1)

{

stringname;

cout<<"请输入姓名:"<

cin>>name;

abs->personArray[ret].m_Name=name;

cout<<"请输入性别:"<

cout<<"1---男"<

cout<<"2---女"<

intsex=0;

while(true)

{

cin>>sex;

if(sex==1||sex==2)

{

abs->personArray[ret].m_Sex=sex;

break;

}

cout<<"输入有误,请重新输入!"<

}

cout<<"请输入年龄:"<

intage=0;

cin>>age;

abs->personArray[ret].m_Age=age;

cout<<"请输入联系电话:"<

stringphone;

cin>>phone;

abs->personArray[ret].m_Phone=phone;

cout<<"请输入家庭住址:"<

stringaddress;

cin>>address;

abs->personArray[ret].m_Addr=address;

cout<<"修改成功"<

}

else

{

cout<<"查无此人"<

}

system("pause");

system("cls");

}

//清空联系人

voidcleanPerson(Addressbooks*abs)

{

abs->m_Size=0;

cout<<"通讯录已清空"<

system("pause");

system("cls");

}

//显示菜单

voidshowMenu()

{

cout<<"*************************"<

cout<<"*****1、添加联系人*****"<

cout<<"*****2、显示联系人*****"<

cout<<"*****3、删除联系人*****"<

cout<<"*****4、查找联系人*****"<

cout<<"*****5、修改联系人*****"<

cout<<"*****6、清空联系人*****"<

cout<<"*****0、退出通讯录*****"<

cout<<"*************************"<

}

intmain()

{

Addressbooksabs;

abs.m_Size=0;

intselect=0;

while(true)

{

showMenu();

cin>>select;

switch(select)

{

case1://添加联系人

addPerson(&abs);

break;

case2://显示联系人

showPerson(&abs);

break;

case3://删除联系人

/*{

cout<<"请输入删除联系人姓名:"<

stringname;

cin>>name;

if(isExist(&abs,name)==-1)

{

cout<<"查无此人"<

}

else

{

cout<<"找到此人"<

}

}*/

deletePerson(&abs);

break;

case4://查找联系人

findPerson(&abs);

break;

case5://修改联系人

modifyPerson(&abs);

break;

case6://清空联系人

cleanPerson(&abs);

break;

case0:

cout<<"欢迎下次使用"<

system("pause");

return0;

break;

default:

break;

}

}

system("pause");

return0;

}

声明:关于《利用C++如何做个通讯录,包括添加通讯录,删除通讯录,显示,查找,存盘,退出》以上内容仅供参考,若您的权利被侵害,请联系13825271@qq.com
本文网址:http://www.25820.com/tutorial/14_2166139.html