欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PTA甲级考试真题练习22——1022 Digital Library

程序员文章站 2022-06-07 13:14:26
...

题目

PTA甲级考试真题练习22——1022 Digital Library

思路

水题

代码

#include <iostream>
#include <string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<list>
#include<sstream>
using namespace std;
#define nmax 100
#define inf 999999

int booknum,quertnum;
typedef struct book
{
	int ID;
	string title;
	string author;
	vector<string> key;
	string publisher;
	string publish_year;
	bool operator < (const book& a) const
	{
		return ID < a.ID;
	}
}book;
set<book> library;
int Type;
string querywords;
vector<string> ans;

int main()
{
	cin >> booknum;
	for (int i = 0; i < booknum; ++i)
	{
        book tmp;
		string keyArray;
		cin >> tmp.ID;
		//防止下一行输入捕获到回车
		cin.ignore();
		getline(cin, tmp.title);
		getline(cin, tmp.author);
		string key;
		while (cin >> key)
		{
			tmp.key.push_back(key);
			char c = getchar();
			if (c == '\n')
				break;
		}
		getline(cin, tmp.publisher);
		getline(cin, tmp.publish_year);
		library.insert(tmp);
	}
	cin >> quertnum;
	for (int i = 0; i < quertnum; ++i)
	{
		stringstream s;
		scanf("%d: ", &Type);
		getline(cin, querywords);
		char tmp_c[100] = "";
		if (Type == 5)
		{
			sprintf(tmp_c, "%d: %04d",Type,atoi(querywords.c_str()));
			ans.push_back(tmp_c);
		}
		else
		{
			s << Type << ": " << querywords;
			ans.push_back(s.str());
		}
		bool isFound = false;
		switch (Type)
		{
		case 1:
			for (auto& p : library)
			{
				if (p.title == querywords)
				{
					isFound = true;
					sprintf(tmp_c,"%07d",p.ID);
					ans.push_back(tmp_c);
				}
			}
			if (!isFound)
				ans.push_back("Not Found");
			break;
		case 2:
			for (auto& p : library)
			{
				if (p.author == querywords)
				{
					isFound = true;
					sprintf(tmp_c, "%07d", p.ID);
					ans.push_back(tmp_c);
				}
			}
			if (!isFound)
				ans.push_back("Not Found");
			break;
		case 3:
			for (auto& p : library)
			{
				for (auto& keywords : p.key)
				{
					if (keywords == querywords)
					{
						isFound = true;
						sprintf(tmp_c, "%07d", p.ID);
						ans.push_back(tmp_c);
						break;
					}
				}
			}
			if (!isFound)
				ans.push_back("Not Found");
			break;
		case 4:
			for (auto& p : library)
			{
				if (p.publisher == querywords)
				{
					isFound = true;
					sprintf(tmp_c, "%07d", p.ID);
					ans.push_back(tmp_c);
				}
			}
			if (!isFound)
				ans.push_back("Not Found");
			break;
		case 5:
			for (auto& p : library)
			{
				if (p.publish_year == querywords)
				{
					isFound = true;
					sprintf(tmp_c, "%07d", p.ID);
					ans.push_back(tmp_c);
				}
			}
			if (!isFound)
				ans.push_back("Not Found");
			break;
		default:
			break;
		}
	}
    int i;
    for(i = 0;i<ans.size()-1;++i)
    {
        cout<<ans[i]<<endl;
    }
    cout<<ans[i];
	return 0;
}