图书管理系统学习与总结


能力有限,并不完整的图书管理系统


#include
using namespace std;
class Time
{
    int year,month,day;
    public:
    Time(){year=month=day=0;}
    Time(int y,int m,int d){year=y;month=m;day=d;}
    void setYear(int y){year=y;}
    void setMonth(int m){month=m;}
    void setDay(int d){day=d;}
    int getYear(){return year;}
    int getMonth(){return month;}
    int getDay(){return day;}
    void setTime(int y,int m,int d){year=y;month=m;day=d;}
    bool operator<(const Time&t1)const
    {
        return year!=t1.year?year>(istream&is,Time&t);
    friend ostream&operator<<(ostream&os,Time&t);
};
ostream&operator<<(ostream&os,Time&t)
{
    os<>(istream&is,Time&t)
{
    is>>t.year>>t.month>>t.day;
    return is;
}
class record
{
    int shuhao;
    int xuehao;
    Time jqi;
    Time hqi;
    int xjie;
    int state;
    public:
    record(){}
    record(int sh,int xh,Time jq):shuhao(sh),xuehao(xh),jqi(jq)
    {
        if(jq.getMonth()+2>12)
        {
            Time hq(jq.getYear()+1,jq.getMonth()-10,jq.getDay());
            hqi=hq;
        }
        else
        {
            Time hq(jq.getYear(),jq.getMonth()+2,jq.getDay());
            hqi=hq;
        }
        xjie=0;
        state=0;
}
    int getsh(){return shuhao;}
    void setsh(int sh){shuhao=sh;}
    int getxh(){return xuehao;}
    void setxh(int xh){xuehao=xh;}
    Time getjq(){return jqi;}
    Time gethq(){return hqi;}
    void sethq();
    void setxj(){xjie=1;}
    int getxj(){return xjie;}
    void setstate(int n){state=n;}
    int getstate(){return state;}
    void setrecord(int sh,int xh,Time jq,Time hq){shuhao=sh;xuehao=xh;jqi=jq;hqi=hq;}
    void display();
    friend ostream&operator<<(ostream&os,record&r);
    friend istream&operator>>(istream&is,record&r);
};
void record::sethq()
{Time hq;
    if(hqi.getMonth()+1>12)
    hq=Time(hqi.getYear()+1,hqi.getMonth()+1,hqi.getDay());
    else
    hq=Time(hqi.getYear()+1,hqi.getMonth(),hqi.getDay());
    hqi=hq;
}
void record::display()
{
    cout<<"书号:"<>(istream&is,record&r)
{
    if(r.xuehao==0)
    return is;
    is>>r.xuehao;
    is>>r.jqi;
    is>>r.shuhao;
    is>>r.hqi;
    is>>r.xjie;
    is>>r.state;
    return is;
}
ostream&operator<<(ostream&os,record&r)
{
    os<v1;
    vector::iterator it1;
    multimapm1;
    multimap::iterator mit1;
    book(){}
    book(int sh,string sm,string zz,string cbs,double p,int zc,int jc)
    {shuhao=sh;
    shumi=sm;
    cbans=cbs;
    zongce=zc;
    jiechu=jc;
    shengyu=zongce-jiechu;
    }
    vectorgetrecord(){return v1;}
    int getshuhao(){return shuhao;}
    string getshumi(){return shumi;}
    string getcbans(){return cbans;}
    string getzuozhe(){return zuozhe;}
    double getprice(){return price;}
    void setzongce(int zc){zongce=zc;}
    int getzongce(){return zongce;}
    void setjiechu(int jc){jiechu=jc;}
    int getjiechu(){return jiechu;}
    void add(record r1);
    void display();
};
void book::display()
{
        cout<>(istream&is,book&b)
{
    is>>b.shuhao;
    if(b.shuhao==-1)
    return is;
    is>>b.shumi;
    is>>b.zuozhe;
    is>>b.cbans;
    is>>b.price;
    is>>b.zongce;
    is>>b.jiechu;
}
ostream &operator<<(ostream&os,const book&b)
{
    os<v2;
    vector::iterator it2;
    multimapm2;
    multimap::iterator mit2;
    public:
    user(int xh,string na,int kj):xuehao(xh),name(na),kejie(kj){}
    user(){cq=0;}
    int getxh(){return xuehao;}
    void setxh(int xh){xuehao=xh;}
    string getna(){return name;}
    void setna(string na){name=na;}
    int getkj(){return kejie;}
    void setkj(int kj){kejie=kj;}
    void setcq(int kj){cq=kj;}
    int getcq(){return cq;}
    vectorgetrecord(){return v2;}
    void addr2(record r2);
    friend istream&operator>>(istream&is,user&u);
    friend ostream&operator<<(ostream&os,user&u);
    void operator=(user&u)
    {
        xuehao=u.getxh();
        name=u.getna();
        kejie=u.getkj();
        v2=u.getrecord();
    }
    bool operator==(user u)
    {
        return this->xuehao==u.getxh()?1:0;
    }
};
istream&operator>>(istream&is,user&u)
{
    is>>u.xuehao;
    if(u.xuehao==-1)
    return is;
    is>>u.name;
    is>>u.kejie;
    //is>>u.cq;
    return is;
}
ostream&operator<<(ostream&os,user&u)
{
    os<v3;
    vector::iterator it3;
    multimapm3;
    multimap::iterator mit3;
    vectorv4;
    vector::iterator it4;
    multimapm4;
    multimap::iterator mit4;
    public:
    manage(){}
    ~manage()
    {
        save1();
        save2();
    }
    void addbook();


    void adduser();


    void load1();
    void load2();
    void save1();
    void save2();
};
void manage::load1()//读取
{
    book b;
    int i;
    ifstream infile("d:\\20171748book.txt",ios::in);
    if(!infile)
    return;
    v3.clear();
    m3.clear();
    i=0;
    while(infile>>b)
    {v3.push_back(b);
     m3.insert(make_pair(b.getshuhao(),i-1));
    i++;
    }
    infile.close();
}
void manage::save1()
{
    ofstream outfile("d:\\20171748book.txt",ios::out);
    if(!outfile)
    return ;
    for(it3=v3.begin();it3!=v3.end();it3++)
    {
        outfile<<*it3;
    }
    outfile.close();
}
void manage::load2()
{
    user u;
    int i;
    ifstream infile("d:\\20171748stu.txt",ios::in);
    if(!infile)
    return;
    v4.clear();
    m4.clear();
    i=0;
    //infile>>u;
    while(infile>>u)
    {
        v4.push_back(u);
        m4.insert(make_pair(u.getxh(),i));
        i++;
    }
    infile.close();
}
void manage::save2()//写入
{
    ofstream outfile("d:\\20171748stu.txt",ios::out);
    if(!outfile)
    return ;
    for(it4=v4.begin();it4!=v4.end();it4++)
    {
        outfile<<*it4;
    }
    outfile.close();
}
void manage::addbook()
{


    int i;
    while(1)
    {
        book b;
        if(b.getshuhao()==-1)
        break;
        v3.push_back(b);
        i=v3.size();
        m3.insert(make_pair(b.getshuhao(),i-1));
        ofstream outfile("d:\\20171748book.txt",ios::app);
    if(!outfile)
    return ;
    outfile<>sh>>xh>>year>>month>>day;
        jq.setTime(year,month,day);
        record x(sh,xh,jq);
        x.display();
    }
}*/
class client
{
    vectorv5;
    vector::iterator it5;
    multimapm5;
    multimap::iterator mit5;
    user u;
    public:
    client(){load3();load4();}
    ~client(){save3();save4();}
    void load3();
    void save3();
    void load4();
    void save4();
    int search5(int sh);
    void querybook();
    void jie();
    void huan();
};
void client::save3()
{
    ofstream outfile("d:\\20171748book.txt",ios::out);
    if(!outfile)  return;
    for(it5=v5.begin();it5!=v5.end();it5++)
    outfile<<*it5;
    outfile.close();
}
void client::load3()
{
    book b;
    ifstream infile("d:\\20171748book.txt",ios::in);
    if(!infile)  return;
    v5.clear();
    m5.clear();
    while(infile>>b)
    {
        v5.push_back(b);
        int i=v5.size();
        m5.insert(make_pair(b.getshuhao(),i-1));
    }
    infile.close();
}
void client::load4()
{
    int xh;
    cin>>xh;
    user u;
    int i;
    ifstream infile("d:\\20171748stu.txt",ios::in);
    if(!infile) return;
    while(infile>>u)
    {
        if(u.getxh()==xh)
        this->u=u;
    }
    infile.close();
}
void client::save4()
{
    vectorv;
    vector::iterator it;
    user u1;
    ifstream infile("d:\\20171748stu.txt",ios::in);
    if(!infile)  return;
    while(infile>>u1)
    {
        if(u1==this->u)
        u1=this->u;
        v.push_back(u1);
    }
    infile.close();
    ofstream outfile("d:\\20171748stu.txt",ios::out);
    if(!outfile) return;
    for(it=v.begin();it!=v.end();it++)
    outfile<<*it;
    outfile.close();
}
void client::jie()
{
    int sh,shumi;
    Time t;
    cin>>sh>>t;


       record r(sh,u.getxh(),t);
   user u;
   u.addr2(r);
ofstream outfile("d:\\20171748book.txt",ios::app);
        outfile<>sh>>h;


}
int main()
{
    manage m;
    m.addbook();
    m.adduser();
    client c;
    c.jie();
    c.huan();
}

图书管理系统总结
1. 写作过程遇到的问题及解决方法
2. 如何自动生成文件以及写入数据?   通过翻阅书籍及资料,找到了生成文件的方法
3. 如何写入数据不覆盖?  上网搜了一下,把ios::in的 in 改成 app 可以做到这一点
4. STL如何使用? 看了很多遍的课件,也不是很懂,最后看了一下别人的代码,发现有很多一样的地方,问了同学,才堪堪解决了这个问题

注意
1. 写入文件后,一定要用 close 关闭
2. 析构函数是直接调用的
3. 重载输入输出流必须是友元函数

心得
因为之前的ATM有老师给的设计方案,所以思路清晰,写起来还算容易。而图书管理系统是要全靠自己,所以一上来有点慌,完全搞不懂思路,没有丁点头绪,再加上基础知识本就掌握不牢,STL是个啥?所以就想自己学一点知识后再写,想好步骤再动手。反正整个过程真是无比纠结,最后我发现写代码嘛,即使不会,也要写,说不定写着写着就有思路了,如果你不写,只是在脑子里想,越想越乱,反正我是这样。通过这次的作业,我发现写程序里面也是有很多套路的,很多地方都是差不多的,主要是理解了他的意思、含义,一切就很简单了(当然是以我并不完善的代码为例说的)所以说学好基础知识很关键啊!!!然后就是STL的使用,这次作业让我对他的了解更多了,起码会用了,但是要说熟练应用甚至完全掌握,还是远远不够的。不过不得不说的是真的很方便,有很多函数直接调用就好,比如vector里的push_back、close等等,节约时间,更节省脑力。

你可能感兴趣的:(c++学习总结)