大作业 通讯录 有排序.docx
《大作业 通讯录 有排序.docx》由会员分享,可在线阅读,更多相关《大作业 通讯录 有排序.docx(17页珍藏版)》请在冰点文库上搜索。
大作业通讯录有排序
C++大作业通讯录
#include
#include
#include
#include
#include//system()
#include
usingnamespacestd;
classStudent{
private:
char*name;
char*sex;//性别
char*num;//学号
char*pnum;//手机号
char*qq;
public:
~Student(){delete[]name;delete[]sex;delete[]num;delete[]pnum;delete[]qq;}
Student&operator=(constStudent&s);
intadd();//添加联系人
intshow();//显示记录
intdel();//删除联系人
intfind();//通过姓名学号查询联系人
intmodify();//修改信息
intgainfromfile();//从文件中读取
intsavetofile();//保存信息到文件中
intmodifymenu();//修改菜单
Student*next;
Student*head;
};
//重载赋值运算符“=”
Student&Student:
:
operator=(constStudent&s)
{
name=newchar[strlen(s.name)+1];
strcpy(name,s.name);
sex=newchar[strlen(s.sex)+1];
strcpy(sex,s.sex);
num=newchar[strlen(s.num)+1];
strcpy(num,s.num);
pnum=newchar[strlen(s.pnum)+1];
strcpy(pnum,s.pnum);
qq=newchar[strlen(s.qq)+1];
strcpy(qq,s.qq);
return*this;
}
Studentuser;
//从文件中读取
intStudent:
:
gainfromfile(){
fstreamfile;
Student*p1,*p2,*p3,*p4;
//p3=p4=newStudent;
charName[20];//姓名
charSex[20];//性别
charNum[20];//QQ
charPnum[20];//电话
charQQ[20];//学号
inti=1;
file.open("D:
\\notebook.txt",ios:
:
in);
if(!
file){
cout<<"\n\n\t对不起,您的操作有误!
!
"<cout<<"\n\t请按回车键继续!
!
"<getch();
return0;
}
while(!
file.eof()){
p1=newStudent;
file>>Name>>Sex>>Num>>Pnum>>QQ;
p1->name=newchar[strlen(Name)+1];
strcpy(p1->name,Name);
p1->sex=newchar[strlen(Sex)+1];
strcpy(p1->sex,Sex);
p1->num=newchar[strlen(Num)+1];
strcpy(p1->num,Num);
p1->pnum=newchar[strlen(Pnum)+1];
strcpy(p1->pnum,Pnum);
p1->qq=newchar[strlen(QQ)+1];
strcpy(p1->qq,QQ);
p1->next=NULL;
if(i==1){head=p1;i=2;}
elsep2->next=p1;
p2=p1;
//if(file.eof())break;
}
for(p1=head;p1->next!
=NULL;p1=p1->next)
{
for(p2=p1,p3=head;p3!
=NULL;p3=p3->next)
{
if(strcmp(p2->name,p3->name)<0)
{
Studenttemp;
temp.next=NULL;
temp=*p3;
*p3=*p2;
*p2=temp;
}
}
}
savetofile();
return1;//记录文件读取成功
}
//保存信息到文件中
intStudent:
:
savetofile(){
fstreamfile;
Student*p1=head;
file.open("D:
\\notebook.txt",ios:
:
out|ios:
:
trunc);
while(p1->next!
=NULL){
file<name<sex<<num<pnum<qq<p1=p1->next;
}
file.close();
return0;
}
//显示当前通讯录信息
intStudent:
:
show(){
system("cls");
Student*p;
cout<<"显示当前所有记录:
\n";
if(gainfromfile()==0)return0;
else{
p=head;
cout<<"\n\n"<<"\t"<:
left)<<<"姓名:
"<"<"
<"<"<while(p->next!
=NULL)
{
cout<<"\t"<:
left)
<name
<sex
<num
<pnum
<qq;
cout<<"\n";
p=p->next;
}
cout<<"\n\t请按回车键继续!
"<getch();
return0;
}
}
//添加联系人
intStudent:
:
add(){
system("cls");
fstreamfile;
Student*p1=newStudent;
charName[20];//姓名
charSex[20];//性别
charNum[20];//学号
charPnum[20];//电话
charQQ[20];//qq
file.open("D:
\\notebook.txt",ios:
:
app|ios:
:
out);
intachoice;
for(;;){
system("cls");
cout<<"请您输入联系人姓名:
\n";
cin>>Name;
p1->name=newchar[strlen(Name)+1];
strcpy(p1->name,Name);
file<:
left)<name;
cout<<"请您输入联系人性别:
\n";
cin>>Sex;
p1->sex=newchar[strlen(Sex)+1];
strcpy(p1->sex,Sex);
file<sex;
cout<<"请您输入联系人学号:
\n";
cin>>Num;
p1->num=newchar[strlen(Num)+1];
strcpy(p1->num,Num);
file<num;
cout<<"请您输入联系人手机号:
\n";
cin>>Pnum;
p1->pnum=newchar[strlen(Pnum)+1];
strcpy(p1->pnum,Pnum);
file<pnum;
cout<<"请您输入联系人QQ号:
\n";
cin>>QQ;
p1->qq=newchar[strlen(QQ)+1];
strcpy(p1->qq,QQ);
file<qq;
p1->next=NULL;
file<cout<<"是否继续添加联系人:
1(是)0(否)"<cin>>achoice;
if(achoice==0)break;
}
file.close();
return0;
}
//删除联系人
intStudent:
:
del(){
system("cls");
Student*p1,*p2;
p1=p2=newStudent;
chardname[20];
intdchoice;
if(gainfromfile()==0)return0;
for(;;){
cout<<"请您输入要删除联系人的姓名:
"<cin>>dname;
p1=head;
while(strcmp(p1->name,dname)!
=0&&p1->next!
=NULL)
{p2=p1;p1=p1->next;}
if(strcmp(p1->name,dname)==0){
cout<<"您要删除联系人的信息为:
\n\n"<cout<<"\t姓名:
"<name<<<"\t性别:
"<sex<<<"\t学号:
"<num<<<"\t电话:
"<pnum<<<"\tQQ:
"<qq<cout<<"请问您是否确认要删除:
1(删除)0(不删除)"<cin>>dchoice;
if(dchoice==0)return0;
if(p1==head)head=p1->next;
elsep2->next=p1->next;
cout<<"\t该联系人已经被删除"<savetofile();
}
elsecout<"<cout<<"\n\t是否继续?
?
?
1(是)/0(否)"<cin>>dchoice;
if(dchoice==0)
break;
}
return0;
}
//通过姓名学号查询联系人
intStudent:
:
find(){
system("cls");
if(gainfromfile()==0)return0;
Student*p;
char*fname;
fname=newchar[20];
char*fnum;
fnum=newchar[15];
while
(1){
system("cls");
cout<<"请您选择要查询的方式:
1(名字查询)2(学号查询)"<intfindway;
cin>>findway;
p=head;
switch(findway)
{
case1:
{
cout<<"输入要查询的名字:
";
cin>>fname;
while(p){
if(strcmp(p->name,fname)==0)//比较名字是否相等。
{
cout<<"您查询的联系人信息如下:
"<cout<<"姓名:
"<name<<<"性别:
"<sex<<<"学号:
"<num<<<"电话:
"<pnum<<<"QQ:
"<qq<break;
}
p=p->next;
}
if(p==NULL){cout<<"\t@@@@通讯簿为空!
@@@@"<elseif(p!
=NULL&&strcmp(p->name,fname)!
=0)
{cout<<"\t您修改的联系人不存在!
!
!
"<break;
}
case2:
{
cout<<"输入要查询的学号:
";
cin>>fnum;
while(p){
if(strcmp(p->num,fnum)==0)//比较名字是否相等。
{
cout<<"您查询的联系人信息如下:
"<cout<<"姓名:
"<name<<<"性别:
"<sex<<<"学号:
"<num<<<"电话:
"<pnum<<<"QQ:
"<qq<break;
}
p=p->next;
}
if(p==NULL){cout<<"\t@@@@通讯簿为空!
@@@@"<elseif(p!
=NULL&&strcmp(p->num,fnum)!
=0)
{cout<<"\t您修改的联系人不存在!
!
!
"<break;
}
}
cout<<"您是否继续查找联系人:
1(是)0(否)"<intfchoice;
cin>>fchoice;
if(fchoice!
=1)return0;
}
return0;
}
//修改菜单
intStudent:
:
modifymenu(){
intmchoice;
cout<<"\t**********输入您要修改的项**********\n\n"<<<"\t\t*****输入1更新人名*****"<<<"\t\t*****输入2更新性别*****"<<<"\t\t*****输入3更新学号*****"<<<"\t\t*****输入4更新电话*****"<<<"\t\t*****选择5更新QQ号*****"<<<"\t\t+++++选择0更新完毕+++++"<<<"\t**********+++++++++++++++++**********\n\n"<<<"\n\t输入要更新的内容:
";
cin>>mchoice;
return(mchoice);
}
//修改联系人信息
intStudent:
:
modify(){
system("cls");
Student*p0;
charmname[20];
intmchoice;
if(gainfromfile()==0)return0;
for(;;){
system("cls");
cout<<"\t输入要更新联系人的名字:
";
cin>>mname;
p0=head;
while(p0)//首先判断该记录是否存在。
{
if(strcmp(p0->name,mname)==0)//比较名字是否相等。
{
cout<<"要更新的内容存在!
"<cout<<"\t姓名:
"<name<<<"\t性别:
"<sex<<<"\t学号:
"<num<<<"\t电话:
"<pnum<<<"\tQQ:
"<qq<cout<<"\n\n";
getchar();
intmflag=0;
for(;;)
{
switch(modifymenu())
{
system("cls");
case1:
{
cout<<"请您输入修改后的姓名:
"<cin>>p0->name;break;
}
case2:
{
cout<<"请您输入修改后的性别:
"<cin>>p0->sex;break;
}
case3:
{
cout<<"请您输入修改后的学号:
"<cin>>p0->num;break;
}
case4:
{
cout<<"请您输入修改后的电话号:
"<cin>>p0->pnum;break;
}
case5:
{
cout<<"请您输入修改后的QQ:
"<cin>>p0->qq;break;
}
case0:
{mflag=1;break;}
}
if(mflag==1)
break;
cout<<"是否修改其他项:
1(是)0(否):
";
intmmchoice;
cin>>mmchoice;
if(mmchoice!
=1){savetofile();return0;}
}
}
elsep0=p0->next;
}
if(head==NULL){cout<<"\t@@@@通讯簿为空!
@@@@"<cout<<"\t您修改的联系人不存在!
!
!
";
cout<<"您是否继续修改联系人信息:
1(是)0(否)"<cin>>mchoice;
if(mchoice!
=1)break;
}
savetofile();
return0;
}
//主功能菜单
intmainmenu(){
intchoice;
cout<<"\n\n\n\t\t请您输入要选择的服务项:
\n\n"
<<"\t\t**1:
添加联系人\n"
<<"\t\t**2:
显示当前通讯录信息\n"
<<"\t\t**3:
删除联系人\n"
<<"\t\t**4:
查找联系人\n"
<<"\t\t**5:
修改联系人信息\n"
<<"\t\t**0:
退出\n\n\n\n"
<<"\t\t**您要操作的选项是:
\n\n";
cin>>choice;
return(choice);
}
//选择菜单
voidchoose(){
intflag=0;
for(;;)
{
system("cls");
switch(mainmenu())
{
case1:
user.add();break;//显示当前通讯录信息
case2:
user.show();break;//添加联系人
case3:
user.del();break;//删除联系人
case4:
user.find();break;//查找联系人信息
case5:
user.modify();break;//修改联系人信息
case0:
{flag=1;break;}
}
if(flag==1)
break;
}
}
//函数入口,欢迎界面
intmain(){
cout<<"\n\n\n\t\t*********************************************\n\n\n";
cout<<"\t\t\t欢迎使用***班***的通讯簿";
cout<<"\n\n\n\t\t*********************************************\n\n\n";
cout<<"\n\n\t\t\t现在请您按回车键!
!
!
\n\n";
getchar();
choose();
}