c程大作业lib

#include  <iostream>
#include  <map>

using namespace std;

struct book{
    string name;
    int id;
    int num;
    int res;//shenyu
    int room;
    int x,y;//x is row,y is column
};
char query;
string str;
map<int,book> books;
map<string,int>bm;
void insert(int i);
void find(int i);
void del(int i);
void brow(int i);
void ret(int i);
void test(){
    brow(bm["b"]);
    brow(bm["b"]);
    ret(bm["b"]);
    brow(bm["b"]);
    del(bm["a"]);
    find(bm["a"]);

}
void tip(){
    cout<<"F:find a book:"<<endl;
    cout<<"I:insert a book:"<<endl;
    cout<<"Q:quit the system"<<endl;
    cout<<"D:delete a book"<<endl;
    cout<<"B:borrow a book"<<endl;
    cout<<"R:return a book"<<endl;
}

int main(int argc, char *argv[]) {
    FILE *fp=freopen("data.in","r",stdin); 
    int n;
    cin>>n;
        for(int i=1;i<=n;i++){
        insert(i);
    }

    fflush(fp);//将输出缓冲区清空
    freopen("/dev/tty", "w", stdout); 
    freopen("/dev/tty", "r", stdin); 

    //test();
    tip();
    cin>>query;
    while(query!='Q'){
        cin>>str;
        int x=bm[str];
        if(query=='I'){
            insert(x);
        }
        if(query=='F'){
            find(x);
        }
        if(query=='D')
            del(x);
        if(query=='B'){
            brow(x);
        }
        if(query=='R'){
            ret(x);
        }
        cout<<endl;
        tip();
        cin>>query;
        }


    /*freopen("data.out", "w", stdout);
    for (map<int,book>::iterator map_it = map_str.begin(); map_it != map_str.end(); map_it++)
    {
          cout << "key:  " << map_it->first << "value:   " << map_it->second;   
       // map_it->first表示键值对的第一个值,也就是键的值,map_it->second表示键值对的第二个值,也就是键对应的值
    }   fclose(stdout);*/

}
void insert(int i){
    struct book a;
    cin>>a.name>>a.id>>a.num>>a.res>>a.room>>a.x>>a.y;
    books[a.id]=a;
    bm[a.name]=a.id;
}
void find(int i){
    if(books[i].id==0){cout<<"can't find this book"<<endl;return;}
    cout<<"<<"<<books[i].name<<">> has "<<books[i].num<<",the rest of this book is "<<books[i].res<<endl;
    cout<<"this book is on the NO."<<books[i].room<<" room, on "<<books[i].x<<"_row  "<<books[i].y<<"_column"<<endl;
}
void del(int i){
    map<int,book>::iterator it; 

    cout<<"you have deleted <<"<<books[i].name<<">>"<<endl;
    it=books.find(i);
    books.erase(it++);
}
void brow(int i)
{
    find(i);
    if(books[i].res>0){
    books[i].res--;
    cout<<"you have borrown the <<"<<books[i].name<<">>"<<endl; }
    else cout<<"there is no enough <<"<<books[i].name<<">> for you to borrow,so sorry;"<<endl;
}
void ret(int i){
    books[i].res++;
    cout<<"you have ruturned the <<"<<books[i].name<<">>"<<endl;
}

猜你喜欢

转载自blog.csdn.net/weixin_41313407/article/details/78934039
今日推荐