UVA - 230 Borrowers string+vector

题目大意:给出n本书,每本书都有相应的title和author,先按照author的大小排序,然后按title的大小排序
现在给出三种操作
BORROW title:借出书名为tile的书
RETURN title:还书,书名为title
SHELVE:把归还的书放回书架,并输出title和放的位置

解题思路:用一个结构体纪录每本书的状态,然后用一个动态数组存储书,具体看代码,对string这stl不太懂,就参考了别人的代码了。。。

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

struct Book{
    string author, title;
    bool borrowed, returned;
    Book(string a, string b) {
        title = a;
        author = b;
        borrowed = returned = false;
    }
};

bool cmpa(Book a, Book b) {
    return a.author < b.author;
}

bool cmpb(Book a, Book b) {
    return a.title < b.title;
}

vector<Book>  All;
string cmd, in, req;

void shelve() {
    int j;
    for(int i = 0; i < All.size(); i++) 
        if(All[i].returned == true) {
            for(j = i; j >= 0; j--)
                if(All[j].borrowed == false)
                    break;
            if(j == -1)
                printf("Put %s first\n", All[i].title.c_str());
            else
                printf("Put %s after %s\n",All[i].title.c_str(), All[j].title.c_str());

            All[i].borrowed = All[i].returned = false;
        }
    cout << "END\n";
}

void borrow() {
    getline(cin,req);

    for(int i = 0; i < All.size(); i++)
        if(All[i].title == req) {
            All[i].borrowed = true;
            return ;
        }
}

void back() {
    getline(cin,req);

    for(int i = 0; i < All.size(); i++)
        if(All[i].title == req) {
            All[i].returned = true;
            return ;
        }
}

int main() {
    while(getline(cin,in) && in != "END") 
        All.push_back(Book(in.substr(0,in.find_last_of("\"") + 1),in.substr(in.find_last_of("\"") + 1) ) );
    stable_sort(All.begin(),All.end(),cmpb);
    stable_sort(All.begin(),All.end(),cmpa);

    while(cin >> cmd) 
        if(cmd == "BORROW")
            cin.get(), borrow();
        else if(cmd == "RETURN")
            cin.get(), back();
        else if(cmd == "SHELVE")
            cin.get(), shelve();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,作为一名语言模型,我无法进行图形绘制。但是我可以为您提供一些关于图书管理系统类图绘制的指导。 在绘制图书管理系统的类图时,您需要考虑系统中的主要对象和它们之间的关系。一般来说,一个图书管理系统中的主要对象包括书籍、读者、图书管理员、借阅记录等。这些对象之间的关系可能包括继承、关联、聚合和组合等。 以下是一个简单的图书管理系统类图示例,希望能对您有所帮助: ``` +---------------------+ +---------------------+ | Book | | Reader | +---------------------+ +---------------------+ | - title : string | | - name : string | | - author : string | | - id : string | | - ISBN : string | | | | - publication_date:| | | | string | | | +---------------------+ +---------------------+ | | | | | | +---------------------+ +---------------------+ | BorrowRecord | | Library | +---------------------+ +---------------------+ | - book : Book | | - books : Book[] | | - reader : Reader | | - borrowers : | | - borrow_date : | | BorrowRecord[] | | string | +---------------------+ | - return_date : | | string | +---------------------+ ``` 在这个示例中,Book、Reader、BorrowRecord和Library都是类,每个类都有一些属性和方法。例如,Book类有标题、作者、ISBN和出版日期等属性,而Reader类有姓名和ID等属性。BorrowRecord类则包含了借阅日期和归还日期等属性,同时包含了一个Book对象和一个Reader对象。 Library类则包含了一些Book对象和BorrowRecord对象,以及一些方法来管理这些对象。在这个示例中,Book和Reader之间是关联关系,BorrowRecord和Book、Reader之间则是聚合关系。 当然,这只是一个简单的示例,实际上您需要根据具体的需求来设计您的类图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值