当前位置:文档之家› 图书管理系统代码

图书管理系统代码

#include <string>#include <fstream>#include <iostream>using namespace std;const int BMAX=50;class bdatabase{public:book btarray[BMAX];int top;public:bdatabase(){book b;top=-1;fstream file("book.txt",ios::in);//打开文件while(1){file.read((char*)&b,sizeof(b));if(!file)break;top++;btarray[top]=b;}file.close();//关闭文件}~bdatabase()//析构函数,将btarray[]写到book.txt文件中{fstream file("book.txt",ios::out);for(int i=0;i<=top;i++){if(btarray[i].b_del==1)file.write((char*)&btarray[i],sizeof(btarray[i]));}file.close();}void addbooks()//往图书数据库中加图书{book bk;int no;charbname[20];cout<<"请输入书号:";cin>>no;cout<<endl<<"请输入书名:";cin>>bname;cout<<endl;bk.b_flag=1;bk.addbook(no,bname);top++;btarray[top]=bk;return;}int findbooks(int suffix)//查找图书,这个函数用于借书这个操作,suffix是数组下标{int no;cout<<"请输入书号:";cin>>no;for(int i=0,i<=top;i++){if(btarray[i].no==no&&btarray[i].b_del==1){return i;}}return -1;}int findbooks()//查找图书,这个函数用于图书维护{int no;char value[6];cout<<"请输入书号:";cin>>no;for(int i=0;i<=top;i++){if(btarray[i].no==no&&btarray[i].b_del==1){if(btarray[i].b_flag==1)strcpy(value,"在架");if(btarray[i].b_flag==2)strcpy(value,"借出");cout<<"书号:"<<btarray[i].no<<"书的名称:"<<btarray[i].name<<"图书状态:"<<value<<endl;return i;}}return -1;}void editbooks()//编辑图书{int cur;cur=findbooks();if(cur==-1){cout<<"没有这本书"<<endl;return;}cout<<"书号:"<<btarray[cur].no<<"书的名称:"<<btarray[cur].name<<endl;cout<<"请修改数据:"<<endl;cout<<endl<<"书的名称:";cin>>btarray[cur].name;return;}void delbooks()//删除图书{int cur;cur=findbooks();if(cur==-1){return;}if(btarray[cur].b_flag==2)//删除时判断图书是否借出{cout<<"该图书已经借出,请在归还后删除。

"<<endl;return;}btarray[cur].b_del=2;return;}void listbooks()//显示全部图书{char value[6];for(int i=0;i<=top;i++){if(btarray[i].b_del==1){if(btarray[i].b_flag==1)strcpy(value,"在架");if(btarray[i].b_flag==2)strcpy(value,"借出");cout<<"书号:"<<btarray[i].no<<"书的名称:"<<btarry[i].name<<"图书状态:"<<value<<endl;}}return;}};class book{public:int no;//图书编号char name[20];//图书名称int b_flag;int b_del;public:book(){}void setno(int n){no=n;int getno(){return no;//返回图书编号值}void setname(char na[]){strcpy(name,na);//字符串复制}char* getname(){return name;//返回图书名称}void borbook(){b_flag=2;//借书标志}void delbook(){b_del=2;//删除标志}void addbook(int no, char na[])//增加图书功{setno(no);setname(na);b_flag=1;b_del=1;}}};//读者类class reader{public:int no;char name[20];int r_del;//1存在,2删除int r_bor;//1借过书,2没有借书book brbook;public:void addreader(int id, char na[])//增加读者{no=id;strcpy(name,na);r_del=1;}void setbrbook(book bt)//设置借书标记{brbook=bt;}book getbrbook(){return brbook;//返回借书信息}};//读者数据库类class rdatabase{public:reader rdarray[BMAX];int top;public:rdatabase(){reader rd;top=-1;fstream file("reader.txt",ios::in);//打开文件while(1){file.read((char*)&rd,sizeof(rd));if(!file)break;top++;rdarray[top]=rd;}file.close();//关闭文件~rdatabase()//析构函数{fstream file("reader.txt",ios::out);for(int i=0;i<=top;i++){if(rdarray[i].r_del==1)file.write((char*)&rdarray[i],sizeof(rdarray[i]));}file.close();}void addreaders(){reader rd;int no;char rname[20];cout<<"请输入学号:";cin>>no;cout<<endl<<"请输入姓名:";cin>>rname;cout<<endl;rd,addreader(no,rname);//添加读者rd.r_bor=2;top++;rdarray[top]=rd;return;}int findreaders(int suffix)//查找读者,这个函数用于借书还书操作。

