pat 1022. Digital Library (字符串水题)

本文介绍了一种使用C++实现的字符串处理方法,通过结构体存储数据并利用字符串流进行关键字搜索。文章详细展示了如何读取和处理输入,并根据不同条件查询特定记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字符串处理题

感觉考点在各种输入函数

用字符串流处理key words比较方便

如果在vc中编程,可能因为getline()的问题遇到一些调试上的不爽


#include<iostream>
#include<sstream>
#include<string>
#include<algorithm>
using namespace std;
const int MAX=10005;

struct node
{
	string id;
	string title;
	string author;
	int num;
	string words[5];
    string pub;
	string year;
};
node arr[MAX];

int comfun(node a,node b)
{
	return a.id<b.id;
}

int main()
{
    int n,m,i,j,k,l;
	
	scanf("%d",&n);
	getchar();  
	
	string s,sp;
    for(i=0;i<n;i++)
	{
		getline(cin,arr[i].id);	
		getline(cin,arr[i].title);	
		getline(cin,arr[i].author);
		getline(cin,s);
		istringstream cin2(s); 
		l=0;
		while(cin2>>sp) 
		{
			arr[i].words[l++]=sp;  
		}
		arr[i].num=l;
		
		getline(cin,arr[i].pub);	
		getline(cin,arr[i].year);		
	}
	
	sort(arr,arr+n,comfun);
 
	scanf("%d",&m);
	getchar();  
	string ss;
	for(i=0;i<m;i++)
	{
		getline(cin,s);
			int len=s.size()-3;
		ss=s.substr(3,len);
		cout<<s<<endl;
		int count=0;
		if(s[0]=='1')
		{
			for(j=0;j<n;j++)
				if(arr[j].title==ss)
				{
					cout<<arr[j].id<<endl;
					count++;
				}
            if(!count)
				cout<<"Not Found"<<endl;
		}
		if(s[0]=='2')
		{
			for(j=0;j<n;j++)
				if(arr[j].author==ss)
				{	
					cout<<arr[j].id<<endl;
					count++;
				}
			if(!count)
				cout<<"Not Found"<<endl;
		}
		if(s[0]=='3')
		{
			for(j=0;j<n;j++)
			for(l=0;l<arr[j].num;l++)
				if(arr[j].words[l]==ss)
				{	
					cout<<arr[j].id<<endl;
					count++;
					break;
				}
			if(!count)
				cout<<"Not Found"<<endl;
		}
		if(s[0]=='4')
		{
			for(j=0;j<n;j++)
				if(arr[j].pub==ss)
				{
					cout<<arr[j].id<<endl;
					count++;
				}
			if(!count)
				cout<<"Not Found"<<endl;
		}
		if(s[0]=='5')
		{
			for(j=0;j<n;j++)
				if(arr[j].year==ss)
				{
					cout<<arr[j].id<<endl;
					count++;
				}
			if(!count)
				cout<<"Not Found"<<endl;
		}
	}
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值