1022.Digital Library

【题意】
        给出一连串书的信息和针对书的内容的查询要求,返回查询结果

【思路】
        按题意码下来就过了

【注意点】
先cin了再getline()的话记得要用getchar()把前一个回车读掉

#include 
#include 
#include 
#include 
#include 
using namespace std;

#define MAXN 10000

typedef struct{
	string id;
	string title;
	string author;
	vector keywords;
	string publisher;
	string year;
}book;

bool cmp(book a, book b){
	return a.id books;
	string allKeyword,keyword;

	cin >> n;
	for(int i=0; i> tmpBook.id;
		getchar();
		getline(cin,tmpBook.title,'\n');
		getline(cin,tmpBook.author,'\n');
		getline(cin,allKeyword,'\n');
		int start=0;
		for(int j=0; j<=allKeyword.length(); j++){
			if(j==allKeyword.length() || allKeyword[j]==' '){
				keyword.assign(allKeyword,start,j-start);
				start = j+1;
				tmpBook.keywords.push_back(keyword);
			}
		}
		getline(cin,tmpBook.publisher,'\n');
		getline(cin,tmpBook.year,'\n');
		books.push_back(tmpBook);
	}

	sort(books.begin(),books.end(),cmp);

	cin >> m;
	getchar();
	string query,information;
	for(int i=0; i::iterator it = books.begin();
		int cnt = 0;
		getline(cin,query,'\n');
		information.assign(query,3,query.length()-3);

		cout << query << endl;

		switch(query[0]){
		case '1':
			while(it!=books.end()){
				if((*it).title==information){
					cout << (*it).id << endl;
					cnt++;
				}
				it++;
			}
			break;
		case '2':
			while(it!=books.end()){
				if((*it).author==information){
					cout << (*it).id << endl;
					cnt++;
				}
				it++;
			}
			break;
		case '3':
			while(it!=books.end()){
				vector::iterator subit = (*it).keywords.begin();
				while(subit!=(*it).keywords.end()){
					if((*subit)==information){
						cout << (*it).id << endl;
						cnt++;
						break;
					}
					subit++;
				}
				it++;
			}
			break;
		case '4':
			while(it!=books.end()){
				if((*it).publisher==information){
					cout << (*it).id << endl;
					cnt++;
				}
				it++;
			}
			break;
		case '5':
			while(it!=books.end()){
				if((*it).year==information){
					cout << (*it).id << endl;
					cnt++;
				}
				it++;
			}
			break;
		default:
			break;
		}
		if(!cnt){
			cout << "Not Found" << endl;
		}
	}

	system("pause");
	return 0;
}


你可能感兴趣的:(PAT(Advanced,Level))