c语言图书管理系统 急需!!
题目要求试图书管理系统设计要求图书信息包括:登录号,书名,作者号,分类号,出版单位,出版时间,价格等。试设计一图书信息管理系统,使之能提供以下功能;1,系统以菜单方式工作...
题目要求试 图书管理系统设计
要求
图书信息包括:登录号,书名,作者号,分类号,出版单位,出版时间,价格等。试设计一图书信息管理系统,使之能提供以下功能;
1,系统以菜单方式工作。
2,图书信息录入功能(图书信息用文件保存)
3,图书信息浏览功能。
4,图书信息查询功能。
5,图书信息的删除与修改(可选项)
提示
建立图书信息结构体,结构体包括登录号,书名,作者号,分类号,出版单位,出版时间,价格。希望能有详细的代码解释,望能得到帮助!!!!
别复制粘贴好不 展开
要求
图书信息包括:登录号,书名,作者号,分类号,出版单位,出版时间,价格等。试设计一图书信息管理系统,使之能提供以下功能;
1,系统以菜单方式工作。
2,图书信息录入功能(图书信息用文件保存)
3,图书信息浏览功能。
4,图书信息查询功能。
5,图书信息的删除与修改(可选项)
提示
建立图书信息结构体,结构体包括登录号,书名,作者号,分类号,出版单位,出版时间,价格。希望能有详细的代码解释,望能得到帮助!!!!
别复制粘贴好不 展开
6个回答
展开全部
我简单给你说一下吧。
1、首先定义数据结构
图书结构:图书编号、图书名称、单价、作者、存在状态
人员结构:人员编号(学生为学号,教师为工作编号)、姓名、性别等
借书结构:人员编号、图书编号、借书时间、还书时间
2、由于是C系统,通常情况下,数据要存储在文件中,因此,还需要定义文件结构:图书、人员、借书信息。
为了数据处理速度,可将借书信息中已经还书的信息单独放在还书信息中,借书信息中的数据只是已经借出还没有归还的数据。
3、系统功能定义,以及编码。
由于系统太过庞大,你还是自己试着做一下吧,应该可以做好的。
学习本身就是做程序,如果有困难,可以上我们团队提问题,随时恭候!
祝学习进步!
1、首先定义数据结构
图书结构:图书编号、图书名称、单价、作者、存在状态
人员结构:人员编号(学生为学号,教师为工作编号)、姓名、性别等
借书结构:人员编号、图书编号、借书时间、还书时间
2、由于是C系统,通常情况下,数据要存储在文件中,因此,还需要定义文件结构:图书、人员、借书信息。
为了数据处理速度,可将借书信息中已经还书的信息单独放在还书信息中,借书信息中的数据只是已经借出还没有归还的数据。
3、系统功能定义,以及编码。
由于系统太过庞大,你还是自己试着做一下吧,应该可以做好的。
学习本身就是做程序,如果有困难,可以上我们团队提问题,随时恭候!
祝学习进步!
展开全部
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>//输入/输出文件流类
using namespace std;
const int Maxr=100;//最多的读者
const int Maxb=100;//最多的图书
const int Maxbor=5;//每位读者最多借五本书
//读者类,实现对读者的信息的描述
class Reader
{
private:
int tag; //删除标记 1:已删 0:未删
int no; //读者编号
char name[10]; //读者姓名
int borbook[Maxbor];//所借图书
public:
Reader() {}
char *getname() {return name;} //获取姓名
int gettag() {return tag;} //获取删除标记
int getno() {return no;} //获取读者编号
void setname(char na[]) //设置姓名
{
strcpy(name,na);
}
void delbook(){ tag=1; }//设置删除标记 1:已删 0:未删
void addreader(int n,char *na)//增加读者
{
tag=0;
no=n;
strcpy(name,na);
for(int i=0;i<Maxbor;i++)
borbook[i]=0;
}
void borrowbook(int bookid)//借书操作
{
for(int i=0;i<Maxbor;i++)
{
if (borbook[i]==0)
{
borbook[i]=bookid;
return;
}
}
}
int retbook(int bookid)//还书操作
{
for(int i=0;i<Maxbor;i++)
{
if(borbook[i]==bookid)
{
borbook[i]=0;
return 1;
}
}
return 0;
}
void disp()//读出读者信息
{
cout << setw(5) << no <<setw(10) << name<<"借书编号:[";
for(int i=0;i<Maxbor;i++)
if(borbook[i]!=0)
cout << borbook[i] << "|";
cout << "]"<<endl;
}
};
//读者类库,实现建立读者的个人资料
class RDatabase
{
private:
int top; //读者记录指针
Reader read[Maxr];//读者记录
public:
RDatabase() //构造函数,将reader.txt读到read[]中
{
Reader s;
top=-1;
fstream file("reader.txt",ios::in);//打开一个输入文件
while (1)
{
file.read((char *)&s,sizeof(s));
if (!file)break;
top++;
read[top]=s;
}
file.close(); //关闭 reader.txt
}
void clear()//删除所有读者信息
{
top=-1;
}
int addreader(int n,char *na)//添加读者时先查找是否存在
{
Reader *p=query(n);
if (p==NULL)
{
top++;
read[top].addreader(n,na);
return 1;
}
return 0;
}
Reader *query(int readerid)//按编号查找
{
for (int i=0;i<=top;i++)
if (read[i].getno()==readerid &&
read[i].gettag()==0)
{
return &read[i];
}
return NULL;
}
void disp() //输出所有读者信息
{
for (int i=0;i<=top;i++)
read[i].disp();
}
void readerdata();//读者库维护
~RDatabase() //析构函数,将read[]写到reader.txt文件中
{
fstream file("reader.txt",ios::out);
for (int i=0;i<=top;i++)
if (read[i].gettag()==0)
file.write((char *)&read[i],sizeof(read[i]));
file.close();
}
};
void RDatabase::readerdata()
{
char choice;
char rname[20];
int readerid;
Reader *r;
while (choice!='0')
{
cout <<"\n\n\t\t\t读 者 维 护\n\n\n\t\t 1 新 增\n\n\t\t 2 更 改\n\n\t\t 3 删 除\n\n\t\t 4 查 找\n\n\t\t 5 显 示\n\n\t\t 6 全 删\n\n\t\t 0 退 出"<<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;
}
}
}
//图书类,实现对图书的描述,图书的编号,书名,借出,还入等
class Book
{
private:
int tag;//删除标记 1:已删 0:未删
int no;//图书编号
char name[20];//书名
int onshelf;//是否再架 1:再架 2:已借
public:
Book(){}
char *getname() { return name; }//获取姓名
int getno(){ return no; }//获取图书编号
int gettag(){ return tag; }//获取删除标记
void setname(char na[])//设置书名
{
strcpy(name,na);
}
void delbook(){ tag=1;}//删除图书
void addbook(int n,char *na)//增加图书
{
tag=0;
no=n;
strcpy(name,na);
onshelf=1;
}
int borrowbook()//借书操作
{
if (onshelf==1)
{
onshelf=0;
return 1;
}
return 0;
}
void retbook()//还书操作
{
onshelf=1;
}
void disp()//输出图书
{
cout << setw(6) << no << setw(18) << name << setw(10)
<<(onshelf==1? "在架":"已借") <<endl;
}
};
//图书库类,实现对图书的维护,查找,删除等
class BDatabase
{
private:
int top; //图书记录指针
Book book[Maxb]; //图书记录
public:
BDatabase()//构造函数,将book.txt读到book[]中
{
Book b;
top=-1;
fstream file("book.txt",ios::in);
while (1)
{
file.read((char *)&b,sizeof(b));
if (!file) break;
top++;
book[top]=b;
}
file.close();
}
void clear()//全删
{
top=-1;
}
int addbook(int n,char *na)//增加图书
{
Book *p=query(n);
if (NULL==p)
{
top++;
book[top].addbook(n,na);
return 1;
}
return 0;
}
Book *query(int bookid)//查找图书
{
for (int i=0;i<=top;i++)
if (book[i].getno()==bookid &&book[i].gettag()==0)
{
return &book[i];
}
return NULL;
}
void bookdata();//图书库维护
void disp()
{
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
book[i].disp();
}
~BDatabase()//析构函数,将book[]写到book.txt文件中
{
fstream file("book.txt",ios::out);
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
file.write((char *)&book[i],sizeof(book[i]));
file.close();
}
};
void BDatabase::bookdata()
{
char choice;
char bname[40];
int bookid;
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() 函数的实现,程序的主界面的引导
void main()
{
char choice;
int bookid,readerid;
RDatabase ReaderDB;
Reader *r;
BDatabase BookDB;
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>//输入/输出文件流类
using namespace std;
const int Maxr=100;//最多的读者
const int Maxb=100;//最多的图书
const int Maxbor=5;//每位读者最多借五本书
//读者类,实现对读者的信息的描述
class Reader
{
private:
int tag; //删除标记 1:已删 0:未删
int no; //读者编号
char name[10]; //读者姓名
int borbook[Maxbor];//所借图书
public:
Reader() {}
char *getname() {return name;} //获取姓名
int gettag() {return tag;} //获取删除标记
int getno() {return no;} //获取读者编号
void setname(char na[]) //设置姓名
{
strcpy(name,na);
}
void delbook(){ tag=1; }//设置删除标记 1:已删 0:未删
void addreader(int n,char *na)//增加读者
{
tag=0;
no=n;
strcpy(name,na);
for(int i=0;i<Maxbor;i++)
borbook[i]=0;
}
void borrowbook(int bookid)//借书操作
{
for(int i=0;i<Maxbor;i++)
{
if (borbook[i]==0)
{
borbook[i]=bookid;
return;
}
}
}
int retbook(int bookid)//还书操作
{
for(int i=0;i<Maxbor;i++)
{
if(borbook[i]==bookid)
{
borbook[i]=0;
return 1;
}
}
return 0;
}
void disp()//读出读者信息
{
cout << setw(5) << no <<setw(10) << name<<"借书编号:[";
for(int i=0;i<Maxbor;i++)
if(borbook[i]!=0)
cout << borbook[i] << "|";
cout << "]"<<endl;
}
};
//读者类库,实现建立读者的个人资料
class RDatabase
{
private:
int top; //读者记录指针
Reader read[Maxr];//读者记录
public:
RDatabase() //构造函数,将reader.txt读到read[]中
{
Reader s;
top=-1;
fstream file("reader.txt",ios::in);//打开一个输入文件
while (1)
{
file.read((char *)&s,sizeof(s));
if (!file)break;
top++;
read[top]=s;
}
file.close(); //关闭 reader.txt
}
void clear()//删除所有读者信息
{
top=-1;
}
int addreader(int n,char *na)//添加读者时先查找是否存在
{
Reader *p=query(n);
if (p==NULL)
{
top++;
read[top].addreader(n,na);
return 1;
}
return 0;
}
Reader *query(int readerid)//按编号查找
{
for (int i=0;i<=top;i++)
if (read[i].getno()==readerid &&
read[i].gettag()==0)
{
return &read[i];
}
return NULL;
}
void disp() //输出所有读者信息
{
for (int i=0;i<=top;i++)
read[i].disp();
}
void readerdata();//读者库维护
~RDatabase() //析构函数,将read[]写到reader.txt文件中
{
fstream file("reader.txt",ios::out);
for (int i=0;i<=top;i++)
if (read[i].gettag()==0)
file.write((char *)&read[i],sizeof(read[i]));
file.close();
}
};
void RDatabase::readerdata()
{
char choice;
char rname[20];
int readerid;
Reader *r;
while (choice!='0')
{
cout <<"\n\n\t\t\t读 者 维 护\n\n\n\t\t 1 新 增\n\n\t\t 2 更 改\n\n\t\t 3 删 除\n\n\t\t 4 查 找\n\n\t\t 5 显 示\n\n\t\t 6 全 删\n\n\t\t 0 退 出"<<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;
}
}
}
//图书类,实现对图书的描述,图书的编号,书名,借出,还入等
class Book
{
private:
int tag;//删除标记 1:已删 0:未删
int no;//图书编号
char name[20];//书名
int onshelf;//是否再架 1:再架 2:已借
public:
Book(){}
char *getname() { return name; }//获取姓名
int getno(){ return no; }//获取图书编号
int gettag(){ return tag; }//获取删除标记
void setname(char na[])//设置书名
{
strcpy(name,na);
}
void delbook(){ tag=1;}//删除图书
void addbook(int n,char *na)//增加图书
{
tag=0;
no=n;
strcpy(name,na);
onshelf=1;
}
int borrowbook()//借书操作
{
if (onshelf==1)
{
onshelf=0;
return 1;
}
return 0;
}
void retbook()//还书操作
{
onshelf=1;
}
void disp()//输出图书
{
cout << setw(6) << no << setw(18) << name << setw(10)
<<(onshelf==1? "在架":"已借") <<endl;
}
};
//图书库类,实现对图书的维护,查找,删除等
class BDatabase
{
private:
int top; //图书记录指针
Book book[Maxb]; //图书记录
public:
BDatabase()//构造函数,将book.txt读到book[]中
{
Book b;
top=-1;
fstream file("book.txt",ios::in);
while (1)
{
file.read((char *)&b,sizeof(b));
if (!file) break;
top++;
book[top]=b;
}
file.close();
}
void clear()//全删
{
top=-1;
}
int addbook(int n,char *na)//增加图书
{
Book *p=query(n);
if (NULL==p)
{
top++;
book[top].addbook(n,na);
return 1;
}
return 0;
}
Book *query(int bookid)//查找图书
{
for (int i=0;i<=top;i++)
if (book[i].getno()==bookid &&book[i].gettag()==0)
{
return &book[i];
}
return NULL;
}
void bookdata();//图书库维护
void disp()
{
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
book[i].disp();
}
~BDatabase()//析构函数,将book[]写到book.txt文件中
{
fstream file("book.txt",ios::out);
for (int i=0;i<=top;i++)
if (book[i].gettag()==0)
file.write((char *)&book[i],sizeof(book[i]));
file.close();
}
};
void BDatabase::bookdata()
{
char choice;
char bname[40];
int bookid;
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() 函数的实现,程序的主界面的引导
void main()
{
char choice;
int bookid,readerid;
RDatabase ReaderDB;
Reader *r;
BDatabase BookDB;
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<iostream.h>
#include<fstream.H>
#include<stdlib.h>
#include<string.h>
struct bookData
{
int booknumber;
char bookname[30];
int store;
};
struct Person
{
char name[20];
char studynumber[10];
int count;
bookData Rbook[2];
//bookData Rbook;
};
class History
{
public:
History(fstream& a){readerbook=a;}
void writehistory();
void readhistory(fstream&);
private:
fstream readerbook;
};
class Liberian;
void Find(fstream&);
void CreateTxt(fstream&);
class Reader
{
friend Liberian;
public:
Reader(fstream&);
void print();
void handlebook(fstream&);
void change();
bool findreader();
void Addreader();
void deletereader();
private:
fstream file;
Person person;
};
class Liberian
{
public:
Liberian(char *,char *);
void changeReader(Reader&);
void selfprint();
void Append(fstream&);
private:
char workname[20];
char worknumber[20];
};
int main()
{
fstream bookRecord(" book.dat",ios::in| ios::out);
if(!bookRecord)
{
cerr<<" Can't open,try again!"<<endl;
exit(1);
}
fstream readerRecord("reader.dat",ios::in|ios::out);
if(!readerRecord)
{
cerr<<" Can't open,try again!"<<endl;
exit(1);
}
int choice;
int key; Reader reader(readerRecord);//读者管理系统
Liberian liberian("李天","123456");//图书管理员
History rec(readerRecord);//声明显示历史记录类
while(1)
{
cout<<" 请输入您的选择:\n\n"
<<"1--图书管理员\n"
<<"2--读者系统\n"
<<"0--结束程序\n";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<" Hello,图书管理员:\n";
liberian.selfprint();cout<<endl;
cout<<"1-- 增加新图书\n"
<<"2-- 改变读者的属性\n"
<<"3-- 显示历史记录\n"
<<"0-- 返回\n";
cin>>key;
switch(key)
{
case 1:
liberian.Append(bookRecord);
break;
case 2:
liberian.changeReader(reader);
break;
case 3:
rec.readhistory(readerRecord);
case 0:
break ;
}
}
break;
case 2:
{
cout<<"学生读者\n"; int iflag=1;
//reader.findreader();
cout<<"1-- 借书与还书信息\n"
<<"2-- 修改个人信息\n"
<<"0-- 返回\n";
cin>>key;
switch(key)
{
case 1:
reader.handlebook(bookRecord);
break;
case 2:
reader.change();
break;
case 0:
break;
}
}
break;
default:
return 0;
}
}
return 0;
}
//增加书籍
void Liberian::Append(fstream& storef)
{
bookData book;
int choice;
int key;
int num;
storef.seekp(0,ios::end);
int posEnd=storef.tellp();
cout<<"*************书籍入库***************"<<endl;
while(1)
{
cout<<" Please enter your choice:"<<endl;
cout<<"1--添加新书\n"<<"2--已有书籍\n"<<"0--返回\n";
cin>>choice;
switch(choice)
{
case 1:
cout<<" 书号, 书名, 数量?"<<endl;
cin>>book.booknumber>>book.bookname>>book.store;
storef.write(reinterpret_cast<char*>(&book),sizeof(bookData));
break;
case 2:
storef.seekg(0,ios::beg);
cout<<"Booknumber?\n";
cin>>key;
do{
storef.read(reinterpret_cast<char*>(&book),sizeof(bookData));
}while(book.booknumber!=key&&storef.tellg()!=posEnd);
if(book.booknumber==key)
{
cout<<book.booknumber<<"----"<<book.bookname<<"----"<<book.store<<endl;
cout<<" 输入已有书增加的数量:"<<endl;
cin>>num;
if(num>0)
book.store+=num;
else
{
cout<<" Invalid input"<<endl;
}
storef.seekp(-long(sizeof(bookData)),ios::cur);////////////////////
storef.write(reinterpret_cast<char*>(&book),sizeof(bookData));
cout<<"现在书籍: "<<book.bookname<<" 余量为: "<<book.store<<endl;
}
else
cout<<"找不到此书,请重新查阅书号是否正确!"<<endl;
break;
case 0:
return ;
}
}
}
//书籍查询
void Find(fstream & f)
{
bookData book;
int key;
int choice;
f.seekg(0,ios::end);
int posEnd=f.tellp();
cout<<"*************书籍查询***************"<<endl;
while(1)
{
cout<<" 请输入您的选择\n"
<<" 1-- 检索一本书\n"
<<"2-- 显示全部书籍\n"
<<"0-- 返回\n";
cin>>choice;
switch(choice)
{
case 1:
f.seekg(0,ios::beg);
cout<<"输入你想检索书的书号"<<endl;
cin>>key;
do{
f.read(reinterpret_cast<char*>(&book),sizeof(bookData));
}while(book.booknumber!=key&&f.tellg()!=posEnd);
if(book.booknumber==key)
cout<<book.booknumber<<"---"<<book.bookname<<"---"<<book.store<<endl;
else
cout<<"找不到此书,请重新确认!"<<endl;
break;
case 2:
f.seekg(0,ios::beg);
do{
f.read(reinterpret_cast<char*>(&book),sizeof(bookData));
cout<<book.booknumber<<"---"<<book.bookname<<"--"<<book.store<<endl;
}while(f.tellg()!=posEnd);
break;
case 0:
return ;
}
}
}
Reader::Reader(fstream& c)
{
file=c;
Person person={"0","0",0,{{0,"0",0}, {0,"0",0}}};
}
void Reader::print()
{
cout<<"Name"<<"---"<<person.name<<'\n\n'<<"studynumber---"<<person.studynumber<<'\n';
}
//操作书籍??????????????????????
void Reader::handlebook(fstream &filee)
{
int key; int choice;
bookData book; bool iflag;int num=0;
filee.seekp(0,ios::end);
int posEnd=filee.tellp();
while(!(iflag=findreader()))
{
num++;
if(num>=3)
return;
}
//cout<<person.name;
while(1)
{
cout<<"1-- 借书\n"
<<"2-- 还书\n"
<<"3-- 查找一本书\n"
<<"0-- 返回"<<'\n';
cin>>choice;
switch(choice)
{
case 1:
{
filee.seekp(0,ios::end);
int posEnd=filee.tellp();
if(person.count<=1)
{
cout<<" 输入你要借阅图书的书号:"<<endl;
cin>>key;
filee.seekg(0);
do{
filee.read(reinterpret_cast<char*>(&book),sizeof(bookData));
}while(book.booknumber!=key&&filee.tellp()!=posEnd);
//cout<<book.booknumber<<endl;
if(book.booknumber==key)
{
//cout<<" 找到你想借阅的图书,成功借阅!"<<endl;
if(book.store>0)
{
person.Rbook[person.count]=book;
person.count+=1;
book.store-=1;
filee.seekp(-long(sizeof(bookData)),ios::cur);
filee.write(reinterpret_cast<char*>(&book),sizeof(bookData));
cout<<" 找到你想借阅的图书,成功借阅!"<<endl;
cout<<"书籍 :"<<book.bookname<<"剩余的本数为:"<<book.store<<endl;
}
else
cout<<" 现在此图书已经被借完了,请耐心等待几天!"<<endl;
}
else
cout<<" 找不到你想要的图书"<<endl;
}
else
cout<<"你最多只能借2本\n\n";
}
break;
case 2:
{
bookData blankbook={0,"0",0};
//if(person.count>0)
//{
int iflag=0;
filee.seekg(0);
cout<<" 输入你想要还的书的书号:"<<endl;
cin>>key;
for(int i=0;i<2;i++)
{
if(key==person.Rbook[i].booknumber)
{
person.Rbook[i]=blankbook;
cout<<"成功归还此书!";
iflag=1;
person.count--;
do{
filee.read(reinterpret_cast<char*>(&book),sizeof(bookData));
}while(book.booknumber!=key&&filee.tellp()!=posEnd);
if(book.booknumber==key)
{
book.store+=1;
filee.seekp(-long(sizeof(bookData)),ios::cur);
filee.write(reinterpret_cast<char*>(&book),sizeof(bookData));
cout<<"书籍 :"<<book.bookname<<"余本量为: "<<book.store<<endl;
}
}
}
if(!iflag)
{
cout<<" 你没有借阅那本书,请确认!"<<endl;
}
// }
// else
// cout<<"你没有借书,请重新确认!\n\n";
}
break;
case 3:
Find(filee);
break;
case 0:
return;
}
}
}
void Reader::change()
{
char newname[20]; char newnumber[10];
int choice;
file.seekp(0,ios::cur);
int Posend=file.tellp(); bool iflag;int num=0;
while(!(iflag=findreader()))
{
num++;
if(num>=3)
return;
}
cout<<" 请输入你的选择!"<<endl;
cout<<"1--改变名字!\n"
<<"2--修改学号r\n"
<<"0--返回\n";
cin>>choice;
switch(choice)
{
case 1:
cout<<"输入你的新名字!\n";
cin>>newname;
strcpy(person.name,newname);
break;
case 2:
cout<<"输入你的信学号?\n";
cin>>newnumber;
strcpy(person.studynumber,newnumber);
break;
default:
break;;
}
file.seekp(-long(sizeof(Person)),ios::cur);
file.write(reinterpret_cast<char*>(&person),sizeof(Person));
cout<<"信息修改成功!"<<endl;
}
void Liberian::selfprint()
{
cout<<" 图书管理员 :\n"
<<worknumber<<"---"<<workname<<" 为您服务!\n";
}
Liberian::Liberian(char *a,char*b)
{
strcpy(workname,a);
strcpy(worknumber,b);
}
void Liberian::changeReader(Reader& a)
{
int choice; char newname[20];char newnumber[10];
cout<<"修改读者的数据信息!\n";
cout<<"1-- 增加一个读者\n"
<<"2-- 删除一个读者\n"
<<"0-- 结束程序\n";
cin>>choice;
switch(choice)
{
case 1:
a.Addreader();
break;
case 2:
a.deletereader();
break;
case 0:
return;
}
}
void Reader::Addreader()
{
Person temp;bookData book={0,"0",0};
file.seekp(0,ios::end);
int Posend=file.tellp();
cout<<"输入你想要增加的读者的名字"<<endl;
cin>>temp.name;
cout<<"新读者的学号:"<<endl;
cin>>temp.studynumber;
temp.Rbook[1]=book; temp.Rbook[0]=book; temp.count=0;
file.write(reinterpret_cast<char*>(&temp),sizeof(Person));
cout<<" 成功添加!"<<temp.name<<endl;
}
void Reader::deletereader()
{
file.seekp(0,ios::end);
int Posend=file.tellp();
Person person;
char name[20];
cout<<"输入要删除的读者的名字!"<<endl;
cin>>name;
file.seekg(0);
do{
file.read(reinterpret_cast<char*>(&person),sizeof(Person));
}while(strcmp(name,person.name)&&file.tellp()!=Posend);
if(!strcmp(name,person.name))
{
bookData blankbook={0,"0",0};
Person guest={"0","0",0,{{0,"0",0},{0,"0",0}}};
file.seekp(-long(sizeof(Person)),ios::cur);
file.write(reinterpret_cast<char*>(&guest),sizeof(Person));
cout<<"成功删除!"<<person.name<<endl;
}
else
cout<<"查无此人!"<<endl;
}
bool Reader::findreader()
{
file.seekp(0,ios::end);
int Posend=file.tellp();
char name[20];
Person guest={"0","0",0,{{0,"0",0},{0,"0",0}}};
cout<<"输入查找人的名字!";
cin>>name;
file.seekg(0);
do{
file.read(reinterpret_cast<char*>(&person),sizeof(Person));
}while(strcmp(name,person.name)&&file.tellp()!=Posend);
if(!strcmp(name,person.name))
{
cout<<"查到此人:"<<person.name<<endl;
return true;
}
else
{
cout<<"找不到此人,请重新确认!"<<endl;
return false;
}
}
//借阅历史
void History::readhistory(fstream& a)
{
readerbook=a;
char name[10]="0";
readerbook.seekp(0,ios::end);
int Posend=readerbook.tellp();
Person guest; int iflag=1;
cout<<" 书籍借阅信息如下:"<<endl;
cout<<"姓名 学号 借书量 书籍名"<<endl;
readerbook.seekg(0,ios::beg);
do{
readerbook.read(reinterpret_cast<char*>(&guest),sizeof(Person));
if(strcmp(guest.Rbook[1].bookname,name) || strcmp(guest.Rbook[1].bookname,name))
{
cout<<guest.name<<" "<<guest.studynumber<<" "<<guest.count<<" ";
for(int i=0;i<2;i++)
if(strcmp(guest.Rbook[i].bookname,name))
cout<<guest.Rbook[i].bookname;
iflag=0;
}
}while(readerbook.tellp()!=Posend);
if(iflag)
cout<<"没有读者借阅图书!"<<endl;
}
以下代码已经编译运行通过,自己改一下就可以了:
#include<iostream.h>
#include<fstream.H>
#include<stdlib.h>
#include<string.h>
struct bookData
{
int booknumber;
char bookname[30];
int store;
};
struct Person
{
char name[20];
char studynumber[10];
int count;
bookData Rbook[2];
//bookData Rbook;
};
class History
{
public:
History(fstream& a){readerbook=a;}
void writehistory();
void readhistory(fstream&);
private:
fstream readerbook;
};
class Liberian;
void Find(fstream&);
void CreateTxt(fstream&);
class Reader
{
friend Liberian;
public:
Reader(fstream&);
void print();
void handlebook(fstream&);
void change();
bool findreader();
void Addreader();
void deletereader();
private:
fstream file;
Person person;
};
class Liberian
{
public:
Liberian(char *,char *);
void changeReader(Reader&);
void selfprint();
void Append(fstream&);
private:
char workname[20];
char worknumber[20];
};
int main()
{
fstream bookRecord(" book.dat",ios::in| ios::out);
if(!bookRecord)
{
cerr<<" Can't open,try again!"<<endl;
exit(1);
}
fstream readerRecord("reader.dat",ios::in|ios::out);
if(!readerRecord)
{
cerr<<" Can't open,try again!"<<endl;
exit(1);
}
int choice;
int key; Reader reader(readerRecord);//读者管理系统
Liberian liberian("李天","123456");//图书管理员
History rec(readerRecord);//声明显示历史记录类
while(1)
{
cout<<" 请输入您的选择:\n\n"
<<"1--图书管理员\n"
<<"2--读者系统\n"
<<"0--结束程序\n";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<" Hello,图书管理员:\n";
liberian.selfprint();cout<<endl;
cout<<"1-- 增加新图书\n"
<<"2-- 改变读者的属性\n"
<<"3-- 显示历史记录\n"
<<"0-- 返回\n";
cin>>key;
switch(key)
{
case 1:
liberian.Append(bookRecord);
break;
case 2:
liberian.changeReader(reader);
break;
case 3:
rec.readhistory(readerRecord);
case 0:
break ;
}
}
break;
case 2:
{
cout<<"学生读者\n"; int iflag=1;
//reader.findreader();
cout<<"1-- 借书与还书信息\n"
<<"2-- 修改个人信息\n"
<<"0-- 返回\n";
cin>>key;
switch(key)
{
case 1:
reader.handlebook(bookRecord);
break;
case 2:
reader.change();
break;
case 0:
break;
}
}
break;
default:
return 0;
}
}
return 0;
}
//增加书籍
void Liberian::Append(fstream& storef)
{
bookData book;
int choice;
int key;
int num;
storef.seekp(0,ios::end);
int posEnd=storef.tellp();
cout<<"*************书籍入库***************"<<endl;
while(1)
{
cout<<" Please enter your choice:"<<endl;
cout<<"1--添加新书\n"<<"2--已有书籍\n"<<"0--返回\n";
cin>>choice;
switch(choice)
{
case 1:
cout<<" 书号, 书名, 数量?"<<endl;
cin>>book.booknumber>>book.bookname>>book.store;
storef.write(reinterpret_cast<char*>(&book),sizeof(bookData));
break;
case 2:
storef.seekg(0,ios::beg);
cout<<"Booknumber?\n";
cin>>key;
do{
storef.read(reinterpret_cast<char*>(&book),sizeof(bookData));
}while(book.booknumber!=key&&storef.tellg()!=posEnd);
if(book.booknumber==key)
{
cout<<book.booknumber<<"----"<<book.bookname<<"----"<<book.store<<endl;
cout<<" 输入已有书增加的数量:"<<endl;
cin>>num;
if(num>0)
book.store+=num;
else
{
cout<<" Invalid input"<<endl;
}
storef.seekp(-long(sizeof(bookData)),ios::cur);////////////////////
storef.write(reinterpret_cast<char*>(&book),sizeof(bookData));
cout<<"现在书籍: "<<book.bookname<<" 余量为: "<<book.store<<endl;
}
else
cout<<"找不到此书,请重新查阅书号是否正确!"<<endl;
break;
case 0:
return ;
}
}
}
//书籍查询
void Find(fstream & f)
{
bookData book;
int key;
int choice;
f.seekg(0,ios::end);
int posEnd=f.tellp();
cout<<"*************书籍查询***************"<<endl;
while(1)
{
cout<<" 请输入您的选择\n"
<<" 1-- 检索一本书\n"
<<"2-- 显示全部书籍\n"
<<"0-- 返回\n";
cin>>choice;
switch(choice)
{
case 1:
f.seekg(0,ios::beg);
cout<<"输入你想检索书的书号"<<endl;
cin>>key;
do{
f.read(reinterpret_cast<char*>(&book),sizeof(bookData));
}while(book.booknumber!=key&&f.tellg()!=posEnd);
if(book.booknumber==key)
cout<<book.booknumber<<"---"<<book.bookname<<"---"<<book.store<<endl;
else
cout<<"找不到此书,请重新确认!"<<endl;
break;
case 2:
f.seekg(0,ios::beg);
do{
f.read(reinterpret_cast<char*>(&book),sizeof(bookData));
cout<<book.booknumber<<"---"<<book.bookname<<"--"<<book.store<<endl;
}while(f.tellg()!=posEnd);
break;
case 0:
return ;
}
}
}
Reader::Reader(fstream& c)
{
file=c;
Person person={"0","0",0,{{0,"0",0}, {0,"0",0}}};
}
void Reader::print()
{
cout<<"Name"<<"---"<<person.name<<'\n\n'<<"studynumber---"<<person.studynumber<<'\n';
}
//操作书籍??????????????????????
void Reader::handlebook(fstream &filee)
{
int key; int choice;
bookData book; bool iflag;int num=0;
filee.seekp(0,ios::end);
int posEnd=filee.tellp();
while(!(iflag=findreader()))
{
num++;
if(num>=3)
return;
}
//cout<<person.name;
while(1)
{
cout<<"1-- 借书\n"
<<"2-- 还书\n"
<<"3-- 查找一本书\n"
<<"0-- 返回"<<'\n';
cin>>choice;
switch(choice)
{
case 1:
{
filee.seekp(0,ios::end);
int posEnd=filee.tellp();
if(person.count<=1)
{
cout<<" 输入你要借阅图书的书号:"<<endl;
cin>>key;
filee.seekg(0);
do{
filee.read(reinterpret_cast<char*>(&book),sizeof(bookData));
}while(book.booknumber!=key&&filee.tellp()!=posEnd);
//cout<<book.booknumber<<endl;
if(book.booknumber==key)
{
//cout<<" 找到你想借阅的图书,成功借阅!"<<endl;
if(book.store>0)
{
person.Rbook[person.count]=book;
person.count+=1;
book.store-=1;
filee.seekp(-long(sizeof(bookData)),ios::cur);
filee.write(reinterpret_cast<char*>(&book),sizeof(bookData));
cout<<" 找到你想借阅的图书,成功借阅!"<<endl;
cout<<"书籍 :"<<book.bookname<<"剩余的本数为:"<<book.store<<endl;
}
else
cout<<" 现在此图书已经被借完了,请耐心等待几天!"<<endl;
}
else
cout<<" 找不到你想要的图书"<<endl;
}
else
cout<<"你最多只能借2本\n\n";
}
break;
case 2:
{
bookData blankbook={0,"0",0};
//if(person.count>0)
//{
int iflag=0;
filee.seekg(0);
cout<<" 输入你想要还的书的书号:"<<endl;
cin>>key;
for(int i=0;i<2;i++)
{
if(key==person.Rbook[i].booknumber)
{
person.Rbook[i]=blankbook;
cout<<"成功归还此书!";
iflag=1;
person.count--;
do{
filee.read(reinterpret_cast<char*>(&book),sizeof(bookData));
}while(book.booknumber!=key&&filee.tellp()!=posEnd);
if(book.booknumber==key)
{
book.store+=1;
filee.seekp(-long(sizeof(bookData)),ios::cur);
filee.write(reinterpret_cast<char*>(&book),sizeof(bookData));
cout<<"书籍 :"<<book.bookname<<"余本量为: "<<book.store<<endl;
}
}
}
if(!iflag)
{
cout<<" 你没有借阅那本书,请确认!"<<endl;
}
// }
// else
// cout<<"你没有借书,请重新确认!\n\n";
}
break;
case 3:
Find(filee);
break;
case 0:
return;
}
}
}
void Reader::change()
{
char newname[20]; char newnumber[10];
int choice;
file.seekp(0,ios::cur);
int Posend=file.tellp(); bool iflag;int num=0;
while(!(iflag=findreader()))
{
num++;
if(num>=3)
return;
}
cout<<" 请输入你的选择!"<<endl;
cout<<"1--改变名字!\n"
<<"2--修改学号r\n"
<<"0--返回\n";
cin>>choice;
switch(choice)
{
case 1:
cout<<"输入你的新名字!\n";
cin>>newname;
strcpy(person.name,newname);
break;
case 2:
cout<<"输入你的信学号?\n";
cin>>newnumber;
strcpy(person.studynumber,newnumber);
break;
default:
break;;
}
file.seekp(-long(sizeof(Person)),ios::cur);
file.write(reinterpret_cast<char*>(&person),sizeof(Person));
cout<<"信息修改成功!"<<endl;
}
void Liberian::selfprint()
{
cout<<" 图书管理员 :\n"
<<worknumber<<"---"<<workname<<" 为您服务!\n";
}
Liberian::Liberian(char *a,char*b)
{
strcpy(workname,a);
strcpy(worknumber,b);
}
void Liberian::changeReader(Reader& a)
{
int choice; char newname[20];char newnumber[10];
cout<<"修改读者的数据信息!\n";
cout<<"1-- 增加一个读者\n"
<<"2-- 删除一个读者\n"
<<"0-- 结束程序\n";
cin>>choice;
switch(choice)
{
case 1:
a.Addreader();
break;
case 2:
a.deletereader();
break;
case 0:
return;
}
}
void Reader::Addreader()
{
Person temp;bookData book={0,"0",0};
file.seekp(0,ios::end);
int Posend=file.tellp();
cout<<"输入你想要增加的读者的名字"<<endl;
cin>>temp.name;
cout<<"新读者的学号:"<<endl;
cin>>temp.studynumber;
temp.Rbook[1]=book; temp.Rbook[0]=book; temp.count=0;
file.write(reinterpret_cast<char*>(&temp),sizeof(Person));
cout<<" 成功添加!"<<temp.name<<endl;
}
void Reader::deletereader()
{
file.seekp(0,ios::end);
int Posend=file.tellp();
Person person;
char name[20];
cout<<"输入要删除的读者的名字!"<<endl;
cin>>name;
file.seekg(0);
do{
file.read(reinterpret_cast<char*>(&person),sizeof(Person));
}while(strcmp(name,person.name)&&file.tellp()!=Posend);
if(!strcmp(name,person.name))
{
bookData blankbook={0,"0",0};
Person guest={"0","0",0,{{0,"0",0},{0,"0",0}}};
file.seekp(-long(sizeof(Person)),ios::cur);
file.write(reinterpret_cast<char*>(&guest),sizeof(Person));
cout<<"成功删除!"<<person.name<<endl;
}
else
cout<<"查无此人!"<<endl;
}
bool Reader::findreader()
{
file.seekp(0,ios::end);
int Posend=file.tellp();
char name[20];
Person guest={"0","0",0,{{0,"0",0},{0,"0",0}}};
cout<<"输入查找人的名字!";
cin>>name;
file.seekg(0);
do{
file.read(reinterpret_cast<char*>(&person),sizeof(Person));
}while(strcmp(name,person.name)&&file.tellp()!=Posend);
if(!strcmp(name,person.name))
{
cout<<"查到此人:"<<person.name<<endl;
return true;
}
else
{
cout<<"找不到此人,请重新确认!"<<endl;
return false;
}
}
//借阅历史
void History::readhistory(fstream& a)
{
readerbook=a;
char name[10]="0";
readerbook.seekp(0,ios::end);
int Posend=readerbook.tellp();
Person guest; int iflag=1;
cout<<" 书籍借阅信息如下:"<<endl;
cout<<"姓名 学号 借书量 书籍名"<<endl;
readerbook.seekg(0,ios::beg);
do{
readerbook.read(reinterpret_cast<char*>(&guest),sizeof(Person));
if(strcmp(guest.Rbook[1].bookname,name) || strcmp(guest.Rbook[1].bookname,name))
{
cout<<guest.name<<" "<<guest.studynumber<<" "<<guest.count<<" ";
for(int i=0;i<2;i++)
if(strcmp(guest.Rbook[i].bookname,name))
cout<<guest.Rbook[i].bookname;
iflag=0;
}
}while(readerbook.tellp()!=Posend);
if(iflag)
cout<<"没有读者借阅图书!"<<endl;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询