图书管理系统总结

   经过数天的艰苦奋斗,简单的图书管理系统终于能够正常运行,实现了管理端的增加用户和增加图书功能、用户端的借书和还书功能,同时附加了借阅记录。

    虽然只是几个小小的功能,但由于对c++标准模板库STL的一知半解,小白还是在实现过程中遇到了重重阻碍。比如说,我们都知道STL中有动态数组vector和搜索multimap,通过map搜索可以快速、高效率查找所需内容,但是如何正确使用成为了我写代码的一大障碍。通过自主学习和请教同学之后,我大致明白了它们的用法。首先二者的构建和使用都要通过迭代器iterator来实现,其构建语句如下:

vector<Record>v1;

vector<Record>::iterator it1;

multimap<int,int>m1;

multimap<int,int>::iterator it1;//对整形数的查找

multimap<string,int>m2;

multimap<string,int>::iterator it2;//对字符串的查找

       管理端的增加用户和图书信息,就是通过loadsave函数,把图书和用户的信息键入,系统自动生成数据文本(txt格式),用以用户端的借书和还书操作。

    在实现用户端操作的时候,我们还要注意一个重要的细节:一定要对用户端“封死”,即一个用户只能对当前用户进行借书和还书操作,所以在用户端里要先判断一下是否为当前用户,然后才能进行借书和还书操作。同时,在借书和还书的时候,要增加借阅记录,即一个用户在借书(还书)之后,在图书和用户数据文件中相应图书和用户下面要跟着借阅(还书)记录。

以下附上源代码:

#include<bits/stdc++.h>
using namespace std;
class Time
{
    int year,month,day;
    public:
    Time()
    {
        year=0;
        month=0;
        day=0;
    }
    Time(int y,int m,int d)
    {
        year=y;
        month=m;
        day=d;
    }
    void setYear(int x){year=x;}
    void setMonth(int x){month=x;}
    void setDay(int x){day=x;}
    int getYear(){return year;}
    int getMonth(){return month;}
    int getDay(){return day;}
    bool operator<(const Time&t1)const
    {
        return year!=t1.year?year<t1.year:month!=t1.month?month<t1.month:day<t1.day;
    }
    friend istream&operator>>(istream&input,Time&t);
    friend ostream&operator<<(ostream&output,Time&t);
};
ostream&operator<<(ostream&output,Time&t)
{
    output<<t.year<<"/";
    output<<t.month<<"/";
    output<<t.day;
    return output;
}
istream&operator>>(istream&input,Time&t)
{
    input>>t.year>>t.month>>t.day;
    return input;
}
class Record
{
    int no;//学号
    Time js;//借书时间
    int bn;//书号
    Time hs;//还书时间
    int xj;//续借
    int state;//书的状态,即是否在借
    public:
    Record(){}
    Record(int x,Time y,int z)
    {
        no=x;
        js=y;
        bn=z;
        if(y.getMonth()+2>12)
        {
            Time t(y.getYear()+1,y.getMonth()-10,y.getDay());
            hs=t;

        }
        else
        {
            Time t(y.getYear(),y.getMonth()+2,y.getDay());
            hs=t;
        }
        xj=0;
        state=0;
    }
    int getNo(){return no;}
    Time getJs(){return js;}
    int getBn(){return bn;}
    Time getHs(){return hs;}
    void setHs();
    void setXj(){xj=1;}
    int getXj(){return xj;}
    void setState(int x){state=x;}
    int getState(){return state;}
    friend ostream&operator<<(ostream&output,Record&r);
    friend istream&operator>>(istream&input,Record&r);
};
void Record::setHs()//修改还书时间(续借)
{
    Time t;
    if(hs.getMonth()+1>12)
    t=Time(hs.getYear()+1,hs.getMonth()+1,hs.getDay());
    else
    t=Time(hs.getYear()+1,hs.getMonth(),hs.getDay());
    hs=t;
}
istream&operator>>(istream&input,Record&r)
{
    input>>r.no;
    input>>r.js;
    input>>r.bn;
    input>>r.hs;
    input>>r.xj;
    input>>r.state;
    return input;
}
ostream&operator<<(ostream&output,Record&r)
{
    output<<r.no<<" ";
    output<<r.js<<" ";
    output<<r.bn<<" ";
    output<<r.hs<<" ";
    output<<r.xj<<" ";
    output<<r.state<<endl;
    return output;
}
class Book
{
    int bn;//书号
    string name;//书名
    string address;//出版社
    int date;//出版日期
    int num;//在馆总册数
    int jc;//借出的数目
    int rnum;//还剩数目
    vector<Record>v1;
    vector<Record>::iterator it1;
    multimap<int,int>m1;
    multimap<int,int>::iterator mit1;
    public:
    Book(){rnum=0;}
    vector<Record>getRecord(){return v1;}
    int getBn(){return bn;}
    string getName(){return name;}
    string getAddress(){return address;}
    int getDate(){return date;}
    void setNum(int x){num=x;}
    int getNum(){return num;}
    void setJc(int j){jc=j;}
    int getJc(){return jc;}
    void setRnum(int n){rnum=n;}
    int getRnum(){return rnum;}
    void addRecord1(Record r1);
    void dispRecord1();
    void addR1(Record r1);
    void deleteRecord1(int no);
    friend istream&operator>>(istream&input,Book&b);
    friend  ostream&operator<<(ostream&output,Book&b);
    int search1(int no);
    void display();
    void modify(int no);
    void clear1();
};
void Book::display()
{
    cout<<bn<<endl;
    cout<<name<<endl;
    cout<<address<<endl;
    cout<<date<<endl;
}