参数suffix 是数组下标{int no;cout<<"请输入学号:";cin>>no;for(int i=0;i<=top;i++){if(rdarray[i].no==no&&rdarray[i].r_del==1){return i;}}return -1;}int findreaders()//查找读者,这个函数用于读者维护操作int no;char value[3];cout<<"请输入学号:";cin>>no;for(int i=0;i<=top;i++){if(rdarray[i].no==no&&rdarray[i].r_del==1){if(rdarray[i].r_bor==1)strcpy(value,"借");if(rdarray[i].r_bor==2)strcpy(value,"无");cout<<"学号:"<<rdarray[i].no<<"姓名"<<rdarray[i].name<<"是否借书:"<<value<<endl;return i;}}return -1;}void editreaders()//编辑读者信息{int cur;cur=findreaders();if(cur==-1){cout<<"没有该学生"<<endl;return;}cout<<"请修改数据:"<<endl;cout<<endl<<"姓名:";cin>>rdarray[cur].name;return;}void delreaders()//删除读者,读者如果借书要还书后才能删除{int cur;cur=findreaders();if(cur==-1){return;}if(rdarray[cur].r_del==1){cout<<"该用户借了图书,请在还书后再删除"<<endl;return;rdarray[cur].r_del=2;return;}void listreaders()//显示所有读者{char value[3];for(int i=0;i<=top;i++){if(rdarray[i].r_del==1){if(rdarray[i].r_bor==1)strcpy(value,"借");if(rdarray[i].r_bor==2)strcpy(value,"无");cout<<"学号:"<<rdarray[i].no<<"姓名:"<<rdarray[i].name<<"是否借书:"<<value<<endl;}}return;}};void bookjiemian()//图书维护子菜单{cout<<"|------------------|"<<endl;cout<<"| 图书维护|"<<endl;cout<<"| (1)增加|"<<endl;cout<<"| (2)更改|"<<endl;cout<<"| (3)删除|"<<endl;cout<<"| (4)查找|"<<endl;cout<<"| (5)显示|"<<endl;cout<<"| (6)返回上一层|"<<endl;cout<<"|------------------|"<<endl;}void readerjiemian()//读者维护子菜单{cout<<"|------------------|"<<endl;cout<<"| 读者维护|"<<endl;cout<<"| (1)增加|"<<endl;cout<<"| (2)更改|"<<endl;cout<<"| (3)删除|"<<endl;cout<<"| (4)查找|"<<endl;cout<<"| (5)显示|"<<endl;cout<<"| (6)返回上一层|"<<endl;cout<<"|------------------|"<<endl;}void//主界面{cout<<"*********************"<<endl;cout<<"* 图书管理系统*"<<endl;cout<<"* *"<<endl;cout<<"* (1)借书*"<<endl;cout<<"* *"<<endl;cout<<"* (2)还书*"<<endl;cout<<"* *"<<endl;cout<<"* (3)图书管理*"<<endl;cout<<"* *"<<endl;cout<<"* (4)用户管理*"<<endl;cout<<"* *"<<endl;cout<<"* (0)退出*"<<endl;cout<<"*********************"<<endl;}void borrowtoreturn(char br)//借书还书函数,参数br用来判断是“借书”还是“还书”。

相关主题