图书馆管理系统

下面介绍的是简易图书馆管理系统。实现的功能有:

  1. 管理端:增加图书,增加用户;
  2. 客户端:借书,还书

程序结构层次概括为四个层次

  1. 基本成员层:该层次包括两个类:Date日期类和Record记录类。日期类定义的对象可用于图书出版日期,借书还书的日期;记录类用于客户端(学生端)借阅图书和还书的时候增加相应的记录。
  2. 基础信息层:该层次包括两个类:student类和book类。用于管理端的操作。
  3. 操作层:该层次包括两个类:管理端Adminop和用户端User,完成增加图书,增加用户(学生)和借书还书的操作
  4. 主函数:通过类定义对象完成操作和对文件的修改


  1. 基本成员层:
    1. class Date  
    2. {  
    3.     int year,month,day;  
    4.     public:  
    5.     Date(int y,int m,int d){year=y;month=m;day=d;}  
    6.     Date(){}  
    7.     void setYear(int y){year=y;}  
    8.     void setMonth(int m){month=m;}  
    9.     void setDay(int d){day=d;}  
    10.     int getYear(){return year;}  
    11.     int getMonth(){return month;}  
    12.     int getDay(){return day;}  
    13.     bool operator<(const Date&d)const  
    14.     {  
    15.         return year!=d.year?year<d.year:month!=d.month?month<d.month:day<d.day;  
    16.     }  
    17.     friend istream&operator>>(istream&is,Date&d);  
    18.     friend ostream&operator<<(ostream&os,Date&d);  
    19. };  
    20. istream&operator>>(istream&is,Date&d)  
    21. {  
    22.     is>>d.year>>d.month>>d.day;  
    23.     return is;  
    24. }  
    25. ostream&operator<<(ostream&os,Date&d)  
    26. {  
    27.     os<<d.year<<" ";  
    28.     os<<d.month<<" ";  
    29.     os<<d.day;  
    30.     return os;  
    31. }  
    32. class Record  
    33. {  
    34.     int id;         //学号  
    35.     Date jieshudate;         //借书时间  
    36.     int bookno;          //书号  
    37.     Date huanshudate;           //还书时间  
    38.     int xujie;           //续借  
    39.     int state;  
    40.     public:  
    41.     Record(int a,Date d,int b)  
    42.     {  
    43.         id=a;  
    44.         jieshudate=d;  
    45.         bookno=b;  
    46.         if(d.getMonth()+2>12)  
    47.         {  
    48.             Date d1(d.getYear()+1,d.getMonth()-10,d.getDay());  
    49.             huanshudate=d1;  
    50.         }  
    51.         else  
    52.         {  
    53.             Date d1(d.getYear(),d.getMonth()+2,d.getDay());  
    54.             huanshudate=d1;  
    55.         }  
    56.         xujie=0;  
    57.         state=0;  
    58.     }  
    59.     Record(){}  
    60.     int getId(){return id;}  
    61.     Date getJieshudate(){return jieshudate;}  
    62.     int getBookno(){return bookno;}  
    63.     int getXujie(){return xujie;}  
    64.     Date getHuanshudate(){return huanshudate;}  
    65.     void setHuanshudate();  
    66.     void setXujie(){xujie=1;}  
    67.     void setState(int n){state=n;}  
    68.     int getState(){return state;}  
    69.     friend istream&operator>>(istream&is,Record&r);  
    70.     friend ostream&operator<<(ostream&os,Record&r);  
    71. };  
    72. void Record::setHuanshudate()  
    73. {  
    74.     Date d;  
    75.     if(huanshudate.getMonth()+1>12)  
    76.     d=Date(huanshudate.getYear()+1,huanshudate.getMonth()+1,huanshudate.getDay());  
    77.     else  
    78.     d=Date(huanshudate.getYear()+1,huanshudate.getMonth(),huanshudate.getDay());  
    79.     huanshudate;  
    80. }  
    81. istream&operator>>(istream&is,Record&r)  
    82. {  
    83.     is>>r.id;  
    84.     is>>r.jieshudate;  
    85.     is>>r.bookno;  
    86.     is>>r.huanshudate;  
    87.     is>>r.xujie;  
    88.     is>>r.state;  
    89.     return is;  
    90. }  
    91. ostream&operator<<(ostream&os,Record&r)  
    92. {  
    93.     os<<r.id<<" ";  
    94.     os<<r.jieshudate<<" ";  
    95.     os<<r.bookno<<" ";  
    96.     os<<r.huanshudate<<" ";  
    97.     os<<r.xujie<<" ";  
    98.     os<<r.state<<endl;  
    99.     return os;  
    100. }  
  2. 基础信息层:
    1. class Book  
    2. {  
    3.     int bookno;          //书号  
    4.     string bookname;               //书名  
    5.     string pub;               //出版社  
    6.     Date pubdate;           //出版时间  
    7.     int num;                  //书的总数  
    8.     int leadnum;               //借出数目  
    9.     int jilunum;              //借阅记录数  
    10.     vector<Record>v1;  
    11.     vector<Record>::iterator it1;  
    12.     multimap<int,int>m1;  
    13.     multimap<int,int>::iterator mit1;  
    14. public:  
    15.     Book(){jilunum=0;}  
    16.     vector<Record>getRecord(){return v1;}  
    17.     int getBookno(){return bookno;}  
    18.     string getBookname(){return bookname;}  
    19.     string getPub(){return pub;}  
    20.     Date getDate(){return pubdate;}  
    21.     void setNum(int n){num=n;}  
    22.     int getNum(){return num;}  
    23.     void setLeadnum(int x){leadnum=x;}  
    24.     int getLeadnum(){return leadnum;}  
    25.     void setJilunum(int n){jilunum=n;}  
    26.     int getJilunum(){return jilunum;}  
    27.     void addRecord1(Record r1)  
    28.     {  
    29.         r1.setState(1);  
    30.         v1.push_back(r1);  
    31.         int i=v1.size();  
    32.         m1.insert(make_pair(r1.getId(),i-1));  
    33.     }  
    34.     void addR1(Record r1)  
    35.     {  
    36.         v1.push_back(r1);  
    37.         int i=v1.size();  
    38.         m1.insert(make_pair(r1.getId(),i-1));  
    39.     }  
    40.     void deleteRecord1(int no)  
    41.     {  
    42.         int i;  
    43.         i=search1(no);  
    44.         if(i!=-1)  
    45.         {  
    46.             v1[i].setState(0);  
    47.         }  
    48.     }  
    49.     int search1(int no)  
    50.     {  
    51.         mit1=m1.find(no);  
    52.         if(mit1!=m1.end())  
    53.         return mit1->second;  
    54.         else  
    55.         return -1;  
    56.     }  
    57.     void clear1(){v1.clear();}  
    58.     friend istream&operator>>(istream&is,Book&b);  
    59.     friend ostream&operator<<(ostream&os,Book&b);  
    60. };  
    61. istream&operator>>(istream&is,Book&b)  
    62. {  
    63.     is>>b.bookno;  
    64.     if(b.bookno==-1)  
    65.     return is;  
    66.     is>>b.bookname;  
    67.     is>>b.pub;  
    68.     is>>b.pubdate;  
    69.     is>>b.num;  
    70.     is>>b.leadnum;  
    71.     is>>b.jilunum;  
    72.     return is;  
    73.     }  
    74.     ostream&operator<<(ostream&os,Book&b)  
    75.     {  
    76.         os<<b.bookno<<" "<<b.bookname<<" "<<b.pub<<" "<<b.pubdate<<" "<<b.num<<" "<<b.leadnum<<endl;  
    77.         os<<b.jilunum<<endl;  
    78.         if(b.getJilunum()!=0)  
    79.             for(int i=0;i<b.getJilunum();i++)  
    80.                 os<<b.v1[i];  
    81.         return os;  
    82.     }  
    83. class student  
    84. {  
    85.     int xuehao;              //学号  
    86.     string name;                //姓名  
    87.     int maxnum;            //最大借阅数  
    88.     int nnum;             //目前借阅数  
    89.     int weiji;             //是否违纪  
    90.     int rnum;               //当前借阅数  
    91.     vector<Record>v2;  
    92.     vector<Record>::iterator it2;  
    93.     multimap<int,int>m2;  
    94.     multimap<int,int>::iterator mit2;  
    95. public:  
    96.     student(){weiji=0;rnum=0;}  
    97.     int getXuehao(){return xuehao;}  
    98.     void setName(string nam){name=nam;}  
    99.     string getName(){return name;}  
    100.     void setMaxnum(int x){maxnum=x;}  
    101.     int getMaxnum(){return maxnum;}  
    102.     void setNnum(int x){nnum=x;}  
    103.     int getNnum(){return nnum;}  
    104.     void setWeiji(int x){weiji=x;}  
    105.     int getWeiji(){return weiji;}  
    106.     void setRnum(int x){rnum=x;}  
    107.     int getRnum(){return rnum;}  
    108.     vector<Record>getRecord(){return v2;}  
    109.     multimap<int,int>getMap(){return m2;}  
    110.     void clear2();  
    111.     void addRecord2(Record r2);  
    112.     int search2(int h);  
    113.     void deleteRecord2(int h);  
    114.     friend istream&operator>>(istream&is,student &s);  
    115.     friend ostream&operator<<(ostream&os,student&s);  
    116.     void huanshudat(Date d);  
    117.     void addR2(Record r);  
    118.     void operator=(student&s)  
    119.     {  
    120.         xuehao=s.getXuehao();  
    121.         name=s.getName();  
    122.         maxnum=s.getMaxnum();  
    123.         nnum=s.getNnum();  
    124.         weiji=s.getWeiji();  
    125.         rnum=s.getRnum();  
    126.         v2=s.getRecord();  
    127.         m2=s.getMap();  
    128.     }  
    129.     bool operator==(student s)  
    130.     {  
    131.         return this->xuehao==s.getXuehao()?1:0;  
    132.     }  
    133. };  
    134.     istream&operator>>(istream&is,student&s)  
    135.     {  
    136.         is>>s.xuehao;  
    137.         if(s.xuehao==-1)  
    138.         return is;  
    139.         is>>s.name;  
    140.         is>>s.maxnum;  
    141.         is>>s.nnum;  
    142.         is>>s.weiji;  
    143.         is>>s.rnum;  
    144.         return is;  
    145.     }  
    146.     ostream&operator<<(ostream&os,student&s)  
    147.     {  
    148.         os<<s.xuehao<<" "<<s.name<<" "<<s.maxnum<<" "<<s.nnum<<" "<<s.weiji<<" "<<endl;  
    149.         os<<s.rnum<<endl;  
    150.         if(s.getRnum()!=0)  
    151.         for(int i=0;i<s.getRnum();i++)  
    152.         os<<s.v2[i];  
    153.         return os;  
    154.     }  
    155.     void student::addR2(Record r)  
    156.     {  
    157.         v2.push_back(r);  
    158.         int i=v2.size();  
    159.         m2.insert(make_pair(r.getBookno(),i-1));  
    160.     }  
    161.     void student::addRecord2(Record r)  
    162.     {  
    163.         r.setState(1);  
    164.         v2.push_back(r);  
    165.         int i=v2.size();  
    166.         m2.insert(make_pair(r.getBookno(),i-1));  
    167.     }  
    168.     int student::search2(int h)  
    169.     {  
    170.         mit2=m2.find(h);  
    171.         if(mit2!=m2.end())  
    172.         return mit2->second;  
    173.         else  
    174.         return -1;  
    175.     }  
    176.     void student::deleteRecord2(int h)  
    177.     {  
    178.         int i;  
    179.         i=search2(h);  
    180.         if(i!=-1)  
    181.         {  
    182.             v2[i].setState(0);  
    183.         }  
    184.     }  
    185.     void student::huanshudat(Date d)  
    186.     {  
    187.         setWeiji(0);  
    188.         for(int i=0;i<v2.size();i++)  
    189.         if(v2[i].getHuanshudate()<d&&v2[i].getState()==1){setWeiji(1);break;}  
    190.     }  
    191.     void student::clear2()  
    192.     {  
    193.         v2.clear();  
    194.     }  
  3. 操作层:
    1. class Adminop  
    2. {  
    3.     vector<Book>v3;  
    4.     vector<Book>::iterator it3;  
    5.     multimap<int,int>m3;  
    6.     multimap<int,int>::iterator mit3;  
    7.     vector<student>v4;  
    8.     vector<student>::iterator it4;  
    9.     multimap<int,int>m4;  
    10.     multimap<int,int>::iterator mit4;  
    11. public:  
    12.     Adminop(){}  
    13.     ~Adminop()  
    14.     {  
    15.         save1();  
    16.         save2();  
    17.     }  
    18.     void load1();  
    19.     void load2();  
    20.     void save1();  
    21.     void save2();  
    22.     void addBook()  
    23.     {  
    24.         cout<<"请输入图书信息"<<endl;  
    25.         cout<<"格式:书号 书名 出版社 出版日期 馆藏数目 借出数目 借阅记录数目"<<endl;  
    26.         Book b;  
    27.         int i;  
    28.         while(cin>>b)  
    29.         {  
    30.             if(b.getBookno()==-1)  
    31.             break;  
    32.             v3.push_back(b);  
    33.             i=v3.size();  
    34.             m3.insert(make_pair(b.getBookno(),i-1));  
    35.             b.clear1();  
    36.         }  
    37.     }  
    38.     int search3(int x)  
    39.     {  
    40.         mit3=m3.find(x);  
    41.         if(mit3!=m3.end())  
    42.         {  
    43.             return mit3->second;  
    44.         }  
    45.         else  
    46.         return -1;  
    47.     }  
    48.     void addstudent()  
    49.     {  
    50.         cout<<"请输入学生信息"<<endl;  
    51.         cout<<"格式:学号 姓名 最大借阅数目 目前借阅数目 是否违纪 借阅记录数目"<<endl;  
    52.         student s;  
    53.         int i;  
    54.         while(cin>>s)  
    55.         {  
    56.             if(s.getXuehao()==-1)  
    57.             break;  
    58.             v4.push_back(s);  
    59.             i=v4.size();  
    60.             m4.insert(make_pair(s.getXuehao(),i-1));  
    61.             s.clear2();  
    62.         }  
    63.     }  
    64.     int search4(int x)  
    65.     {  
    66.         mit4=m4.find(x);  
    67.         if(mit4!=m4.end())  
    68.         {  
    69.             return mit4->second;  
    70.         }  
    71.         else  
    72.         return -1;  
    73.     }  
    74. };  
    75.     void Adminop::save1()  
    76.     {  
    77.         ofstream outfile("d:\\20171808book.txt",ios::out);  
    78.         if(!outfile)  
    79.         return ;  
    80.         for(it3=v3.begin();it3!=v3.end();it3++)  
    81.         {  
    82.             outfile<<*it3;  
    83.         }  
    84.         outfile.close();  
    85.     }  
    86.     void Adminop::save2()  
    87.     {  
    88.         ofstream outfile("d:\\20171808stu.txt",ios::out);  
    89.         if(!outfile)  
    90.         return ;  
    91.         for(it4=v4.begin();it4!=v4.end();it4++)  
    92.         {  
    93.             outfile<<*it4;  
    94.         }  
    95.         outfile.close();  
    96.     }  
    97. class User  
    98. {  
    99.     vector<Book>v5;  
    100.     vector<Book>::iterator it5;  
    101.     multimap<int,int>m5;  
    102.     multimap<int,int>::iterator mit5;  
    103.     student s;  
    104.     Date t;  
    105. public:  
    106.     User(int x)  
    107.     {  
    108.         cout<<"当前日期,如2017 1 1 ;图书书号,如1001"<<endl;  
    109.         load3();  
    110.         load4(x);  
    111.     }  
    112.     ~User()  
    113.     {  
    114.         save3();  
    115.         save4();  
    116.     }  
    117.     void load3();  
    118.     void save3();  
    119.     void load4(int x);  
    120.     void save4();  
    121.     int search5(int x)  
    122.     {  
    123.         mit5=m5.find(x);  
    124.         if(mit5!=m5.end())  
    125.         {  
    126.             return mit5->second;  
    127.         }  
    128.         else  
    129.         return -1;  
    130.     }  
    131.     void borrow()  
    132.     {  
    133.         int x;  
    134.         cin>>x;  
    135.         int a=search5(x);  
    136.         s.huanshudat(t);  
    137.         if(s.getWeiji()==0&&s.getNnum()<s.getMaxnum())  
    138.         {  
    139.             v5[a].setLeadnum(v5[a].getLeadnum()+1);  
    140.             v5[a].setJilunum(v5[a].getJilunum()+1);  
    141.             s.setNnum(s.getNnum()+1);  
    142.             s.setRnum(s.getRnum()+1);  
    143.             Record r(s.getXuehao(),t,x);  
    144.             v5[a].addRecord1(r);  
    145.             s.addRecord2(r);  
    146.             cout<<"Borrow book successfully"<<endl;  
    147.         }  
    148.     }  
    149.     void back()  
    150.     {  
    151.         int x;  
    152.         cin>>x;  
    153.         int a=search5(x);  
    154.         v5[a].setLeadnum(v5[a].getLeadnum()-1);  
    155.         s.setNnum(s.getNnum()-1);  
    156.         v5[a].deleteRecord1(s.getXuehao());  
    157.         s.deleteRecord2(x);  
    158.         cout<<"return book successfully"<<endl;  
    159.     }  
    160. };  
    161.     void User::save3()  
    162.     {  
    163.         ofstream outfile("d:\\20171808book.txt",ios::out);  
    164.         if(!outfile)  
    165.         {  
    166.             return;  
    167.         }  
    168.         for(it5=v5.begin();it5!=v5.end();it5++)  
    169.         {  
    170.             outfile<<*it5;  
    171.         }  
    172.         outfile.close();  
    173.     }  
    174.     void User::load3()  
    175.     {  
    176.         Book b;  
    177.         int i;  
    178.         ifstream infile("d:\\20171808book.txt",ios::in);  
    179.         if(!infile)return;  
    180.         v5.clear();  
    181.         m5.clear();  
    182.         while(infile>>b)  
    183.         {  
    184.             if(b.getJilunum()!=0)  
    185.             for(int i=0;i<b.getJilunum();i++)  
    186.             {  
    187.                 Record r3;  
    188.                 infile>>r3;  
    189.                 b.addR1(r3);  
    190.             }  
    191.         v5.push_back(b);  
    192.         int i=v5.size();  
    193.         m5.insert(make_pair(b.getBookno(),i-1));  
    194.         b.clear1();  
    195.         }  
    196.         infile.close();  
    197.     }  
    198.     void User::load4(int x)  
    199.     {  
    200.         student s1;  
    201.         int i;  
    202.         cin>>t;  
    203.         ifstream infile("d:\\20171808stu.txt",ios::in);  
    204.         if(!infile)  
    205.         return;  
    206.         while(infile>>s1)  
    207.         {  
    208.             if(s1.getRnum()!=0)  
    209.             for(int i=0;i<s1.getRnum();i++)  
    210.             {  
    211.                 Record r4;  
    212.                 infile>>r4;  
    213.                 s1.addR2(r4);  
    214.             }  
    215.         if(s1.getXuehao()==x)  
    216.         this->s=s1;  
    217.         s1.clear2();  
    218.         }  
    219.         infile.close();  
    220.     }  
    221.     void User::save4()  
    222.     {  
    223.         vector<student>v;  
    224.         vector<student>::iterator it;  
    225.         student s1;  
    226.         ifstream infile("d:\\20171808stu.txt",ios::in);  
    227.         if(!infile)return ;  
    228.         while(infile>>s1)  
    229.         {  
    230.             if(s1.getRnum()!=0)  
    231.             for(int i=0;i<s1.getRnum();i++)  
    232.             {  
    233.                 Record r5;  
    234.                 infile>>r5;  
    235.                 s1.addR2(r5);  
    236.             }  
    237.         if(s1==this->s)  
    238.         s1=this->s;  
    239.         v.push_back(s1);  
    240.         s1.clear2();  
    241.         }  
    242.         infile.close();  
    243.         ofstream outfile("d:\\20171808stu.txt",ios::out);  
    244.         if(!outfile)return;  
    245.         for(it=v.begin();it!=v.end();it++)  
    246.         outfile<<*it;  
    247.         outfile.close();  
    248.     }  
  4. 程序的主函数:

  1. int main()  
  2. {  
  3. /* 
  4.    Adminop a;              //管理端定义对象
  5.  
  6.    a.addBook();            //增加图书,调用函数时会提示输入的信息,输入-1终止
  7.  
  8.    a.addstudent();         //增加学生用户,调用函数时会提示输入的信息,输入-1终止
  9.  
  10. */  
  11.   
  12. /* 
  13.    int xuehao; 
  14.    cout<<"请输入学号"; 
  15.    cin>>xuehao ; 
  16.    User h(xuehao);            //定义学生用户
  17.    //h.borrow();              //借书,函数调用时会提示输入借书日期、书号
  18.    h.back();                  //还书,函数调用时会提示输入还书日期、书号,如果逾期还书或者不还书,会把违纪记录变为1,将不能再借书
  19. */  
  20. }  


完整代码链接:https://paste.ubuntu.com/p/qzvvFbpnBm/

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值