当前位置: 代码迷 >> 综合 >> pat 1022. Digital Library
  详细解决方案

pat 1022. Digital Library

热度:14   发布时间:2024-01-14 23:17:48.0

水题,熟悉一下string和cin的getline方法

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>using namespace std;string id[10005],title[10005],author[10005],keywords[10005],publisher[10005],year[10005];int main()
{freopen("F:/CppProject/data/1022.txt","r",stdin);int n=0,m=0;cin>>n;cin.get();char bid[100],btitle[100],bauthor[100],bkeywords[100],bpublisher[100],byear[100];for(int i=0;i<n;i++){cin.getline(bid,sizeof(bid));cin.getline(btitle,sizeof(btitle));cin.getline(bauthor,sizeof(bauthor));cin.getline(bkeywords,sizeof(bkeywords));cin.getline(bpublisher,sizeof(bpublisher));cin.getline(byear,sizeof(byear));id[i]=bid;title[i]=btitle;author[i]=bauthor;keywords[i]=bkeywords;publisher[i]=bpublisher;year[i]=byear;//cout<<id[i]<<"---"<<title[i]<<"---"<<author[i]<<"---"<<keywords[i]<<"---"<<publisher[i]<<"---"<<year[i]<<"---"<<endl;}cin>>m;cin.get();char query[100];for(int i=0;i<m;i++){cin.getline(query,sizeof(query));string str = query;cout<<str<<endl;str = str.substr(3,str.length()-1);vector<string> vec;int type=query[0]-'0';int npos=str.find("a@fdsa");//npos is the return value of sub string not foundfor(int j=0;j<n;j++){switch (type){case 1:if(title[j]==str)vec.push_back(id[j]);break;case 2:if(author[j]==str)vec.push_back(id[j]);break;case 3:if(keywords[j].find(str)!=npos)vec.push_back(id[j]);break;case 4:if(publisher[j]==str)vec.push_back(id[j]);break;case 5:if(year[j]==str)vec.push_back(id[j]);break;}}if(vec.size()==0){cout<<"Not Found"<<endl;}else{sort(vec.begin(),vec.end());for(int j=0;j<vec.size();j++)cout<<vec[j]<<endl;}}return 0;
}


  相关解决方案