图书馆管理系统--STL

#include<fstream>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>

using namespace std;



class time
{
    int year, month, day;

public:

    time(int x, int y, int z){year = x;month = y;day = z;}
    time() {};

    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;}

    void display();
    time operator +(int x);
    bool operator <(time b);
    friend istream & operator >>(istream & is, time &);
    friend ostream & operator <<(ostream & os, time &);
    time(const time & A)
    {
        year=A.year;
        month=A.month;
        day=A.day;
    }
};
void time::display()
{
    cout<<year<<"年"<<month<<"月 "<<day<<"日"<<endl;
}
time time::operator+(int x)
{
    time temp;
    for (int i = 1; i <= x; i++)
    {
        if (day<30)day+=1;
        else
        {
            day=1;
            if (month<12)month+=1;
            else
            {
                year++;
                month=1;
            }
        }
    }
    temp.year=year;
    temp.day=day;
    temp.month=month;
    return temp;

}
bool time::operator < (time b)
{
    if (year<b.year)return 1;
    if (month<b.month)return 1;
    if (day<b.day)return 1;
    return 0;
}
istream   & operator   >>(istream & input,time & A)
{
    input >> A.year >> A.month >> A.day;
    return input;
}
ostream & operator <<(ostream & output,time & A)
{
    output << A.year << "年" << A.month << "月" << A.day<<"日" ;
    return output;

}

time t_now;

class record
{

    int xuehao;
    int no;
    time t1;
    time t2;
    time t3;
    int xj;
    bool state;

    public:

    record(int x,time t,int y):xuehao(x),t1(t),no(y){t2=t1+60;xj=0;state=0;}
    record(){xj=0;state=0;}

    void setrecord(int x,time t,int y)
    {
        t1=t;
        xuehao=x;
        no=y;
        t2=t1+60;
        xj=0;
        state=0;
    }
    int getxuehao(){return xuehao;}
    int getno(){return no;}
    time gett1(){return t1;}
    time gett2(){return t2;}
    time gett3(){return t2;}
    bool getstate(){return state;}
    bool getxujie(){return xj;}


    void retbook(time t){state=1;t3=t;}
    void xujie(){xj=1;t2=t2+60;}
    void display()
    {
        cout<<xuehao<<"在"<<t1<<"借了书号为"<<no<<"的书。应在"<<t2<<"归还!  ";
        if(xj==1)cout<<"续借过一次!   ";
        else cout<<"没续借过   ";

        if(state==0&&t2<t_now)cout<<" 超时且未归还图书!!   ";
        else
        if(state==0)cout<<"还未归还书!   ";
        else
        if(state==1&&t3<t2)cout<<"  已按时归还图书.在"<<t3;
        else
        cout<<"  超时归还!在"<<t3;
        cout<<endl;

    }
    bool check()    //如果有违规记录返回0;没有违规记录就返回1;
{

        if(state==0&&t2<t_now)   //现在的时间过了还书期限
        return 0;
        else
        if(state==0&&t_now<t2)return 1;  //现在的时间还没超过还书期限
        else
        if(state==1&&t3<t2)     //书还了 按时还的。
        return 1;
        else
        if(state==1&&t2<t3)   //超时还书
        return 0;
        else
        return 1;


}
     friend istream & operator >>(istream & is,record & a)
     {
         is>>a.xuehao>>a.t1>>a.no>>a.t2>>a.xj>>a.state>>a.t3;
     }
     friend ostream &operator <<(ostream & os,record & r)
     {
        os<<r.xuehao<<"在"<<r.t1<<"借了书号为"<<r.no<<"的书。应在"<<r.t2<<"归还!  ";
        if(r.xj==1)os<<"续借过一次!   ";
        else os<<"没续借过   ";

        if(r.state==0&&r.t2<t_now)os<<" 超时且未归还图书!!   ";
        else
        if(r.state==0)os<<"还未归还书!   ";
        else
        if(r.state==1&&r.t3<r.t2)os<<"  已按时归还图书.在"<<r.t3;
        else
        os<<"  超时归还!在"<<r.t3;
        os<<endl;

     }
};

class book
{
    int no,price,state;  //书号,价格,有多少本在架
    string shuming,chubanshe,zuozheming;
    time cbtime;   //出版时间d
    vector<record>r;
    vector<record>::iterator it;
    int flag;

public :

    book(int x,string y,string z,string b,time t,int p,int s):no(x),shuming(y),chubanshe(z),zuozheming(b),cbtime(t),price(p),state(s) {flag=0;}
    book() {flag=0;}
    book(int x){no=x;}

