HDU1497(Simple Library Management System) 用类来实现

Problem Description

After AC all the hardest problems in the world , the ACboy 8006 now has nothing to do . One day he goes to an old library to find a part-time job .It is also a big library which has N books and M users.The user’s id is from 1 to M , and the book id is from 1 to N . According to the library rules , every user are only allowed to borrow 9 books .But what surprised him is that there is no computer in the library , and everything is just recorded in paper ! How terrible , I must be crazy after working some weeks , he thinks .So he wants to change the situation .

In the other hand , after 8006’s fans know it , they all collect money and buy many computers for the library .

Besides the hardware , the library needs a management program . Though it is just a piece of cake for 8006 , the library turns to you , a excellent programer,for help .

What they need is just a simple library management program . It is just a console program , and have only three commands : Borrow the book , Return the book , Query the user .

1.The Borrow command has two parameters : The user id and the book id
The format is : “B ui bi” (1<=ui<=M , 1<=bi<=N)
The program must first check the book bi wether it’s in the library . If it is not , just print “The book is not in the library now” in a line .
If it is , then check the user ui .
If the user has borrowed 9 books already, print “You are not allowed to borrow any more” .
Else let the user ui borrow the book , and print “Borrow success”.

2.The Return command only has one parameter : The book id
The format is : “R bi” (1<=bi<=N)
The program must first check the book bi whether it’s in the library . If it is , just print “The book is already in the library” . Otherwise , you can return the book , and print “Return success”.

3.The Query command has one parameter : The user id
The format is : “Q ui” (1<=ui<=M)
If the number of books which the user ui has borrowed is 0 ,just print “Empty” , otherwise print the books’ id which he borrows in increasing order in a line.Seperate two books with a blank.

Input

The input file contains a series of test cases . Please process to the end of file . The first line contains two integers M and N ( 1<= M <= 1000 , 1<=N<=100000),the second line contains a integer C means the number of commands(1<=C<=10000). Then it comes C lines . Each line is a command which is described above.You can assum all the books are in the library at the beginning of each cases.

Output

For each command , print the message which described above .
Please output a blank line after each test.
If you still have some questions , see the Sample .

Sample Input

5 10
9
R 1
B 1 5
B 1 2
Q 1
Q 2
R 5
Q 1
R 2
Q 1
5 10
9
R 1
B 1 5
B 1 2
Q 1
Q 2
R 5
Q 1
R 2
Q 1

Sample Output

The book is already in the library
Borrow success
Borrow success
2 5
Empty
Return success
2
Return success
Empty

The book is already in the library
Borrow success
Borrow success
2 5
Empty
Return success
2
Return success
Empty

思路:

题目要我们设计一个简单的图书管理系统,该题比较简单,正好可以作为C++中类的练手题。也可以用结构体写,类的写法代码可能会多一点。
注意每一个大循环结束后要输出一行空行分割。

#include <bits/stdc++.h>
#define mem(a,n) memset(a,n,sizeof(a))
using namespace std;

int book_list[100005];//保存借书人的id,0为未借出

class User
{
private:
    set<int> borrowed_book;
    int Id;
public:
    friend void init_user(int n);
    bool return_book(int book_id)
    {
        borrowed_book.erase(book_id);
        return 1;
    }
    bool borrow_book(int id)
    {
        if(!book_list[id])//该书未借出
        {
            if(borrowed_book.size()>=9)
            {
                cout<<"You are not allowed to borrow any more"<<endl;
                return 0;
            }
            book_list[id]=Id;
            borrowed_book.insert(id);
            cout<<"Borrow success"<<endl;
            return 1;
        }
        else
        {
            cout<<"The book is not in the library now"<<endl;
            return 0;
        }
    }
    void query_book()
    {
        if(borrowed_book.size()==0)
            cout<<"Empty"<<endl;
        else
        {
            set<int>::iterator it=borrowed_book.begin();
            cout<<*it;
                it++;
            while(it!=borrowed_book.end())
            {
                cout<<" "<<*it;
                it++;
            }
            cout<<endl;
        }
    }
}user[1005];

void init_user(int n)//初始化用户
{
    for(int i=1;i<=n;i++)
    {
        user[i].Id=i;
        user[i].borrowed_book.clear();
    }
}

int main()
{ios::sync_with_stdio(false);cin.tie(0);
    int m,n,k,u,b;
    char op;
    while(cin>>m>>n)
    {
        //每次循环都要初始化
        mem(book_list,0);
        init_user(m);
        
        cin>>k;
        while(k--)
        {
            cin>>op;
            if(op=='B')
            {
                cin>>u>>b;
                user[u].borrow_book(b);
            }
            else if(op=='R')
            {
                cin>>b;
                if(!book_list[b]) cout<<"The book is already in the library"<<endl;
                else
                {
                    user[book_list[b]].return_book(b);
                    book_list[b]=0;
                    cout<<"Return success"<<endl;
                }
            }
            else
            {
                cin>>u;
                user[u].query_book();
            }
        }
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值