I mean your borrowers of books--those mutilators of collections, spoilers of the symmetry of shelves, and creators of odd volumes. --Charles Lamb, Essays of Elia (1823) 'The Two Races of Men'
Like Mr. Lamb, librarians have their problems with borrowers too. People don't put books back where they should. Instead, returned books are kept at the main desk until a librarian is free to replace them in the right places on the shelves. Even for librarians, putting the right book in the right place can be very time-consuming. But since many libraries are now computerized, you can write a program to help.
When a borrower takes out or returns a book, the computer keeps a record of the title. Periodically, the librarians will ask your program for a list of books that have been returned so the books can be returned to their correct places on the shelves. Before they are returned to the shelves, the returned books are sorted by author and then title using the ASCII collating sequence. Your program should output the list of returned books in the same order as they should appear on the shelves. For each book, your program should tell the librarian which book (including those previously shelved) is already on the shelf before which the returned book should go.
Input
Output
Sample Input
"The Canterbury Tales" by Chaucer, G. "Algorithms" by Sedgewick, R. "The C Programming Language" by Kernighan, B. and Ritchie, D. END BORROW "Algorithms" BORROW "The C Programming Language" RETURN "Algorithms" RETURN "The C Programming Language" SHELVE END
Sample Output
Put "The C Programming Language" after "The Canterbury Tales" Put "Algorithms" after "The C Programming Language" END
真是一看到各种麻烦的字符串的输入输出就不敢做了,真的对字符串好不熟悉。
这个题感觉数据的处理确实比较麻烦
以下为摘来的代码
#include <algorithm> #include <cstdio> #include <iostream> #include <string> #include <string.h> #include <vector> using namespace std; struct book{ string title, author; bool borrowed, returned; book(string a,string b){ title = a; author = b; borrowed = returned = false; } }; vector<book> allbook; string command,request,ss; bool cmpa(book a,book b) { return (a.author < b.author); } bool cmpt(book a,book b) { return (a.title < b.title); } void shelve() { int j; for(int i = 0; i < allbook.size();i++){ if(allbook[i].returned == true){ for(j = i; j >= 0;j--){ if(allbook[j].borrowed == false)break; } if(j == -1)printf("Put %s first ", allbook[i].title.c_str()); //之前的所有书都被借走了 else printf("Put %s after %s ",allbook[i].title.c_str(), allbook[j].title.c_str()); allbook[i].borrowed = allbook[i].returned = false; } } cout << "END "; } void borrow() { getline(cin, request); for(int i = 0;i < allbook.size(); i++){ if(allbook[i].title == request){ allbook[i].borrowed = true; return; } } } void back() { getline(cin, request); for(int i = 0; i < allbook.size(); i++ ) { if(allbook[i].title == request){ allbook[i].returned = true; return; } } } int main() { while(getline(cin,ss)&&ss!="END"){ allbook.push_back(book(ss.substr( 0, ss.find_last_of( """ ) + 1 ), ss.substr(ss.find_last_of( """ ) + 1 ))); } stable_sort(allbook.begin(), allbook.end(), cmpt); stable_sort(allbook.begin(), allbook.end(), cmpa); while(cin >> command){ if( command == "BORROW" ) cin.get(), borrow(); else if( command == "RETURN" ) cin.get(), back(); else if( command == "SHELVE" ) cin.get(), shelve(); } //system("pause"); return 0; }
把C++中的string 转化为C中的%s输出,要用c_str() ,否则输出的是一堆乱码
看完后觉得操作并不是很难,然而思路和对字符串的处理都值得膜拜!