5月25日學習總結

這幾天沒學啥新鮮的,今天就分享一下圖書館代碼吧!這個代碼比較繁瑣,大家可以在裏面找找自己需要的信息,希望有所幫助。與君共勉!

#include<bits/stdc++.h>
using namespace std;
class Book//在這裏定義圖書類,存書籍信息
{
    string shuming,bianhao,zuozhe;
    int money,allshu,outshu=0;
    public:
    void setoutshu(int);
    void setallshu()
    {
        cout<<"newnumber:";
        int n;
        cin>>n;
        allshu=n;
    }
    string outshuming(){return shuming;}
    string outzuozhe(){return zuozhe;}
    friend ostream& operator<<(ostream& os,const Book& c);//重新定義輸入輸出流
    friend istream& operator>>(istream& is,Book& c);
};
ostream& operator<<(ostream& os,const Book& c)
{
    cout<<"bookname/author/note/number/outnumber/herenumber/money"<<endl;
    os<<c.shuming<<" "<<c.zuozhe<<" "<<c.bianhao<<" "<<c.allshu<<" "<<c.outshu<<" "<<(c.allshu-c.outshu)<<" "<<c.money<<endl;
}
istream& operator>>(istream& is,Book& c)
{
    cout<<"bookname/author/note/number/money"<<endl;
    is>>c.shuming>>c.zuozhe>>c.bianhao>>c.allshu>>c.money;
}
void Book::setoutshu(int b)
{
    outshu=outshu+b;
}
class Student//定義一個學生類
{
    string stuname,xuehao,nianji,zhuanye,banji;
    int canjie=10,hadjie=0,cun=1;
    public:
    Student(string n,string x,string nian,string zhu,string ban):stuname(n),xuehao(x),nianji(nian),zhuanye(zhu),banji(ban){}
    Student(){}
    string outxuehao(){return xuehao;}
    string outnianji(){return nianji;}
    void sethadjie(int);
    int outcun(){return cun;}
    void setcun(){cun=0;}
    void fixstudent();
    friend ostream& operator<<(ostream& os,const Student& c);
    friend istream& operator>>(istream& is,Student& c);
};
ostream& operator<<(ostream& os,const Student& c)
{
    cout<<"name/xuehao/nianji/zhuanye/banji/yijie/kejie"<<endl;
    os<<c.stuname<<" "<<c.xuehao<<" "<<c.nianji<<" "<<c.zhuanye<<" "<<c.banji<<" "<<c.hadjie<<" "<<(c.canjie-c.hadjie)<<endl;
}
istream& operator>>(istream& is,Student& c)
{
    cout<<"name/xuehao/nianji/zhuanye/banji"<<endl;
    is>>c.stuname>>c.xuehao>>c.nianji>>c.zhuanye>>c.banji;
}
void Student::fixstudent()
{
    string newnianji,newzhuanye,newbanji;
    cout<<"newnianji/newzhuanye/newbanji"<<endl;
    cin>>newnianji>>newzhuanye>>newbanji;
    nianji=newnianji;
    zhuanye=newzhuanye;
    banji=newbanji;
}
void Student::sethadjie(int b)
{
    if(b==1)
    {
        hadjie++;
    }
    else if(b==0)
    {
        hadjie--;
    }
    else
    {
        cout<<"intput error"<<endl;
    }
}//以上兩個類作爲數據類,下面就是操作類
class Manage//定義管理員類對圖書信息與學生信息進行整理
{
    friend class Record;
    //vector<Record>allrecord;
    vector<Student>stuall;
    map<string,int>findstu;
    multimap<string,int>byyear;
    vector<Book>all;
    multimap<string,int>findbook;
public:
    void addstudent();
    void fixstudent();
    void offstudent();
    void findstudent();
    void addbook();
    void fixbook();
    void obbook();
    void findbooks();
    void readstudent();
    void writestudent();
    void readbook();
    void writebook();
};
void Manage::readbook()//從文件中讀取圖示信息
{
    ifstream in;
    in.open("C:\\GameDownload\\book.txt");
    while(!in.eof())
    {
        string one;
        getline(in,one);
        if(one.empty())
        continue;
        istringstream is(one);
        Book two;
        is>>two;
        all.push_back(two);
        findbook.insert(make_pair(two.outshuming(),all.size()-1));
    }
    in.close();
}
void Manage::writebook()//將圖書信息寫到文件中
{
    ofstream out;
    out.open("C:\\GameDownload\\book1.txt");
    for(auto it1=all.begin();it1!=all.end();it1++)
    {
        out<<(*it1);
    }
    out.close();
}
void Manage::readstudent()
{
    ifstream in;
    in.open("C:\\GameDownload\\student.txt");
    while(!in.eof())
    {
        string one;
        getline(in,one);
        if(one.empty())
        continue;
        istringstream is(one);
        Student two;
        is>>two;
        stuall.push_back(two);
        findstu.insert(make_pair(two.outxuehao(),stuall.size()-1));
        byyear.insert(make_pair(two.outnianji(),stuall.size()-1));
    }
    in.close();
}
void Manage::writestudent()
{
    ofstream out;
    out.open("C:\\GameDownload\\student1.txt");
    for(auto it1=stuall.begin();it1!=stuall.end();it1++)
   {
        if((*it1).outcun())
        out<<(*it1);
    }
    out.close();
}
void Manage::addstudent()
{
    Student three;
    cin>>three;
    map<string,int>::iterator it1;
    it1=findstu.find(three.outxuehao());
    if(it1==findstu.end())
    {
        stuall.push_back(three);
        findstu.insert(make_pair(three.outxuehao(),stuall.size()-1));
        byyear.insert(make_pair(three.outnianji(),stuall.size()-1));
    }
    else
    {
        cout<<"already have this note,add defeat"<<endl;
    }
}
void Manage::findstudent()
{
    string findxue;
    cout<<"xuehao:";
    cin>>findxue;
    map<string,int>::iterator it1;
    it1=findstu.find(findxue);
    if(it1!=findstu.end())
    {
        if(stuall[it1->second].outcun())
        {
            cout<<stuall[it1->second];
        }
        else
        {
            cout<<"not have student"<<endl;
        }
    }
    else
    {
        cout<<"not have student"<<endl;
    }
}
void Manage::fixstudent()
{
    string findxue;
    cout<<"xuehao:";
    cin>>findxue;
    map<string,int>::iterator it1;
    it1=findstu.find(findxue);
    if(it1!=findstu.end())
    {
        stuall[it1->second].fixstudent();
    }
    else
    {
        cout<<"not have student"<<endl;
    }
}
void Manage::offstudent()
{
    cout<<"offbyyear/0 offbyone/1"<<endl;
    int n;
    cin>>n;
    if(n==0)
    {
        string bynian;
        cin>>bynian;
        multimap<string,int>::iterator it1;
        it1=byyear.lower_bound(bynian);
        if(it1!=byyear.end())
        {
            for(;it1!=byyear.upper_bound(bynian);it1++)
            {
                stuall[it1->second].setcun();
            }
        }
    }
    else
    {
        string findxue;
        cout<<"xuehao:";
        cin>>findxue;
        map<string,int>::iterator it1;
        it1=findstu.find(findxue);
        if(it1!=findstu.end())
       {
           stuall[it1->second].setcun();
       }
       else
       {
           cout<<"not have student"<<endl;
       }
    }
}
void Manage::addbook()
{
    Book one;
    cin>>one;
    all.push_back(one);
    findbook.insert(make_pair(one.outshuming(),all.size()-1));
}
void Manage::findbooks()
{
    string zhaoshu,author;
    cout<<"bookname/author"<<endl;
    cin>>zhaoshu>>author;
    multimap<string,int>::iterator it1;
    it1=findbook.lower_bound(zhaoshu);
    if(it1!=findbook.end())
    {
        for(;it1!=findbook.upper_bound(zhaoshu);it1++)
        {
            if(all[it1->second].outzuozhe()==author)
            {
                cout<<all[it1->second];
            }
        }
    }
    else
    {
        cout<<"not found"<<endl;
    }
}
void Manage::fixbook()
{
    string zhaoshu,author;
    cout<<"bookname/author"<<endl;
    cin>>zhaoshu>>author;
    multimap<string,int>::iterator it1;
    it1=findbook.lower_bound(zhaoshu);
    if(it1!=findbook.end())
    {
        for(;it1!=findbook.upper_bound(zhaoshu);it1++)
        {
            if(all[it1->second].outzuozhe()==author)
            {
                all[it1->second].setallshu();
            }
        }
    }
    else
    {
        cout<<"not found"<<endl;
    }
}
void Manage::obbook()
{
    string zhaoshu,author;
    cout<<"bookname/author"<<endl;
    cin>>zhaoshu>>author;
    multimap<string,int>::iterator it1;
    it1=findbook.lower_bound(zhaoshu);
    if(it1!=findbook.end())
    {
        for(;it1!=findbook.upper_bound(zhaoshu);it1++)
        {
            if(all[it1->second].outzuozhe()==author)
            {
                cout<<"outbook/1 backbook/0"<<endl;
                int n;
                cin>>n;
                if(n==1)
                {
                    all[it1->second].setoutshu(1);
                }
                else if(n==0)
                {
                    all[it1->second].setoutshu(-1);
                }
                else
                {
                    cout<<"intput error"<<endl;
                }
            }
        }
    }
}
int main()
{
    int s;
    Manage two;
    two.readstudent();
    two.readbook();
    while(cin>>s)
    {
        if(s==1)
        {
            two.addstudent();
        }
        else if(s==2)
        {
            two.findstudent();
        }
        else if(s==3)
        {
            two.fixstudent();
        }
        else if(s==4)
        {
            two.offstudent();
        }
        else if(s==5)
        {
            two.addbook();
        }
        else if(s==6)
        {
            two.findbooks();
        }
        else if(s==7)
        {
            two.fixbook();
        }
        else if(s==8)
        {
            two.obbook();
        }
        else if(s==9)
        {
            return 0;
        }
    }
    two.writestudent();
    two.writebook();
}

以上代碼只實現了對圖書和學生的增刪查改,圖書借閱記錄沒有完成,過兩天再繼續完善此代碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章