通讯录管理软件C++编程.docx
《通讯录管理软件C++编程.docx》由会员分享,可在线阅读,更多相关《通讯录管理软件C++编程.docx(14页珍藏版)》请在冰豆网上搜索。
通讯录管理软件C++编程
#include
#include
#include
#include
#include//文本操作默认信息保存在工程文件夹下
#defineN40//定义基本信息的长度
intt;
usingnamespacestd;
charname[N];//全局变量定义
charaddress[N];
charphone[N];
charcode[N];
charemail[N];
classhum
{
private:
charname[N];//基础类的定义
charaddress[N];
charphone[N];
charcode[N];
charemail[N];
public:
hum*next;
hum()
{}
~hum()
{}
friendclassList;
};
classList:
publichum//私有信息封装
{
private:
hum*head,*tail;
public:
List();
~List();
voidCreatList();
voidset(char*name=".",char*address=".",char*phone=".",char*code=".",char*email=".");
voidDelete(intt);
voidChange(char*name);
voidFind(intx);
voidfilecin();
voidfileout();
voidshowall();
};
List:
:
List()
{
head=tail=NULL;
}
List:
:
~List()
{
hum*p;
if(head)
{
p=head;
head=head->next;
deletep;
}
}
voidList:
:
set(char*name,char*address,char*phone,char*email,char*code)//文本导入时调用的构造函数
{
hum*p=newhum;
strcpy(p->name,name);
strcpy(p->address,address);
strcpy(p->phone,phone);
strcpy(p->code,code);
strcpy(p->email,email);
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
}
voidList:
:
CreatList()//默认的构造函数
{
intflag=1;
hum*p=newhum;
strcpy(p->name,".");
strcpy(p->address,".");
strcpy(p->phone,".");
strcpy(p->code,".");
strcpy(p->email,".");
getchar();
cout<<"请输入学生姓名:
";
gets(name);
if(strcmp(name,"")!
=0)
strcpy(p->name,name);
cout<<"请输入家庭地址:
";
gets(address);
if(strcmp(address,"")!
=0)
strcpy(p->address,address);
cout<<"请输入联系电话:
";
gets(phone);
if(strcmp(phone,"")!
=0)
strcpy(p->phone,phone);
cout<<"请输入邮编:
";
gets(code);
if(strcmp(code,"")!
=0)
strcpy(p->code,code);
cout<<"请输入电子邮件:
";
gets(email);
if(strcmp(email,"")!
=0)
strcpy(p->email,email);
hum*q=head;
while(q)
{//判断是否以存在相同的信息
if(strcmp(q->name,name)==0&&strcmp(q->address,address)==0&&strcmp(q->phone,phone)==0&&strcmp(q->code,code)==0&&strcmp(q->email,email)==0)
{
flag=0;
cout<<"已存在相同信息!
\n存入失败!
\n";
break;
}
q=q->next;
}
if(flag)
{
ofstreamoutfile;
outfile.open("cin.txt",ios:
:
app);//将新数据添加的文本末尾
if(!
outfile)//调试期间怕出错添加的判断函数
{
cerr<<"Fileopenorcreateerror!
"<exit
(1);
}
outfile<name<<""<address<<""<phone<<""<code<<""<email;
outfile.close();
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
cout<<"信息已经存入通讯录。
\n";
}
}
voidList:
:
Delete(intt)//删除函数
{
cout<<"请输入要删除人信息:
\n";
charshuru[N];
cin>>shuru;
hum*p=head,*q;
charflag;
while(p)
{
if((strcmp(p->name,shuru)==0&&t==1)||(strcmp(p->phone,shuru)==0&&t==2)||(strcmp(p->code,shuru)==0)&&t==3)
{
cout<<"确定要删除该信息yORn"<cin>>flag;
if(flag=='Y'||flag=='y')
{
if(p==head)
head=p->next;
else
q->next=p->next;
deletep;
break;
}
break;
}
q=p;
p=p->next;
}
ofstreamoutfile;
outfile.open("cin.txt",ios:
:
trunc);
if(!
outfile)//调试期间怕出错添加的判断函数
{
cerr<<"Fileopenorcreateerror!
"<exit
(1);
}
q=head;
while(q->next)//链表输出信息至文本
{
outfile<name<<""<address<<""<phone<<""<code<<""<email<q=q->next;
}//将删除后的信息存入cin.txt文件中保存
outfile<name<<""<address<<""<phone<<""<code<<""<email;
outfile.close();
system("cls");//每次程序的最终结果都清屏一次,并给出程序最后的结果,方便使用者查看
cout<<"程序运行结果:
\n";
if(flag=='Y'||flag=='y')
cout<<"已删除成功!
\n"<else
if(flag!
='Y'||flag!
='y')
cout<<"信息成员未删除.\n";
else
cout<<"通讯录无此人!
\n"<}
voidList:
:
Change(char*name)//修改信息函数
{
hum*p=head;//使用链表的查找
intflag=0;
while(p)
{
if(strcmp(p->name,name)==0)
{
cout<<"姓名:
\n"<name<<"地址:
"<address<<"电话:
"<phone<<"邮编:
"<code<<"E-mail:
"<email<getchar();
cout<<"------无需修改选项直接回车!
---------\n";//对于无需修改的项目不需要重复填入,方便使用者
cout<<"输入要修改成的地址:
";
gets(address);
cout<<"输入要修改成的电话:
";
gets(phone);
cout<<"输入要修改成的邮编:
";
gets(code);
cout<<"输入要修改成的E-mail:
";
gets(email);
if(strcmp(address,"")!
=0)
strcpy(p->address,address);
if(strcmp(code,"")!
=0)
strcpy(p->code,code);
if(strcmp(email,"")!
=0)
strcpy(p->email,email);
if(strcmp(phone,"")!
=0)
strcpy(p->phone,phone);
flag=1;
cout<break;
}
p=p->next;
}
hum*q=head;
ofstreamoutfile;
outfile.open("cin.txt",ios:
:
trunc);
if(!
outfile)//调试期间怕出错添加的判断函数
{
cerr<<"Fileopenorcreateerror!
"<exit
(1);
}
while(q->next)//链表输出信息至文本
{
outfile<name<<""<address<<""<phone<<""<code<<""<email<q=q->next;
}//将删除后的信息存入cin.txt文件中保存
outfile<name<<""<address<<""<phone<<""<code<<""<email;
outfile.close();
system("cls");
cout<if(flag==0)
{
cout<<"程序最终结果:
\n";
cout<<"通讯录不包含这个人!
\n"<}
else
{
cout<<"程序最终结果:
\n";
cout<<"姓名:
"<name<"<address<<"电话:
"<phone<<"邮编:
"<code<<"E-mail:
"<email<cout<<"信息修改成功!
\n"<}
cout<}
voidList:
:
Find(intx)//1、按名字查找2、按电话查找3、按邮编查找4、按E-mail查找"
{
charshuru[N];
cout<<"输入查找人信息:
\n";
cin>>shuru;
intflag=0;
hum*p=head;
while(p)
{//查找时4种条件,方便使用者。
if((strcmp(p->name,shuru)==0&&x==1)||(strcmp(p->phone,shuru)==0&&x==2)||(strcmp(p->code,shuru)==0)&&x==3||(strcmp(p->email,shuru)==0&&x==4))
{
system("cls");
cout<cout<<"程序最终结果:
\n查找人:
\n";
cout<<"姓名:
"<name<<"地址:
"<address<<"电话:
"<phone<<"邮编:
"<code<<"E-mail:
"<email<flag=1;
}
p=p->next;
}
cout<if(flag==0)
{
system("cls");
cout<<"程序最终结果:
\n";
cout<<"通讯录不包含这个人!
\n"<}
cout<}
voidList:
:
filecin()//默认导入文本信息至程序
{
ifstreamtxtfile;
txtfile.open("cin.txt");
if(!
txtfile)
{
cerr<<"txtopenerror!
"<exit
(1);
}
while(!
txtfile.eof())
{
txtfile>>name>>address>>phone>>code>>email;
List:
:
set(name,address,phone,code,email);
}
txtfile.close();
}
voidList:
:
fileout()//导出信息至文本
{
fstreamoutfile;
outfile.open("cout.txt",ios:
:
out|ios:
:
trunc);
if(!
outfile)//调试期间怕出错添加的判断函数
{
cerr<<"Fileopenorcreateerror!
"<exit
(1);
}
hum*p=head;
while(p)//链表输出信息至文本
{
outfile<name<<""<address<<""<phone<<""<code<<""<email<p=p->next;
}
system("cls");
cout<cout<<"程序最终结果:
\n";
printf("导出信息至文本成功!
\n");
outfile.close();
cout<}
voidList:
:
showall()//链表输出各个成员信息
{
hum*p=head;
while(p)
{
cout<<"姓名:
"<name<"<address<<"电话:
"<phone<<"邮编:
"<email<<"E-mail:
"<code<p=p->next;
}
}
intmain()
{
ListTXL;
intn=1;//n表示要输入的操作步骤
TXL.filecin();//程序打开默认导入记录在cin.txt中的文件内容。
while(n)
{
cout<<"欢迎使用通信簿!
\n";//用户友好界面
cout<<"请选择以下操作!
\n";
cout<<"!
-----1-添加通讯录信息\n";
cout<<"!
-----2-修改通讯录信息\n";
cout<<"!
-----3-删除通讯录信息\n";
cout<<"!
-----4-查找通讯录信息\n";
cout<<"!
-----5-输出通讯录信息\n";
cout<<"!
-----6-文本导出信息\n";
cout<<"!
-----7-退出通讯录\n";
cout<<"输入菜单中的操作步骤:
";
cin>>n;
switch(n)
{
case1:
TXL.CreatList();//在程序运行时添加成员
break;
case2:
cout<<"输入要修改的人的名字:
";
cin>>name;
TXL.Change(name);
break;
case3:
cout<<"1---根据姓名删除信息\n";//不同的删除方式。
cout<<"2---根据电话删除信息\n";
cout<<"3---根据E-mail删除信息\n";
intt;
cin>>t;
TXL.Delete(t);
break;
case4:
cout<<"查找方式"<cout<<"------1、按名字查找\n------2、按电话查找\n------3、按邮编查找\n------4、按E-mail查找"<cout<<"请输入查找方式:
";
intx;
cin>>x;
TXL.Find(x);
break;
case5:
TXL.showall();
break;
case6:
TXL.fileout();break;//文件导出最终结果
case7:
n=0;
break;
default:
cout<<"操作错误!
"<cout<}
}
return0;
}