C++實現通訊錄管理系統
本文實例為大家分享了C++通訊錄管理系統的具體代碼,供大家參考,具體內容如下
#include<iostream>
#include<string>
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<<"通訊錄已滿,無法添加!"<<endl;
return;
}
else
{
stringname;
cout<<"請輸入姓名:"<<endl;
cin>>name;
abs->personArray[abs->m_Size].m_Name=name;
cout<<"請輸入性別:"<<endl;
cout<<"1---男"<<endl;
cout<<"2---女"<<endl;
intsex=0;
while(true)
{
cin>>sex;
if(sex==1||sex==2)
{
abs->personArray[abs->m_Size].m_Sex=sex;
break;
}
cout<<"輸入有誤,請重新輸入!"<<endl;
}
cout<<"請輸入年齡:"<<endl;
intage=0;
cin>>age;
abs->personArray[abs->m_Size].m_Age=age;
cout<<"請輸入聯系電話:"<<endl;
stringphone;
cin>>phone;
abs->personArray[abs->m_Size].m_Phone=phone;
cout<<"請輸入家庭住址:"<<endl;
stringaddress;
cin>>address;
abs->personArray[abs->m_Size].m_Addr=address;
abs->m_Size++;
cout<<"添加成功"<<endl;
system("pause");
system("cls");
}
}
voidshowPerson(Addressbooks*abs)
{
if(abs->m_Size==0)
{
cout<<"當前記錄為空"<<endl;
}
else
{
for(inti=0;i<abs->m_Size;i++)
{
cout<<"姓名:"<<abs->personArray[i].m_Name<<"\t";
cout<<"性別:"<<(abs->personArray[i].m_Sex==1?"男":"女")<<"\t";
cout<<"年齡:"<<abs->personArray[i].m_Age<<"\t";
cout<<"電話:"<<abs->personArray[i].m_Phone<<"\t";
cout<<"住址:"<<abs->personArray[i].m_Addr<<endl;
}
}
system("pause");
system("cls");
}
intisExist(Addressbooks*abs,stringname)
{
for(inti=0;i<abs->m_Size;i++)
{
if(abs->personArray[i].m_Name==name)
{
returni;
}
}
return-1;//沒找到
}
//刪除聯系人
voiddeletePerson(Addressbooks*abs)
{
cout<<"請輸入要刪除的聯系人:"<<endl;
stringname;
cin>>name;
intret=isExist(abs,name);
if(ret!=-1)
{
for(inti=ret;i<abs->m_Size;i++)
{
abs->personArray[i]=abs->personArray[i+1];
}
abs->m_Size--;
cout<<"刪除成功!"<<endl;
}
system("pause");
system("cls");
}
//查找聯系人
voidfindPerson(Addressbooks*abs)
{
cout<<"請輸入要查找的聯系人:"<<endl;
stringname;
cin>>name;
intret=isExist(abs,name);
if(ret!=-1)
{
cout<<"姓名:"<<abs->personArray[ret].m_Name<<"\t";
cout<<"性別:"<<(abs->personArray[ret].m_Sex==1?"男":"女")<<"\t";
cout<<"年齡:"<<abs->personArray[ret].m_Age<<"\t";
cout<<"電話:"<<abs->personArray[ret].m_Phone<<"\t";
cout<<"住址:"<<abs->personArray[ret].m_Addr<<endl;
}
else
{
cout<<"查無此人"<<endl;
}
system("pause");
system("cls");
}
//修改聯系人
voidmodifyPerson(Addressbooks*abs)
{
cout<<"請輸入要修改的聯系人:"<<endl;
stringname;
cin>>name;
intret=isExist(abs,name);
if(ret!=-1)
{
stringname;
cout<<"請輸入姓名:"<<endl;
cin>>name;
abs->personArray[ret].m_Name=name;
cout<<"請輸入性別:"<<endl;
cout<<"1---男"<<endl;
cout<<"2---女"<<endl;
intsex=0;
while(true)
{
cin>>sex;
if(sex==1||sex==2)
{
abs->personArray[ret].m_Sex=sex;
break;
}
cout<<"輸入有誤,請重新輸入!"<<endl;
}
cout<<"請輸入年齡:"<<endl;
intage=0;
cin>>age;
abs->personArray[ret].m_Age=age;
cout<<"請輸入聯系電話:"<<endl;
stringphone;
cin>>phone;
abs->personArray[ret].m_Phone=phone;
cout<<"請輸入家庭住址:"<<endl;
stringaddress;
cin>>address;
abs->personArray[ret].m_Addr=address;
cout<<"修改成功"<<endl;
}
else
{
cout<<"查無此人"<<endl;
}
system("pause");
system("cls");
}
//清空聯系人
voidcleanPerson(Addressbooks*abs)
{
abs->m_Size=0;
cout<<"通訊錄已清空"<<endl;
system("pause");
system("cls");
}
//顯示菜單
voidshowMenu()
{
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;
}
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<<"請輸入刪除聯系人姓名:"<<endl;
stringname;
cin>>name;
if(isExist(&abs,name)==-1)
{
cout<<"查無此人"<<endl;
}
else
{
cout<<"找到此人"<<endl;
}
}*/
deletePerson(&abs);
break;
case4://查找聯系人
findPerson(&abs);
break;
case5://修改聯系人
modifyPerson(&abs);
break;
case6://清空聯系人
cleanPerson(&abs);
break;
case0:
cout<<"歡迎下次使用"<<endl;
system("pause");
return0;
break;
default:
break;
}
}
system("pause");
return0;
}