istream&operator>>(istream&input,Book&b)
{
    input>>b.bn;
    if(b.bn==-1)
    return input;
    input>>b.name;
    input>>b.address;
    input>>b.date;
    input>>b.num;
    input>>b.jc;
    input>>b.rnum;
    return input;
}
ostream&operator<<(ostream&output,Book&b)
{
    output<<b.bn<<" "<<b.name<<" "<<b.address<<" "<<b.date<<" "<<b.num<<" "<<b.jc<<" "<<b.rnum<<endl;
    if(b.getRnum()!=0)
    for(int i=0;i<b.getRnum();i++)
    output<<b.v1[i];
    return output;
    }
    void Book::addR1(Record r1)
    {
        v1.push_back(r1);
        int i=v1.size();
        m1.insert(make_pair(r1.getNo(),i-1));
    }
void Book::addRecord1(Record r1)
{
    r1.setState(1);
    v1.push_back(r1);
    int i=v1.size();
    m1.insert(make_pair(r1.getNo(),i-1));
}
void Book::dispRecord1()
{
   if(v1.size()!=0)
   for(it1=v1.begin();it1!=v1.end();it1++)
   cout<<*it1;
   else
   cout<<"no record!"<<endl;
}
void Book::clear1()
{
    v1.clear();
}
int Book::search1(int no)
{
    mit1=m1.find(no);
    if(mit1!=m1.end())
    return mit1->second;
    else return -1;
}
void Book::modify(int no)
{
    int i=search1(no);
    if(v1[i].getXj()==0)
    {
        v1[i].setXj();
        v1[i].setHs();
    }
}
void Book::deleteRecord1(int no)
{
    int i;
    i=search1(no);
    if(i!=-1)
    {
      v1[i].setState(0);
    }
}

