急求c++图书管理系统 100
展开全部
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>//输入/输出文件流类
usingnamespacestd;
constintMaxr=100;//最多的读者
constintMaxb=100;//最多的图书
constintMaxbor=5;//每位读者最多借五本书
//读者类,实现对读者的信息的描述
classReader
{
private:
inttag;//删除标记1:已删0:未删
intno;//读者编号
charname[10];//读者姓名
intborbook[Maxbor];//所借图书
public:
Reader(){}
char*getname(){returnname;}//获取姓名
intgettag(){returntag;}//获取删除标记
intgetno(){returnno;}//获取读者编号
voidsetname(charna[])//设置姓名
{
strcpy(name,na);
}
voiddelbook(){tag=1;}//设置删除标记1:已删0:未删
voidaddreader(intn,char*na)//增加读者
{
tag=0;
no=n;
strcpy(name,na);
for(inti=0;i<Maxbor;i++)
borbook[i]=0;
}
voidborrowbook(intbookid)//借书操作
{
for(inti=0;i<Maxbor;i++)
{
if(borbook[i]==0)
{
borbook[i]=bookid;
return;
}
}
}
intretbook(intbookid)//还书操作
{
for(inti=0;i<Maxbor;i++)
{
if(borbook[i]==bookid)
{
borbook[i]=0;
return1;
}
}
return0;
}
voiddisp()//读出读者信息
{
cout<<setw(5)<<no<<setw(10)<<name<<"借书编号:[";
for(inti=0;i<Maxbor;i++)
if(borbook[i]!=0)
cout<<borbook[i]<<"|";
cout<<"]"<<endl;
}
};
//读者类库,实现建立读者的个人资料
classRDatabase
{
private:
inttop;//读者记录指针
Readerread[Maxr];//读者记录
public:
RDatabase()//构造函数,将reader.txt读到read[]中
{
Readers;
top=-1;
fstreamfile("reader.txt",ios::in);//打开一个输入文件
while(1)
{
file.read((char*)&s,sizeof(s));
if(!file)break;
top++;
read[top]=s;
}
file.close();//关闭reader.txt
}
voidclear()//删除所有读者信息
{
top=-1;
}
intaddreader(intn,char*na)//添加读者时先查找是否存在
{
Reader*p=query(n);
if(p==NULL)
{
top++;
read[top].addreader(n,na);
return1;
}
return0;
}
Reader*query(intreaderid)//按编号查找
{
for(inti=0;i<=top;i++)
if(read[i].getno()==readerid&&
read[i].gettag()==0)
{
return&read[i];
}
returnNULL;
}
voiddisp()//输出所有读者信息
{
for(inti=0;i<=top;i++)
read[i].disp();
}
voidreaderdata();//读者库维护
~RDatabase()//析构函数,将read[]写到reader.txt文件中
{
fstreamfile("reader.txt",ios::out);
for(inti=0;i<=top;i++)
if(read[i].gettag()==0)
file.write((char*)&read[i],sizeof(read[i]));
file.close();
}
};
voidRDatabase::readerdata()
{
charchoice;
charrname[20];
intreaderid;
Reader*r;
while(choice!='0')
{
cout<<"\n\n\t\t\t读者维护\n\n\n\t\t1新增\n\n\t\t2更改\n\n\t\t3删除\n\n\t\t4查找\n\n\t\t5显示\n\n\t\t6全删\n\n\t\t0退出"<<endl;
cin>>choice;
switch(choice)
{
case'1':
cout<<"输入读者编号:";
cin>>readerid;
cout<<"输入读者姓名:";
cin>>rname;
addreader(readerid,rname);
break;
case'2':
cout<<"输入读者编号:";
cin>>readerid;
r=query(readerid);
if(r==NULL)
{
cout<<"该读者不存在"<<endl;
break;
}
cout<<"输入新的姓名:";
cin>>rname;
r->setname(rname);
break;
case'3':
cout<<"输入读者编号:";
cin>>readerid;
r=query(readerid);
if(r==NULL)
{
cout<<"该读者不存在"<<endl;
break;
}
r->delbook();
break;
case'4':
cout<<"读入读者编号:";
cin>>readerid;
r=query(readerid);
if(r==NULL)
{
cout<<"该读者不存在"<<endl;
break;
}
r->disp();
break;
case'5':
disp();
break;
case'6':
clear();
break;
default:cout<<"输入错误,请从新输入:";break;
}
}
}
//图书类,实现对图书的描述,图书的编号,书名,借出,还入等
classBook
{
private:
inttag;//删除标记1:已删0:未删
intno;//图书编号
charname[20];//书名
intonshelf;//是否再架1:再架2:已借
public:
Book(){}
char*getname(){returnname;}//获取姓名
intgetno(){returnno;}//获取图书编号
intgettag(){returntag;}//获取删除标记
voidsetname(charna[])//设置书名
{
strcpy(name,na);
}
voiddelbook(){tag=1;}//删除图书
voidaddbook(intn,char*na)//增加图书
{
tag=0;
no=n;
strcpy(name,na);
onshelf=1;
}
intborrowbook()//借书操作
{
if(onshelf==1)
{
onshelf=0;
return1;
}
return0;
}
voidretbook()//还书操作
{
onshelf=1;
}
voiddisp()//输出图书
{
cout<<setw(6)<<no<<setw(18)<<name<<setw(10)
<<(onshelf==1?"在架":"已借")<<endl;
}
};
//图书库类,实现对图书的维护,查找,删除等
classBDatabase
{
private:
inttop;//图书记录指针
Bookbook[Maxb];//图书记录
public:
BDatabase()//构造函数,将book.txt读到book[]中
{
Bookb;
top=-1;
fstreamfile("book.txt",ios::in);
while(1)
{
file.read((char*)&b,sizeof(b));
if(!file)break;
top++;
book[top]=b;
}
file.close();
}
voidclear()//全删
{
top=-1;
}
intaddbook(intn,char*na)//增加图书
{
Book*p=query(n);
if(NULL==p)
{
top++;
book[top].addbook(n,na);
return1;
}
return0;
}
Book*query(intbookid)//查找图书
{
for(inti=0;i<=top;i++)
if(book[i].getno()==bookid&&book[i].gettag()==0)
{
return&book[i];
}
returnNULL;
}
voidbookdata();//图书库维护
voiddisp()
{
for(inti=0;i<=top;i++)
if(book[i].gettag()==0)
book[i].disp();
}
~BDatabase()//析构函数,将book[]写到book.txt文件中
{
fstreamfile("book.txt",ios::out);
for(inti=0;i<=top;i++)
if(book[i].gettag()==0)
file.write((char*)&book[i],sizeof(book[i]));
file.close();
}
};
voidBDatabase::bookdata()
{
charchoice;
charbname[40];
intbookid;
Book*b;
while(choice!='0')
{
cout<<"\n\n\n\t\t\t图书维护"<<endl<<endl;
cout<<"\t\t1新增\n\t\t2更改\n\t\t3删除\n\t\t4查找\n\t\t5显示\n\t\t6全删\n\t\t0退出"<<endl;
cin>>choice;
switch(choice)
{
case'1':
cout<<"输入图书编号:"<<endl;
cin>>bookid;
cout<<"输入图书书名:"<<endl;
cin>>bname;
addbook(bookid,bname);
break;
case'2':
cout<<"输入图书编号:"<<endl;
cin>>bookid;
b=query(bookid);
if(b==NULL)
{
cout<<"该图书不存在"<<endl;
break;
}
cout<<"输入新的书名:"<<endl;
cin>>bname;
b->setname(bname);
break;
case'3':
cout<<"读入图书编号:"<<endl;
cin>>bookid;
b=query(bookid);
if(b==NULL)
{
cout<<"该图书不存在"<<endl;
break;
}
b->delbook();
break;
case'4':
cout<<"读入图书编号:"<<endl;
cin>>bookid;
b=query(bookid);
if(b==NULL)
{
cout<<"该图书不存在"<<endl;
break;
}
b->disp();
break;
case'5':
disp();
break;
case'6':
clear();
break;
default:cout<<"输入错误,请从新输入:";
}
}
}
//main()函数的实现,程序的主界面的引导
voidmain()
{
charchoice;
intbookid,readerid;
RDatabaseReaderDB;
Reader*r;
BDatabaseBookDB;
Book*b;
while(choice!='0')
{
cout<<endl<<endl<<"\t\t\t图书管理系统\n\n\n";
cout<<"\t\t\t1借书\n\n\t\t\t2还书\n\n\t\t\t3图书维护\n\n\t\t\t4读者维护\n\n\t\t\t0离开"<<endl;
cin>>choice;
switch(choice)
{
case'1':
cout<<"借书读者编号:";
cin>>readerid;
cout<<"图书编号:";
cin>>bookid;
r=ReaderDB.query(readerid);//按编号查找
if(NULL==r)
{
cout<<"不存在该读者,不能借书"<<endl;
break;
}
b=BookDB.query(bookid);
if(b==NULL)
{
cout<<"不存在该图书,不能借书"<<endl;
break;
}
if(b->borrowbook()==0)
{
cout<<"该图书已借出,不能借书"<<endl;
break;
}
r->borrowbook(b->getno());
break;
case'2':
cout<<"还书\n读者编号:";
cin>>readerid;
cout<<"图书编号:";
cin>>bookid;
r=ReaderDB.query(readerid);
if(r==NULL)
{
cout<<"不存在该读者,不能还书"<<endl;
break;
}
b=BookDB.query(bookid);
if(b==NULL)
{
cout<<"不存在该图书,不能还书"<<endl;
break;
}
b->retbook();
r->retbook(b->getno());
break;
case'3':
BookDB.bookdata();
break;
case'4':
ReaderDB.readerdata();
break;
default:cout<<"输入错误,请从新输入:";
}
}
}
#include<iomanip>
#include<string>
#include<fstream>//输入/输出文件流类
usingnamespacestd;
constintMaxr=100;//最多的读者
constintMaxb=100;//最多的图书
constintMaxbor=5;//每位读者最多借五本书
//读者类,实现对读者的信息的描述
classReader
{
private:
inttag;//删除标记1:已删0:未删
intno;//读者编号
charname[10];//读者姓名
intborbook[Maxbor];//所借图书
public:
Reader(){}
char*getname(){returnname;}//获取姓名
intgettag(){returntag;}//获取删除标记
intgetno(){returnno;}//获取读者编号
voidsetname(charna[])//设置姓名
{
strcpy(name,na);
}
voiddelbook(){tag=1;}//设置删除标记1:已删0:未删
voidaddreader(intn,char*na)//增加读者
{
tag=0;
no=n;
strcpy(name,na);
for(inti=0;i<Maxbor;i++)
borbook[i]=0;
}
voidborrowbook(intbookid)//借书操作
{
for(inti=0;i<Maxbor;i++)
{
if(borbook[i]==0)
{
borbook[i]=bookid;
return;
}
}
}
intretbook(intbookid)//还书操作
{
for(inti=0;i<Maxbor;i++)
{
if(borbook[i]==bookid)
{
borbook[i]=0;
return1;
}
}
return0;
}
voiddisp()//读出读者信息
{
cout<<setw(5)<<no<<setw(10)<<name<<"借书编号:[";
for(inti=0;i<Maxbor;i++)
if(borbook[i]!=0)
cout<<borbook[i]<<"|";
cout<<"]"<<endl;
}
};
//读者类库,实现建立读者的个人资料
classRDatabase
{
private:
inttop;//读者记录指针
Readerread[Maxr];//读者记录
public:
RDatabase()//构造函数,将reader.txt读到read[]中
{
Readers;
top=-1;
fstreamfile("reader.txt",ios::in);//打开一个输入文件
while(1)
{
file.read((char*)&s,sizeof(s));
if(!file)break;
top++;
read[top]=s;
}
file.close();//关闭reader.txt
}
voidclear()//删除所有读者信息
{
top=-1;
}
intaddreader(intn,char*na)//添加读者时先查找是否存在
{
Reader*p=query(n);
if(p==NULL)
{
top++;
read[top].addreader(n,na);
return1;
}
return0;
}
Reader*query(intreaderid)//按编号查找
{
for(inti=0;i<=top;i++)
if(read[i].getno()==readerid&&
read[i].gettag()==0)
{
return&read[i];
}
returnNULL;
}
voiddisp()//输出所有读者信息
{
for(inti=0;i<=top;i++)
read[i].disp();
}
voidreaderdata();//读者库维护
~RDatabase()//析构函数,将read[]写到reader.txt文件中
{
fstreamfile("reader.txt",ios::out);
for(inti=0;i<=top;i++)
if(read[i].gettag()==0)
file.write((char*)&read[i],sizeof(read[i]));
file.close();
}
};
voidRDatabase::readerdata()
{
charchoice;
charrname[20];
intreaderid;
Reader*r;
while(choice!='0')
{
cout<<"\n\n\t\t\t读者维护\n\n\n\t\t1新增\n\n\t\t2更改\n\n\t\t3删除\n\n\t\t4查找\n\n\t\t5显示\n\n\t\t6全删\n\n\t\t0退出"<<endl;
cin>>choice;
switch(choice)
{
case'1':
cout<<"输入读者编号:";
cin>>readerid;
cout<<"输入读者姓名:";
cin>>rname;
addreader(readerid,rname);
break;
case'2':
cout<<"输入读者编号:";
cin>>readerid;
r=query(readerid);
if(r==NULL)
{
cout<<"该读者不存在"<<endl;
break;
}
cout<<"输入新的姓名:";
cin>>rname;
r->setname(rname);
break;
case'3':
cout<<"输入读者编号:";
cin>>readerid;
r=query(readerid);
if(r==NULL)
{
cout<<"该读者不存在"<<endl;
break;
}
r->delbook();
break;
case'4':
cout<<"读入读者编号:";
cin>>readerid;
r=query(readerid);
if(r==NULL)
{
cout<<"该读者不存在"<<endl;
break;
}
r->disp();
break;
case'5':
disp();
break;
case'6':
clear();
break;
default:cout<<"输入错误,请从新输入:";break;
}
}
}
//图书类,实现对图书的描述,图书的编号,书名,借出,还入等
classBook
{
private:
inttag;//删除标记1:已删0:未删
intno;//图书编号
charname[20];//书名
intonshelf;//是否再架1:再架2:已借
public:
Book(){}
char*getname(){returnname;}//获取姓名
intgetno(){returnno;}//获取图书编号
intgettag(){returntag;}//获取删除标记
voidsetname(charna[])//设置书名
{
strcpy(name,na);
}
voiddelbook(){tag=1;}//删除图书
voidaddbook(intn,char*na)//增加图书
{
tag=0;
no=n;
strcpy(name,na);
onshelf=1;
}
intborrowbook()//借书操作
{
if(onshelf==1)
{
onshelf=0;
return1;
}
return0;
}
voidretbook()//还书操作
{
onshelf=1;
}
voiddisp()//输出图书
{
cout<<setw(6)<<no<<setw(18)<<name<<setw(10)
<<(onshelf==1?"在架":"已借")<<endl;
}
};
//图书库类,实现对图书的维护,查找,删除等
classBDatabase
{
private:
inttop;//图书记录指针
Bookbook[Maxb];//图书记录
public:
BDatabase()//构造函数,将book.txt读到book[]中
{
Bookb;
top=-1;
fstreamfile("book.txt",ios::in);
while(1)
{
file.read((char*)&b,sizeof(b));
if(!file)break;
top++;
book[top]=b;
}
file.close();
}
voidclear()//全删
{
top=-1;
}
intaddbook(intn,char*na)//增加图书
{
Book*p=query(n);
if(NULL==p)
{
top++;
book[top].addbook(n,na);
return1;
}
return0;
}
Book*query(intbookid)//查找图书
{
for(inti=0;i<=top;i++)
if(book[i].getno()==bookid&&book[i].gettag()==0)
{
return&book[i];
}
returnNULL;
}
voidbookdata();//图书库维护
voiddisp()
{
for(inti=0;i<=top;i++)
if(book[i].gettag()==0)
book[i].disp();
}
~BDatabase()//析构函数,将book[]写到book.txt文件中
{
fstreamfile("book.txt",ios::out);
for(inti=0;i<=top;i++)
if(book[i].gettag()==0)
file.write((char*)&book[i],sizeof(book[i]));
file.close();
}
};
voidBDatabase::bookdata()
{
charchoice;
charbname[40];
intbookid;
Book*b;
while(choice!='0')
{
cout<<"\n\n\n\t\t\t图书维护"<<endl<<endl;
cout<<"\t\t1新增\n\t\t2更改\n\t\t3删除\n\t\t4查找\n\t\t5显示\n\t\t6全删\n\t\t0退出"<<endl;
cin>>choice;
switch(choice)
{
case'1':
cout<<"输入图书编号:"<<endl;
cin>>bookid;
cout<<"输入图书书名:"<<endl;
cin>>bname;
addbook(bookid,bname);
break;
case'2':
cout<<"输入图书编号:"<<endl;
cin>>bookid;
b=query(bookid);
if(b==NULL)
{
cout<<"该图书不存在"<<endl;
break;
}
cout<<"输入新的书名:"<<endl;
cin>>bname;
b->setname(bname);
break;
case'3':
cout<<"读入图书编号:"<<endl;
cin>>bookid;
b=query(bookid);
if(b==NULL)
{
cout<<"该图书不存在"<<endl;
break;
}
b->delbook();
break;
case'4':
cout<<"读入图书编号:"<<endl;
cin>>bookid;
b=query(bookid);
if(b==NULL)
{
cout<<"该图书不存在"<<endl;
break;
}
b->disp();
break;
case'5':
disp();
break;
case'6':
clear();
break;
default:cout<<"输入错误,请从新输入:";
}
}
}
//main()函数的实现,程序的主界面的引导
voidmain()
{
charchoice;
intbookid,readerid;
RDatabaseReaderDB;
Reader*r;
BDatabaseBookDB;
Book*b;
while(choice!='0')
{
cout<<endl<<endl<<"\t\t\t图书管理系统\n\n\n";
cout<<"\t\t\t1借书\n\n\t\t\t2还书\n\n\t\t\t3图书维护\n\n\t\t\t4读者维护\n\n\t\t\t0离开"<<endl;
cin>>choice;
switch(choice)
{
case'1':
cout<<"借书读者编号:";
cin>>readerid;
cout<<"图书编号:";
cin>>bookid;
r=ReaderDB.query(readerid);//按编号查找
if(NULL==r)
{
cout<<"不存在该读者,不能借书"<<endl;
break;
}
b=BookDB.query(bookid);
if(b==NULL)
{
cout<<"不存在该图书,不能借书"<<endl;
break;
}
if(b->borrowbook()==0)
{
cout<<"该图书已借出,不能借书"<<endl;
break;
}
r->borrowbook(b->getno());
break;
case'2':
cout<<"还书\n读者编号:";
cin>>readerid;
cout<<"图书编号:";
cin>>bookid;
r=ReaderDB.query(readerid);
if(r==NULL)
{
cout<<"不存在该读者,不能还书"<<endl;
break;
}
b=BookDB.query(bookid);
if(b==NULL)
{
cout<<"不存在该图书,不能还书"<<endl;
break;
}
b->retbook();
r->retbook(b->getno());
break;
case'3':
BookDB.bookdata();
break;
case'4':
ReaderDB.readerdata();
break;
default:cout<<"输入错误,请从新输入:";
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询