通讯录源代码.docx
《通讯录源代码.docx》由会员分享,可在线阅读,更多相关《通讯录源代码.docx(14页珍藏版)》请在冰豆网上搜索。
![通讯录源代码.docx](https://file1.bdocx.com/fileroot1/2023-2/21/7df6ce4d-77bc-4c80-9465-74ab3605f7c6/7df6ce4d-77bc-4c80-9465-74ab3605f7c61.gif)
通讯录源代码
#include
#include
#include
usingnamespacestd;
typedefstruct//定义个人信息结构体
{
charname[15];//存放名字
charposition[20];//存放职务
chartitle[20];//存放职称
charnumber[12];//存放电话号码
charemail[20];//存放电子邮件
}Person;
Personper[100];
/*******************菜单显示函数******************/
intMenu()
{
charstr;
while(str<'0'||str>'6')
{
system("cls");/************清屏**********/
cout<<"\t\t*****通讯录*****\n";
cout<<"\t\t┌───────┐\n";
cout<<"\t\t│1.添加信息│\n";
cout<<"\t\t│2.显示信息│\n";
cout<<"\t\t│3.删除信息│\n";
cout<<"\t\t│4.查询信息│\n";
cout<<"\t\t│5.修改信息│\n";
cout<<"\t\t│6.姓名排序│\n";
cout<<"\t\t│0.退出程序│\n";
cout<<"\t\t└───────┘\n";
cout<<"\t\t请您选择(0-6):
";
str=getchar();
}
return(str-'0');/**********返回数字**********/
}
/********************信息添加函数******************/
intInput(Personper[],intn)
{
inti=0;
chars;
while(s!
='n'&&s!
='N')//判断是否继续添加信息
{
cout<<"\t姓名:
";
cin>>per[n+i].name;
cout<<"\t职务:
";
cin>>per[n+i].position;
cout<<"\t职称:
";
cin>>per[n+i].title;
cout<<"\t电话:
";
cin>>per[n+i].number;
cout<<"\t邮箱:
";
cin>>per[n+i].email;
cout<<"\n\t是否继续添加?
(Y/N)";
cin>>s;
i++;
}
returnn;//返回i值到主函数
}
/*********************显示函数**********************/
voidDisplay(Personper[],intn)
{
inti;
cout<<"\t****************************************************\n";
cout<<"\t"<<"姓名";
cout<<"\t"<<"职务";
cout<<"\t"<<"职称";
cout<<"\t"<<"电话";
cout<<"\t"<<"邮箱\n";
cout<<"\t****************************************************\n";
for(i=0;i<=n;i++)//循环函数,显示个人信息
{
cout<<"\t"<cout<<"\t"<cout<<"\t"<cout<<"\t"<cout<<"\t"<if(i>1&&i%10==0)
{
printf("\t---------------------------------\n");
printf("\t");
system("pause");
printf("\t---------------------------------\n");
}
}
cout<<"\t****************************************************\n";
}
/****************************删除函数*****************************/
intDelete(Personper[],intn)
{
charstr[15];
inti=0;
intj;
cout<<"\t请输入想删除记录中的名字:
";
cin>>str;
while(strcmp(per[i].name,str)!
=0&&ii++;
if(i==n)
{
cout<<"\t通讯录中没有此人!
\n";
}
for(j=i;j{
strcpy(per[j].name,per[j+1].name);
strcpy(per[j].position,per[j+1].position);/************将后一条记录拷贝到前一条**********/
strcpy(per[j].title,per[j+1].title);
strcpy(per[j].number,per[j+1].number);
strcpy(per[j].email,per[j+1].email);
}
cout<<"\t已经成功删除!
\n";
returnn-1;
}
/********************************查询函数*******************************/
voidFind(Personper[],intn)
{
intm;
cout<<"\t请选择查询方式:
\n";
cout<<"\t┌──────┐\n";
cout<<"\t│1------姓名│\n";
cout<<"\t│2------职务│\n";
cout<<"\t│3------职称│\n";//显示查询方式的目录
cout<<"\t│4------电话│\n";
cout<<"\t│5------邮箱│\n";
cout<<"\t│6------返回│\n";
cout<<"\t└──────┘\n";
cout<<"\t请选择序号:
";
cin>>m;
while(m!
=1&&m!
=2&&m!
=3&&m!
=4&&m!
=5)
{
cout<<"\t输入错误,请重新选择:
";
cin>>m;
}
if(m==1)//按姓名查询
{
charstr[15];
inti=0;
cout<<"\t请输入想查询的姓名:
";
cin>>str;
while(strcmp(per[i].name,str)!
=0&&ii++;
if(i==n)
{
cout<<"\t通讯录中没有此人!
";
return;
}
cout<<"\t此人职务:
\n"<<"\t"<cout<<"\t此人职称:
\n"<<"\t"<cout<<"\t电话号码:
\n"<<"\t"<cout<<"\t此人邮箱:
\n"<<"\t"<};
if(m==2)//按职务查询
{
charstr[20];
inti=0;
cout<<"\t请输入想查询的职务:
";
cin>>str;
while(strcmp(per[i].position,str)!
=0&&ii++;
if(i==n)
{
cout<<"\t通讯录中没有此人!
";
return;
}
cout<<"\t此人姓名:
\n"<<"\t"<cout<<"\t此人职称:
\n"<<"\t"<cout<<"\t电话号码:
\n"<<"\t"<cout<<"\t此人邮箱:
\n"<<"\t"<};
if(m==3)//按职称查询
{
charstr[20];
inti=0;
cout<<"\t请输入想查询的职称:
";
cin>>str;
while(strcmp(per[i].title,str)!
=0&&ii++;
if(i==n)
{
cout<<"\t通讯录中没有此人!
";
return;
}
cout<<"\t此人姓名:
\n"<<"\t"<cout<<"\t此人职务:
\n"<<"\t"<cout<<"\t电话号码:
\n"<<"\t"<cout<<"\t此人邮箱:
\n"<<"\t"<};
if(m==4)//按号码查询
{
charl[12];
inti=0;
cout<<"\t请输入想查询的电话:
";
cin>>l;
while((per[i].number,l)!
=0&&ii++;
if(i==n)
{
cout<<"\t通讯录中没有此人!
";
return;
}
cout<<"\t此人姓名:
\n"<<"\t"<cout<<"\t此人职务:
\n"<<"\t"<cout<<"\t此人职称:
\n"<<"\t"<cout<<"\t此人邮箱:
\n"<<"\t"<}
if(m==5)//按邮箱查询
{
charstr[20];
inti=0;
cout<<"\t请输入想查询的邮箱:
";
cin>>str;
while(strcmp(per[i].email,str)!
=0&&ii++;
if(i==n)
{
cout<<"\t通讯录中没有此人!
";
return;
}
cout<<"\t此人姓名:
\n"<<"\t"<cout<<"\t此人职务:
\n"<<"\t"<cout<<"\t此人职称:
\n"<<"\t"<cout<<"\t电话号码:
\n"<<"\t"<};
}
/**********************修改函数************************/
voidModify(Personper[],intn)/*修改函数*/
{
charstr[15];
inti=0;
cout<<"\t想修改的名字:
\t";
cin>>str;
while(strcmp(per[i].name,str)!
=0&&ii++;
if(i==n)
{
cout<<"\t没有此人!
\t";
return;
}
cout<<"\t输入新姓名:
";
gets(per[i].name);
cout<<"\t输入新职务:
";
gets(per[i].position);
cout<<"\t输入新职称:
";
gets(per[i].title);
cout<<"\t输入新号码:
";
gets(per[i].number);
cout<<"\t输入新邮箱:
";
gets(per[i].email);
cout<<"\t修改成功\n\t";
}
/*****************排序函数***************/
voidSort(Personper[],intn)
{
inti;
intj;
chartemp[15];
for(i=0;ifor(j=i;jif(strcmp(per[i].name,per[j].name)>0)
{
strcpy(temp,per[i].name);
strcpy(per[i].name,per[j].name);
strcpy(per[j].name,temp);
strcpy(temp,per[i].position);
strcpy(per[i].position,per[j].position);
strcpy(per[j].position,temp);
strcpy(temp,per[i].title);
strcpy(per[i].title,per[j].title);
strcpy(per[j].title,temp);
strcpy(temp,per[i].number);
strcpy(per[i].number,per[j].number);
strcpy(per[j].number,temp);
strcpy(temp,per[i].email);
strcpy(per[i].email,per[j].email);
strcpy(per[j].email,temp);
}
Display(per,n);
return;
}
/******************主函数开始******************/
voidmain()
{
intn=0;
for(n=0;n<=6;n++)
{
switch(Menu())/*调用主菜单函数,返回值整数作开关语句的条件*/
{
case1:
cout<<"\t添加记录到通讯录\n";
n=Input(per,n);
cout<<"\t";
system("pause");
break;
case2:
cout<<"\t通讯录显示\n";
Display(per,n);
cout<<"\t";
system("pause");
break;
case3:
cout<<"\t从通讯录中删除记录\n";
n=Delete(per,n);
cout<<"\t";
system("pause");
break;
case4:
cout<<"\t通讯录中查找\n";
Find(per,n);
cout<<"\t";
system("pause");
break;
case5:
cout<<"\t修改记录\n";
Modify(per,n);
cout<<"\t";
system("pause");
break;
case6:
cout<<"\t按姓名排序\n";
Sort(per,n);
cout<<"\t";
system("pause");
break;
case0:
/*如返回值为0则程序结束*/
cout<<"\tOVER!
\n";
default:
cout<<"\n\t";
exit(0);
}
}
}