习题5-8 图书管理系统(Borrowers, UVa230)

来源:互联网 发布:网络教学软件平台 编辑:程序博客网 时间:2024/05/16 17:16
习题5-8 图书管理系统(Borrowers, UVa230)Before they are returned to the shelves, the returnedbooks are sorted by author and then title using the ASCII collating sequence.#include <iostream>#include <string>#include <vector>#include <stack>#include <queue>#include <deque>#include <set>#include <map>#include <algorithm>#include <sstream>#include <utility>#include <cstring>#include <cstdio>#include <cmath>#include <cctype>#define CLOSE() ios::sync_with_stdio(false)#define CLEAR(a, b) memset(a, b, sizeof(a))#define IN() freopen("in.txt", "r", stdin)#define OUT() freopen("out.txt", "w", stdout)const int maxn = 1e5 + 5;using LL = long long;using UI = unsigned int;using namespace std;//------------------------------------------------------------------------------------------//struct Book {string title, author;bool borrow, back, in_shelve = true;bool s;bool operator<(const Book &rhs) {if (author != rhs.author) return author < rhs.author;else return title < rhs.title;}}book[maxn];int cntb = 0;map<string, int> rnk;vector<string> t;void Read_Books() {string in, s;while (getline(cin, in)) {if (in == "END") break;stringstream ss(in);int flag = 1;while (ss >> s) {if (s == "by") break;if (flag) {flag = 0; s = s.substr(1);}else s = " " + s;if (s.back() == '"') s.pop_back();book[cntb].title += s;}flag = 1;while (ss >> s) {if (flag) flag = 0;else s = " " + s;book[cntb].author += s;}//cout << book[cntb].author << endl << book[cntb].title << endl;cntb++;}}bool Read_Records() {string s;int flag = 1;while (cin >> s) {//cout << s << endl;if (s == "SHELVE") return true;else if (s == "END") return false;if (s == "BORROW") {flag = 1;string t;while (cin >> s) {if (flag) {flag = 0;s = s.substr(1);}else s = " " + s;if (s.back() == '"') {s.pop_back();t += s;break;}t += s;}book[rnk[t]].borrow = true;book[rnk[t]].in_shelve = false;//cout << t << endl;}if (s == "RETURN") {flag = 1;string t;while (cin >> s) {if (flag) {flag = 0;s = s.substr(1);}else s = " " + s;if (s.back() == '"') {s.pop_back();t += s;break;}t += s;}book[rnk[t]].back = true;}}}void Solve() {t.clear();bool have_book = false;//cout << "books : " << endl;for (int i = 0; i < cntb; i++) {if (book[i].in_shelve || book[i].back) {t.push_back(book[i].title);//cout << book[i].title << endl;}if (book[i].in_shelve) have_book = true;}if (!t.empty() && book[rnk[t[0]]].back && (t[0] == book[0].title || !have_book)) {cout << "Put " << "\"" << t[0] << "\"" << " first\n";}for (int i = 1; i < t.size(); i++) {if (book[rnk[t[i]]].back) cout << "Put " << "\"" << t[i] << "\"" << " after " << "\"" << t[i - 1] << "\"\n";}for (int i = 0; i < t.size(); i++) {if (book[rnk[t[i]]].back) {book[rnk[t[i]]].back = false;book[rnk[t[i]]].borrow = false;book[rnk[t[i]]].in_shelve = true;}}cout << "END\n";}int main() {//IN(); OUT();Read_Books();sort(book, book + cntb);for (int i = 0; i < cntb; i++) rnk[book[i].title] = i;while (Read_Records()) Solve();return 0;}//看了大佬的代码的改进版,无论是清晰度还是代码长度都好太多了struct Book {string title, author;int status = 1;};vector<string> bvec;map<string, Book> bmap;bool cmp(const string &a, const string &b) {if (bmap[a].author != bmap[b].author) return bmap[a].author < bmap[b].author;else return bmap[a].title < bmap[b].title;}int main() {string s;Book b;//IN(); OUT();//cout << "Books :" << endl;while (getline(cin, s) && s != "END") {int p = s.find('"', 1);b.title = s.substr(0, p + 1);b.author = s.substr(p + 5);bvec.push_back(b.title);bmap[b.title] = b;}sort(bvec.begin(), bvec.end(), cmp);//for (int i = 0; i < bvec.size(); i++) cout << bvec[i] << endl;while (cin >> s && s != "END") {if (s == "SHELVE") {for (int i = 0; i < bvec.size(); i++)if (bmap[bvec[i]].status == -1) {int j;for (j = i; j >= 0; j--)if (bmap[bvec[j]].status == 1) break;if(j > -1) cout << "Put " << bvec[i] << " after " << bvec[j] << endl;else cout << "Put " << bvec[i] << " first" << endl;bmap[bvec[i]].status = 1;}cout << "END\n";}if (s == "BORROW") {getchar();getline(cin, s);//cout << "bo" << " : " << s << endl;bmap[s].status = 0;}if (s == "RETURN") {getchar();getline(cin, s);//cout << "re" << " : " << s << endl;bmap[s].status = -1;}}return 0;}/*期末考试结束前,暂时先停一下。做这题时感觉胃疼。。。好难受,,明明很水的题却做了有大半天。感觉我想题的过程还是不够完善,应该在敲题之前再好好想一想如何实现比较好再动手。中间出现了一些问题啊,比如自定义结构体不能用vector,sort传char * 出现问题等。感觉应该是因为自己定义的结构体不够完善,缺少很多操作的定义吧,还是用库函数的类型比较好。刚开始还选错了方法,直接开字符串数组写,麻烦多,问题也多,像这类不知道具体个数的题,直接用vector就好了。*/

原创粉丝点击