    void dele(){flag=1;}
    void clear(){r.clear();}
    void setno(int x){no=x;}
    void setshuming(string x){x=shuming;}
    void setchubanshe(string x){x=chubanshe;}
    void setzuozheming(string x){x=zuozheming;}
    void setcbtime(time t){cbtime=t;}
    void setprice(int x){price=x;}
    void setstate(int x){state=x;}

    int getflag(){return flag;}
    int getno(){return no;}
    string getshuming(){return shuming;}
    string getchubanshe(){return chubanshe;}
    string getzuozheming(){return zuozheming;}
    time getcbtime(){return cbtime;}
    int getprice(){return price;}
    int getstate(){return state;}


    bool operator==(book a){if(a.no==no)return 1;return 0;}
    void borrow();
    void retbook(time t1);
    friend istream & operator >>(istream & is,book &);
    friend ostream & operator <<(ostream & os,book &);
    void addrecord(int x,int y,time t)
    {   record  rr(x,t,y);
        r.push_back(rr);
    }
    void addrecord(record rr)
    {
        r.push_back(rr);
    }
    void displaybyxuhao(int x);
    int check();
    void xujie(int x);
};
void book::xujie(int x)
{
    int len=r.size();
    for(int i=0;i<len;i++)
    if(r[i].getxuehao()==x)
    r[i].xujie();
}
int book::check()
{
    int len=r.size();
    for(int i=0;i<len;i++)
    if(r[i].check())return 1;
    else return 0;


}
void book::displaybyxuhao(int x)
 {
        for(it=r.begin();it!=r.end();it++)
        if((*it).getxuehao()==x)(*it).display();
    }
void book::borrow()
{
    if(state>=1)state--;
    else
        cout<<"no book anymore!"<<endl;
}
void book::retbook(time t1)
{
    state++;
    int len2=r.size();
    for(int i=0;i<len2;i++)
    if(r[i].getno()==no)r[i].retbook(t1);
}
istream & operator >> (istream & is,book & A)
{
    is>>A.no>>A.shuming>>A.zuozheming>>A.chubanshe>>A.cbtime>>A.price>>A.state;
    return is;
}
ostream & operator << (ostream & os,book & A)
{
    if(A.flag==0)
    {
     os<<A.no<<" "<<A.shuming<<" "<<A.zuozheming<<" "<<A.chubanshe<<" "<<A.cbtime<<" "<<A.price<<" "<<A.state<<endl;
     int len=A.r.size();
     os<<"record:"<<endl;
     if(len==0)os<<"无记录!"<<endl<<endl;
     for(int i=0;i<len;i++)
     os<<A.r[i]<<endl;
     return os;
     }
}

class reader
{
    int xuehao,num;
    string name;
    vector<book>a;
    vector<book>::iterator it;
    vector<record>r;
    vector<record>::iterator it1;
    bool flag;

public:
    reader(int x,string y):xuehao(x),name(y) {flag=0;}
    reader() {flag=0;}

    void dele(){flag=1;}
    void clear(){a.clear();r.clear();}
    void setxuehao(int x){xuehao=x;}
    void setname(string x){name=x;}

    int getflag(){return flag;}
    int getxuehao(){return xuehao;}
    string getname(){return name;}
    int getsize(){int len=r.size();return len;}

    void borrow(book r);
    void retbook(book r,time t1);
    bool operator ==(reader a)
    {
        if(xuehao==a.xuehao)return 1;
        else return 0;
    }
    friend istream & operator >>(istream & is,reader &);

    friend ostream & operator <<(ostream & os,reader &);



    void addrecord(int x,int y,time t)
    {   record  rr(x,t,y);
        r.push_back(rr);
    }
    void addrecord(record rr)
    {
        r.push_back(rr);
    }
    void displaybyno(int x);
    int check();
    void xujie(int x);
};
void reader::xujie(int x)
{
    int len=r.size();
    for(int i=0;i<len;i++)
   {
    if(r[i].getno()==x)
    r[i].xujie();

    }
}
int reader::check()
{
    int len=r.size();
    for(int i=0;i<len;i++)

    if(!r[i].check())return 0;

    return 1;
    }

void reader::displaybyno(int x)
 {
        for(it1=r.begin();it1!=r.end();it1++)
        if((*it1).getno()==x)
        (*it1).display();
    }

istream & operator >>(istream & is,reader & a)
{
    is>>a.xuehao>>a.name;
    return is;
}

