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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值