1022 Digital Library (30 分) 注:vector+结构体+sort排序
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp (struct bookk,struct bookk);
struct bookk{
int id;
string title;
string author;
vector <string> str;
string publisher;
string year;
};
int main ()
{
int n,i,j,type,flag,z,m;
char c;
string sst,s;
scanf ("%d",&n);
struct bookk book[n];
for (i=0;i<n;i++)
{
j=0;
scanf ("%d",&book[i].id);
getchar();
getline (cin,book[i].title);
getline (cin,book[i].author);
while (1)
{
cin >> s;
book[i].str.push_back(s);
c=getchar();
if (c=='\n')
break;
}
getline (cin,book[i].publisher);
getline (cin,book[i].year);
}
sort (book,book+n,cmp);
scanf ("%d",&m);
for (i=0;i<m;i++)
{
scanf ("%d: ",&type);
getline (cin,sst);
cout<< type <<": "<<sst <<endl;
if (type==1)
{
flag=0;
for (j=0;j<n;j++)
{
if (book[j].title==sst)
{
flag=1;
printf("%07d\n",book[j].id);
}
}
if (flag==0)
cout << "Not Found" <<endl;
}
else if (type==2)
{
flag=0;
for (j=0;j<n;j++)
{
if (book[j].author==sst)
{
flag=1;
printf("%07d\n",book[j].id);
}
}
if (flag==0)
cout << "Not Found"<<endl;
}
else if (type==3)
{
flag=0;
for (j=0;j<n;j++)
{
for (z=0;z<book[j].str.size();z++)
if (book[j].str[z]==sst)
{
flag=1;
printf("%07d\n",book[j].id);
break;
}
}
if (flag==0)
cout <<"Not Found" <<endl;
}
else if (type==4)
{
flag=0;
for (j=0;j<n;j++)
{
if (book[j].publisher==sst)
{
flag=1;
printf("%07d\n",book[j].id);
}
}
if (flag==0)
cout << "Not Found" <<endl;
}
else
{
flag=0;
for (j=0;j<n;j++)
{
if (book[j].year==sst)
{
flag=1;
printf("%07d\n",book[j].id);
}
}
if (flag==0)
cout << "Not Found"<<endl;
}
}
return 0;
}
bool cmp (struct bookk a,struct bookk b)
{
return a.id<b.id;
}
|