急 高手帮我看一下这个程序
#include<iostream>#include<fstream>
#include <stdlib.h> /*其它说明*/
#include <string.h> /*字符串操作函数*/
using namespace std;
class Book
{
private:
char name[8];
char sex[2];
int num;
char add[20];
Book *next;
public:
void ClearBook(Book *&Head); //清空链表
int BookSize(Book *Head); //求表长
bool BookEmpty(Book *Head); //检查表是否为空
int TraverseBook(Book *Head); //遍历表
void enterBook(Book *&Head); //向表中插如元素
void InsertBook(Book *&Head,char * str);
void FindBook(Book * Head, char * str); //查询一个元素
void UpdateBook(Book *Head, char * str); //修改一个元素
void DeleteBook(Book *&Head, char * str); //删除一个元素
Book* Read_File();
void Write_File(Book *&Head);
};
Book* Book::Read_File() //读取
{
int n = 0;
Book *Head = NULL;
Book *pa, *pb;
ifstream infile("Book.dat");
if(infile)
{
while(!infile.eof())
{
n++;
pa = new Book;
infile>>pa->name;
infile>>pa->sex;
infile>>pa->num;
infile>>pa->add;
if(n == 1)
Head = pb = pa;
else
{
pb->next = pa;
pb = pb->next;
pb->next = NULL;
}
}
}
infile.close();
return Head;
}
void Book::Write_File(Book *&Head)//存储入文件
{
if(!Head)
{
system("del Book.dat");
return;
}
else
{
ofstream outfile("Book.dat");
Book *NP = Head;
if(outfile)
{
while(true)
{
outfile<<NP->name<<" ";
outfile<<NP->sex<<" ";
outfile<<NP->num<<" ";
if(!NP->next)
{
outfile<<NP->add;
break;
}
outfile<<NP->add<<" ";
NP = NP->next;
}
}
outfile.close();
}
}
void Book::ClearBook(Book *&Head)
{
Book *cp,*np;
cp=Head;
while(cp!=NULL)
{
np=cp->next;
delete cp;
cp=np;
}
Head=NULL;
cout<<"通讯录清空!\n";
}
int Book::BookSize(Book *Head)
{
Book *p=Head;
int i=0;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}
bool Book::BookEmpty(Book *Head)
{
return (Head==NULL);
}
int Book::TraverseBook(Book *Head)
{
Book *p=Head;
if(p!=NULL)cout<<"姓名\t性别\t电话\t住址\n";
while(p!=NULL)
{
cout<<p->name<<"\t"<<p->sex<<"\t"<<p->num<<"\t"<<p->add<<endl;
p=p->next;
}
return 0;
}
void Book::enterBook(Book *&Head)
{
Book *new1;
new1=new Book;
cout<<"请输入姓名:";
cin>>new1->name;
cout<<endl;
cout<<"请输入性别:";
cin>>new1->sex;
cout<<endl;
cout<<"请输入联系电话:";
cin>>new1->num;
cout<<endl;
cout<<"请输入联系地址:";
cin>>new1->add;
{
new1->next=Head;
Head=new1;
}
}
void Book::InsertBook(Book *&Head,char *str)
{
if(Head==NULL)
cout<<"没有资料"<<endl;
else
{
Book *p=Head,*q=NULL;
while(p!=NULL)
if(strcmp(p->name,str)==0)
break;
else
{
q=p;
p=p->next;
}
if(p==NULL)
cout<<"对不起,找不到你要插入的位置。"<<endl;
else
{
Book *new1;
new1=new Book;
cout<<"请输入姓名:";
cin>>new1->name;
cout<<endl;
cout<<"请输入性别:";
cin>>new1->sex;
cout<<endl;
cout<<"请输入联系电话:";
cin>>new1->num;
cout<<endl;
cout<<"请输入联系地址:";
cin>>new1->add;
new1->next=p->next;
p->next=new1;
cout<<"插入成功!"<<endl;
}
}
}
void Book::FindBook(Book * Head, char *str)
{
Book *p=Head;
cout<<"请输入你想要查找的人的姓名:";
while(p!=NULL)
{
while(strcmp(p->name,str)!=0)
{
p=p->next;
}
break;
}
cout<<"========================================"<<endl;
cout<<endl;
cout<<" 姓名:"<<p->name<<"\t性别:"<<p->sex<<endl;
cout<<" 电话:"<<p->num<<"\t地址:"<<p->add<<endl;
cout<<endl;
cout<<"========================================"<<endl;
}
void Book::UpdateBook(Book *Head, char *str)
{
Book *p=Head;
cout<<" ! 对不起,没有你要修改的人 ";
while(p!=NULL)
{
while(strcmp(p->name,str)!=0)
{
p=p->next;
}
cout<<"请输入新的资料"<<endl;
cout<<"请输入姓名:";
cin>>p->name;
cout<<"请输入性别:";
cin>>p->sex;
cout<<"请输入联系电话:";
cin>>p->num;
cout<<"请输入联系地址:";
cin>>p->add;
break;
}
}
void Book::DeleteBook(Book *&Head, char *str)
{
if(Head==NULL)
cout<<"没有资料"<<endl;
else
{
Book *p=Head,*q=NULL;
while(p!=NULL)
if(strcmp(p->name,str)==0)
break;
else
{
q=p;
p=p->next;
}
if(p==NULL)
cout<<" 对不起,找不到你要删除的人。"<<endl;
else
{
if(q==NULL)
Head=Head->next;
else
q->next=p->next;
delete p;
cout<<"此人已删除"<<endl;
}
}
}
void main()
{
cout<<" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
cout<<endl;
cout<<" 学生信息管理系统 \n";
cout<<endl;
cout<<" 1.输出记录 2.输入记录 \n";
cout<<" 3.插入记录 4.删除记录 \n";
cout<<" 5.修改记录 6.查找记录 \n";
cout<<" 7.清除所有 8.保存退出 \n";
cout<<" 0.退出不保存 \n";
cout<<" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━ \n";
cout<<"请选择您的操作:";
int a;
cin>>a;
char x[8];
Book *z,b;
z = b.Read_File();
while(a!=0)
{
switch(a)
{
case 1: b.TraverseBook(z);
int i;
i=b.BookSize(z);
cout<<"通讯录中的人数:"<<i<<endl;
cout<<"请选择您的操作:";
break;
case 2:b.enterBook(z);
cout<<"请选择您的操作:";
break;
case 3:
{cout<<"选择插入的位置:";
cin>>x;
b.InsertBook(z,x);
}
cout<<"请选择您的操作:";
break;
case 4:
{cout<<"请输入你想要删除的人的姓名:";
cin>>x;
b.DeleteBook(z,x);
}
cout<<"请选择您的操作:";
break;
case 5:
{cout<<"请输入你想要修改的人的姓名:";
cin>>x;
b.UpdateBook(z,x);
}
cout<<"请选择您的操作:";
break;
case 6:
{cout<<"请输入你想要查找的人的姓名:";
cin>>x;
b.FindBook(z,x);
}
cout<<"请选择您的操作:";
break;
case 7:
b.ClearBook(z);
cout<<"请选择您的操作:";
break;
case 8:
b.Write_File(z);
case 0:
exit(0);
default:cout<<"输入错误请重新选择:";
}
cin>>a;
}
}
为什么在读取文件时老是读取内存失败
邮箱 tanshaoshi208@
119762333