16,467
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include <fstream>
#include<string>
#include<cmath>
using namespace std;
struct TEL
{
char name[20];
char addrass[20];
char zip[20];
char telephone[20];
TEL *next;
};
class book
{
public:
void showmenu(); //菜单
void Appenditem(); //添加条目
void print(); //输出条目
void Finditem1(); //查找条目( 按姓名 )
void Removeitem(); //删除信息
void Saveandfree(); //保存到文件
void Open(); //打开文件
private:
TEL *head;
};
static book s;
void book::showmenu()
{
cout<<"*****************通讯录系统*****************"<<endl;
cout<<"\t1.添加条目"<<endl;
cout<<"\t2.输出输出"<<endl;
cout<<"\t3.按姓名查询"<<endl;
cout<<"\t4.删除条目"<<endl;
cout<<"\t0.保存并退出"<<endl;
cout<<"********************************************"<<endl;
cout<<"请选择"<<endl;
}
//添加条目
void book::Appenditem()
{
TEL *p1=NULL,*p2=NULL;
p1=new TEL; //申请结点
memset(p1,0,sizeof(TEL));
cout<<"输入姓名:"<<endl; //添加信息
cin>>p1->name;
cout<<"输入地址:"<<endl;
cin>>p1->addrass;
cout<<"输入邮编:"<<endl;
cin>>p1->zip;
cout<<"输入电话:"<<endl;
cin>>p1->telephone;
p1->next=NULL; //保存到链表
if(head==NULL)
{
head=p1;
}
else
{
for(p2=head;p2->next!=NULL;p2=p2->next); //找到结点尾
p2->next=p1;
}
cout<<"此信息已添加!"<<endl;
}
//输出学生信息
void book::print()
{
TEL *p=NULL;
if(head==NULL)
{
cout<<"此通讯录中无记录,请输入记录后在使用本功能!"<<endl;
return;
}
cout<<"**************通讯录系统*********************"<<endl; //输出信息
cout<<"姓名\t地址\t邮编\t电话"<<endl;
for(p=head;p!=NULL;p=p->next)
printf("%s\t%s\t%s\t%s\n",p->name,p->addrass,p->zip,p->telephone);
}
//查找信息1.按姓名
void book::Finditem1()
{
TEL *p;
char findname[20];
cout<<"请输入要查找的姓名:"<<endl;
cin>>findname;
cout<<"**************通讯录系统*********************"<<endl;
cout<<"姓名\t地址\t邮编\t电话"<<endl;
bool bfind = false;
for(p=head;p!=NULL;p=p->next)
{
if(strcmp(p->name,findname)==0)
{
cout<<p->name<<'\t'<<p->addrass<<'\t'<<p->zip<<'\t'<<p->telephone<<endl;
bfind = true;
}
}
if(!bfind)
{
cout<<"无此节点"<<endl;
}
}
//删除信息
void book::Removeitem()
{
char findname[20]; //先查找 后删除
TEL *p = head, *pr = head;
cout<<"请输入要删除的姓名:"<<endl;
cin>>findname;
if (head == NULL)
{
cout<<"无此节点!"<<endl;
return;
}
while (p != NULL && (strcmp(p->name,findname)!=0 ) )
{
pr = p;
p = p->next;
}
if (strcmp(findname, p->name)==0) //输出删除信息
{
cout<<p->name<<'\t'<<p->addrass<<'\t'<<p->zip<<'\t'<<p->telephone<<endl;
if (p == head)
head = head->next;
else
pr->next = p->next;
delete p;
cout<<"此信息已删除!"<<endl;
}
}
//保存链表信息到文件并释放内存空间
void book::Saveandfree()
{
TEL *p=NULL;
fstream ofile;
string strBook="books.txt";
if(head==NULL)
{
cout<<"\n记录为空!\n";
return;
}
else
{
ofile.open( strBook.c_str(), ios_base::out );
if(!ofile.is_open())
{
cout<<("\n打不开文件!\n");
return;
}
int pos = 0;
while(head!=NULL) //保存信息
{
p=head;
head=head->next;
p->next=NULL;
ofile.write((char*)p,sizeof(TEL));
pos += sizeof(TEL);
ofile.seekp(pos);
delete p;
}
cout<<("保存完毕!\n");
ofile.close();
}
}
//文件信息输出到链表
void book::Open()
{
fstream ifile;
TEL *p1=NULL,*p2=NULL;
ifile.open("books.txt",ios_base::in);
if(!ifile.is_open())
{
cout<<"\n****************这是一个新的通讯录管理系统******************\n";
return;
}
head=NULL;
int pos = 0;
p1=new TEL;
memset(p1,0,sizeof(TEL));
while(ifile.read((char*)p1,sizeof(TEL))) //循环读取
{
p1->next=NULL;
pos += sizeof(TEL);
ifile.seekg( pos );
if( NULL == head )
{
head=p1;
p2=head;
}
else
{
p2->next=p1;
p2=p2->next;
}
p1=new TEL;
memset(p1,0,sizeof(TEL));
}
delete p1;
ifile.close();
}
void main()
{
char ch;
s.Open(); //打开文件
while(1)
{
s.showmenu(); //显示菜单
cin>>ch;
switch(ch)
{
case '1':s.Appenditem(); //添加条目
break;
case '2':s.print(); //输出条目
break;
case '3':s.Finditem1(); //查找条目1.按姓名
break;
case '4': s.Removeitem(); //删除信息
s.print(); //输出删除后的结果
break;
case '0':s.Saveandfree(); //保存并释放内存
exit(0); //退出
break;
default:
cout<<"选择错误!"<<endl;
break;
}
}
}