这是一个用C++编写图书馆管理系统,在VC6.0编译通过.
#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:
//删除标记 1:已删 0:未删
int tag;
//读者编号
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);
}
//设置删除标记 1:已删 0:未删
void delbook()
{
tag=1;
}
//增加读者
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:
//构造函数,将reader.txt读到read[]中
RDatabase()
{
Reader s;
top=-1;
//蚩桓鍪淙胛募?nbsp;
fstream file("reader.txt",ios::in);
while (1)
{
file.read((char *)&s,sizeof(s));
if (!file)break;
top++;
read[top]=s;
}
//关闭 reader.txt
file.close();
}
//删除所有读者信息
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();
//析构函数,将read[]写到reader.txt文件中
~RDatabase()
{
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:
//删除标记 1:已删 0:未删
int tag;
//图书编号
int no;
//书名
char name[20];
//是否再架 1:再架 2:已借
int onshelf;
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:
//构造函数,将book.txt读到book[]中
BDatabase()
{
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();
}
//析构函数,将book[]写到book.txt文件中
~BDatabase()
{
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<<"输入错误,请从新输入:";
}
}
}