class User
{
    int no;//学号
    string name;//姓名
    int most_num;//最大借阅量
    int pre_num;//当前借阅量
    int wj;//违纪
    int rnum;//还能借书的册数
    vector<Record>v2;
    vector<Record>::iterator it2;
    multimap<int,int>m2;
    multimap<int,int>::iterator mit2;
    public:
    User()
    {
      wj=0;
      rnum=0;
    }
int getNo(){return no;}
void setName(string x){name=x;}
string getName(){return name;}
void setMost_num(int m){most_num=m;}
int getMost_num(){return most_num;}
void setPre_num(int x){pre_num=x;}
int getPre_num(){return pre_num;}
void setWj(int n){wj=n;}
int getWj(){return wj;}
void setRnum(int n){rnum=n;}
int getRnum(){return rnum;}
vector<Record>getRecord(){return v2;}
multimap<int,int>getMap(){return m2;}
void clear2();
void addRecord2(Record r2);
void dispRecord2();
int search2(int h);
void deleteRecord2(int h);
void modify(int h);
friend istream&operator>>(istream&input,User&u);
friend ostream&operator<<(ostream&output,User&u);
void pd(Time t);
void addR2(Record r);
void operator=(User&u)
{
    no=u.getNo();
    name=u.getName();
    most_num=u.getMost_num();
    pre_num=u.getPre_num();
    wj=u.getWj();
    rnum=u.getRnum();
    v2=u.getRecord();
    m2=u.getMap();
}
bool operator==(User u)
{
    return this->no==u.getNo()?1:0;
}
};
istream&operator>>(istream&input,User&u)
{
    input>>u.no;
    if(u.no==-1)
    return input;
    input>>u.name;
    input>>u.most_num;
    input>>u.pre_num;
    input>>u.wj;
    input>>u.rnum;
    return input;
}
ostream&operator<<(ostream&output,User&u)
{
    output<<u.no<<" "<<u.name<<" "<<u.most_num<<" "<<u.pre_num<<" "<<u.wj<<" "<<u.rnum<<endl;
    if(u.getRnum()!=0)
    for(int i=0;i<u.getRnum();i++)
    output<<u.v2[i];
    return output;
}
void User::addR2(Record r2)
{
    v2.push_back(r2);
    int i=v2.size();
    m2.insert(make_pair(r2.getBn(),i-1));
}
void User::addRecord2(Record r2)
{
    r2.setState(1);
    v2.push_back(r2);
    int i=v2.size();
    m2.insert(make_pair(r2.getBn(),i-1));
}
void User::dispRecord2()
{
   if(v2.size()!=0)
   for(it2=v2.begin();it2!=v2.end();it2++)
   cout<<*it2;
   else cout<<"no record"<<endl;
}
int User::search2(int h)
{
    mit2=m2.find(h);
    if(mit2!=m2.end())
    return mit2->second;
    else return -1;
}
void User::deleteRecord2(int h)
{
    int i;
    i=search2(h);
    if(i!=-1)
    {
        v2[i].setState(0);
    }
}
void User::pd(Time t)
{
    setWj(0);
    for(int i=0;i<v2.size();i++)
    if(v2[i].getHs()<t&&v2[i].getState()==1)
    {
        setWj(1);
        break;
    }
}
void User::modify(int h)
{
    int i=search2(h);
    if(v2[i].getXj()==0)
    {
        v2[i].setXj();
        v2[i].setHs();
    }
}

void User::clear2()
{
    v2.clear();
}
class Regulator
{
    vector<Book>v3;
    vector<Book>::iterator it3;
    multimap<int,int>m3;
    multimap<int,int>::iterator mit3;
    vector<User>v4;
    vector<User>::iterator it4;
    multimap<int,int>m4;
    multimap<int,int>::iterator mit4;
    public:
    Regulator(){}
    ~Regulator()
    {
        save1();
        save2();
    }
    void addBook();
    void dispBook();
    int search3(int h);
    void queryBySh();
    void addUser();
    void dispUser();
    int search4(int h);
    void load1();
    void load2();
    void save1();
    void save2();
};


void Regulator::save1()
{
    ofstream outfile("d:\\20171828book.txt",ios::out);
    if(!outfile)
    return ;
    for(it3=v3.begin();it3!=v3.end();it3++)
    {
        outfile<<*it3;
    }
    outfile.close();
}

void Regulator::save2()
{
    ofstream outfile("d:\\20171828stu.txt",ios::out);
    if(!outfile)
    return ;
    for(it4=v4.begin();it4!=v4.end();it4++)
    {
        outfile<<*it4;
    }
    outfile.close();
}
void Regulator::addBook()
{
    Book b;
    int i;
    while(cin>>b)
    {
        if(b.getBn()==-1) break;
        v3.push_back(b);
        i=v3.size();
        m3.insert(make_pair(b.getBn(),i-1));
        b.clear1();
    }
}

int Regulator::search3(int h)
{
    mit3=m3.find(h);
    if(mit3!=m3.end())
    {
        return mit3->second;
    }
    else
    return -1;

}
void Regulator::dispBook()
{
    for(it3=v3.begin();it3!=v3.end();++it3)
    cout<<*it3;
}
void Regulator::addUser()
{
    User u;
    int i;
    while(cin>>u)
    {
        if(u.getNo()==-1) break;
        v4.push_back(u);
        i=v4.size();
        m4.insert(make_pair(u.getNo(),i-1));
        u.clear2();
    }
}