ostream & operator <<(ostream & os,reader & a)
{if(a.flag==0)
   {

os<<a.xuehao<<" "<<a.name<<endl;

    for(int i=0; i<a.a.size(); i++)
    {
        os<<"借的第"<<i+1<<"本书的书号:"<<a.a[i].getno();
        os<<endl;
    }

    os<<"record:"<<endl;
     int len=a.r.size();
     if(len==0)os<<"无记录!"<<endl<<endl;
     for(int i=0;i<len;i++)
     os<<a.r[i];
     os<<endl;
    return os;
   }

}

void reader::borrow(book r)
{
    int i=a.size();
    if(i==3)cout<<"不能再借书了!"<<endl;
    else
    a.push_back(r);
}

void reader::retbook(book r1,time t1)
{
    int len1=a.size();
    int len2=r.size();

    for(it=a.begin();it!=a.end();it++)
          if((*it).getno()==r1.getno())
                a.erase(it);

    for(int i=0;i<len2;i++)
          if(r[i].getno()==r1.getno())
                r[i].retbook(t1);


}


class base
{
    vector<book>b;
    vector<book>::iterator it1;
    vector<reader>r;
    vector<reader>::iterator it2;
    multimap<int,int>m1;
    multimap<int,int>::iterator mit1;  //按书号查找图书
    multimap<int,int>m2;
    multimap<int,int>::iterator mit2;   //按学号查找读者
    public:

    base()
    {
        load1();
        load2();

    }
     ~base()
     {
       save1();
       save2();
     }
      void delebook(int x);   //删除一本图书
      void delereader(int y);   //删除一位读者
      void addbook(book x){b.push_back(x);}
      void addreader(reader x){r.push_back(x);}
      void load1();
      void load2();
      void save1();
      void save2();
      void borrow(reader x,int y,time t1);
      void retbook(int x,int y,time t1);
      int querybook(int x);
      int queryreader(int x);
      void displaybyno(int x);
     void displaybyxuehao(int x);
     void changeno(int x,int y);
};
void base::changeno(int x,int y)
{
    mit1=m1.find(x);
    if(mit1!=m1.end())
    b[mit1->second].setno(y);
    else cout<<"change error:no  this   no!"<<endl;

}
void base::displaybyno(int x)
{
    mit1=m1.find(x);
    if(mit1!=m1.end())
    cout<<b[mit1->second];
    else cout<<"querry error:no  this   book!"<<endl;
}
void base::displaybyxuehao(int x)
{
    mit2=m2.find(x);
    if(mit2!=m2.end())
    cout<<r[mit2->second];
    else cout<<"querry error:no this reader!"<<endl;
}
int base::querybook(int x)
{
    mit1=m1.find(x);
    if(mit1!=m1.end())
    return mit1->second;
    else return -1;
}
int base::queryreader(int x)
{
    mit2=m2.find(x);
    if(mit2!=m2.end())
    return mit2->second;
    else
    return -1;
}
void base::load1()
{
   cout<<"请输入图书(1 普通物理学 曹学成 中国农业出版社 2014 2 1 32 5):"<<endl;
    book b1;

    while(cin>>b1&&b1.getno()!=-1)
    {
        b.push_back(b1);
        int i=b.size();
        m1.insert(make_pair(b1.getno(),i-1));
        b1.clear();
    }

}
void base::load2()
{
    cout<<"请输入读者(20171746 虞启贤):"<<endl;
    reader r1;
    while(cin>>r1&&r1.getxuehao()!=-1)
    {
        r.push_back(r1);
        int i=r.size();
        m2.insert(make_pair(r1.getxuehao(),i-1));
        r1.clear();
    }

}
void base::save1()
{
 ofstream outfile("20171746book.txt",ios::out);
 int len=b.size();
 for(int b1=0;b1<len;b1++)
 {
     if(b[b1].getflag()==0)
      {time t=b[b1].getcbtime();
        outfile<<b[b1].getno()<<" ";
       outfile<<b[b1].getshuming()<<" ";
       outfile<<b[b1].getzuozheming()<<" ";
       outfile<<b[b1].getchubanshe()<<" ";
       outfile<<t.getyear()<<" "<<t.getmonth()<<" "<<t.getday()<<" ";
       outfile<<b[b1].getprice()<<" "<<b[b1].getstate()<<endl;
      }
 }
  outfile.close();
}
void base::save2()
 {

    ofstream outfile("20171746stu.txt",ios::out);
    if(!outfile)return ;
    int len=r.size();
    for(int r1=0;r1<len;r1++)
    if(r[r1].getflag()==0)
    outfile<<r[r1].getxuehao()<<" "<<r[r1].getname()<<endl;
    outfile.close();
}
void base::delebook(int x)
{  int j=querybook(x);
    if(b[j].getflag()==0)
    {mit1=m1.find(x);
    if(mit1!=m1.end())
     b[mit1->second].dele();
    else
    cout<<"delebook error:no this book1!"<<endl;
    }
    else
    cout<<"delebook error:no this book2!"<<endl;
}
void base::delereader(int x)
{   int i=queryreader(x);
  if(r[i].getflag()==0)
   {
    mit2=m2.find(x);
    if(mit2!=m2.end())
    r[mit2->second].dele();
    else
    cout<<"delereader error:no this reader1!"<<endl;
   }
    else
    cout<<"delereader error:no this reader2!"<<endl;
}