int Regulator::search4(int no)
{
    mit4=m4.find(no);
    if(mit4!=m4.end())
    {
        return mit4->second;
    }
    else return -1;

}
void Regulator::dispUser()
{
    for(it4=v4.begin();it4!=v4.end();++it4)
    cout<<*it4;
}

class UserOp
{
    vector<Book>v5;
    vector<Book>::iterator it5;
    multimap<int,int>m5;
    multimap<int,int>::iterator mit5;
    User u;
    public:
    UserOp()
    {
     load3();
     load4();
    }
    ~UserOp()
     {
        save3();
        save4();
     }
     void load3();
     void save3();
     void load4();
     void save4();
     int search5(int h);
     void borrowbook();
     void retbook();
};

void UserOp::save3()
{
    ofstream outfile("d:\\20171828book.txt",ios::out);
    if(!outfile)
    {return ;}
    for(it5=v5.begin();it5!=v5.end();it5++)
    {
        outfile<<*it5;
    }
    outfile.close();
}
void UserOp::load3()
{
    Book b;
    int i;
    ifstream infile("d:\\20171828book.txt",ios::in);
    if(!infile)
    {
       return;
    }
    v5.clear();
    m5.clear();
    while(infile>>b)
    {
       if(b.getRnum()!=0)
       for(int i=0;i<b.getRnum();i++)
       {
        Record r3;
        infile>>r3;
        b.addR1(r3);
       }
       v5.push_back(b);
       int i=v5.size();
       m5.insert(make_pair(b.getBn(),i-1));
       b.clear1();
     }
     infile.close();
}
void UserOp::load4()
{
    int no;
    cin>>no;
    User u1;
    int i;
    ifstream infile("d:\\20171828stu.txt",ios::in);
    if(!infile)
    return;
    while(infile>>u1)
    {
        if(u1.getRnum()!=0)
        for(int i=0;i<u1.getRnum();i++)
        {
          Record r4;
          infile>>r4;
          u1.addR2(r4);
        }
        if(u1.getNo()==no)
        this->u=u1;
        u1.clear2();
    }
    infile.close();
}
void UserOp::save4()
{
    vector<User>v;
    vector<User>::iterator it;
    User u2;
    ifstream infile("d:\\20171828stu.txt",ios::in);
    if(!infile) return;
    while(infile>>u2)
    {
       if(u2.getRnum()!=0)
       for(int i=0;i<u2.getRnum();i++)
       {
           Record r5;
           infile>>r5;
           u2.addR2(r5);
       }
       if(u2==this->u)
       u2=this->u;
       v.push_back(u2);
       u2.clear2();
    }
    infile.close();

    ofstream outfile("d:\\20171828stu.txt",ios::out);
    if(!outfile) return;
    for(it=v.begin();it!=v.end();it++)
    outfile<<*it;
    outfile.close();
}
int UserOp::search5(int h)
{
    mit5=m5.find(h);
    if(mit5!=m5.end())
    {
        return mit5->second;
    }
    else return -1;
}
void UserOp::borrowbook()
{
    int h,no;
    Time t;
    cin>>h;
    cin>>t;
    int a=search5(h);
    u.pd(t);
    if(u.getWj()==0&&u.getPre_num()<u.getMost_num())
    {
        v5[a].setJc(v5[a].getJc()+1);
        v5[a].setRnum(v5[a].getRnum()+1);
        u.setPre_num(u.getPre_num()+1);
        u.setRnum(u.getRnum()+1);
        Record r(u.getNo(),t,h);
        v5[a].addRecord1(r);
        u.addRecord2(r);
     }
}
void UserOp::retbook()
{
   int h,no;
   cin>>h;
   int i=search5(h);
   v5[i].setJc(v5[i].getJc()-1);
   u.setPre_num(u.getPre_num()-1);
   v5[i].deleteRecord1(u.getNo());
   u.deleteRecord2(h);
}
int main()
{

    /*Regulator r;
    r.addBook();
    r.addUser();
    UserOp u;
    u.borrowbook();
    u.borrowbook();
    u.retbook();*/
}


     

  

     

     

  

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值