class Client              //查书 借书 还书。
{

    vector<book>b;
    vector<book>::iterator it1;
    vector<reader>r;
    vector<reader>::iterator it2;
    multimap<int,int>m1;
    multimap<int,int>::iterator mit1;
    int k;

    public:
    Client()
    {
        load1();
        load2();

    }
     ~Client()
     {
         save1();
         save2();
     }
 void load1();
 void load2();
 void save1();
 void save2();
 void borrow(int x);
 void retbook(int x);
 void displaybook(int x);
 int querrybook(int x);
 void displayself();
 void xujie(int x);
};
void Client::load1()
{
    cout<<"请输入学号(20171746/20171754/20171828):";
    int x;
    cin>>x;
    cout<<endl;

    reader rr;
    ifstream infile;
    infile.open("20171746stu.txt",ios::in);
    while(infile>>rr)
    {
        if(rr.getxuehao()==x){ k=r.size();}
        r.push_back(rr);
    }
    infile.close();
}
void Client::load2()
{
      book b1;
    ifstream infile("20171746book.txt",ios::in);
    while(infile>>b1)
    {

        b.push_back(b1);
        int i=b.size();
        m1.insert(make_pair(b1.getno(),i-1));
        b1.clear();
    }
    infile.close();
}
void Client::save1()
{
    ofstream outfile("20171746bookout.txt",ios::out);

    if(!outfile)return ;

    for(it1=b.begin();it1!=b.end();it1++)
    outfile<<(*it1);

    outfile.close();
}
void Client::save2()
{
     ofstream outfile("20171746stuout.txt",ios::out);

    if(!outfile)return ;
    for(it2=r.begin();it2!=r.end();it2++)
    outfile<<(*it2);

    outfile.close();
}
void Client::borrow(int x)
{
    if(r[k].check()==1)
    {
    int i=querrybook(x);
    if(i==-1)cout<<"borrow error:no this book"<<endl;
    else
    {

    b[i].addrecord(r[k].getxuehao(),x,t_now);
    r[k].addrecord(r[k].getxuehao(),x,t_now);
    b[i].borrow();
    r[k].borrow(b[i]);
    }
    }
    else cout<<"有违规记录,无法借书!"<<endl;

}
int Client::querrybook(int x)
{
    mit1=m1.find(x);
    if(mit1!=m1.end())
    return mit1->second;
    else return -1;
}
void Client::displaybook(int x)
{
     mit1=m1.find(x);
    if(mit1!=m1.end())
    cout<<b[mit1->second];
    else cout<<"querry error::no this book!"<<endl;
}
void Client::retbook(int x)
{
    int i=querrybook(x);
    r[k].retbook(b[i],t_now);
    b[i].retbook(t_now);
}
void Client::displayself()
{
    cout<<r[k];
}
void Client::xujie(int x)
{

    int i=querrybook(x);
    b[i].xujie(r[k].getxuehao());
    r[k].xujie(x);
}


int main()
{
   /* cout<<"请输入现在的时间:";
    cin>>t_now;
    cout<<endl;
    Client u;

    u.borrow(2);
    u.xujie(2);*/


/*   base u;*/


 /*cout<<"请输入现在的时间:";
    cin>>t_now;
    cout<<endl;
    Client u;
    u.displaybook(3);
    u.borrow(2);
    u.borrow(3);
    u.retbook(2);
   u.displayself();*/

   /* base u;
     u.displaybyno(1);
      u.displaybyxuehao(20171746);
      u.changeno(5,6);
      u.delebook(4);
*/

    /*base u;
    time t(2011,6,1);
    book b(6,"数据结构(c++版)","王红梅","清华大学出版社",t,29,10);
    reader r(20171825,"李辉");
    u.addbook(b);
    u.addreader(r);*/
}






猜你喜欢

转载自blog.csdn.net/qq_40893490/article/details/80785272