这道题目是考察stl模板库中map的使用,使用map<string,int>类型保存每本图书的状态,根据不同输入指令实现状态变换即可。另外需要注意的就是对于各种字符串的处理,需要灵活使用各种string类函数。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define LEN 90
#define MAXL 1010
struct Book{
string name;
string author;
friend bool operator < (Book a,Book b){
if(a.author < b.author) return true;
else if(a.author > b.author) return false;
else{
if(a.name < b.name) return true;
else return false;
}
}
}book[MAXL];
map<string,int> value;
int main()
{
// freopen("input.txt","r",stdin);
string a,b;
int n=0,i,j,pos;
while(getline(cin,a)&&a != "END"){
pos = a.find('\"',1);
book[n].name = a.substr(1,pos);
pos += 6;
book[n].author = a.substr(pos-1,a.length() - pos + 1);
value[book[n].name] = 1;
n++;
}
sort(book,book + n);
while(cin >> a&&a != "END"){
if(a == "BORROW"){
getchar();
getchar();
getline(cin,b);
value[b] = 0;
}
else if(a == "RETURN"){
getchar();
getchar();
getline(cin,b);
value[b] = 2;
}
else if(a == "SHELVE"){
for(i = 0 ; i < n ; i++)
{
if(value[book[i].name] == 2){
for(j = i-1 ; j >= 0 ; j--)//注意最后有可能j=-1
{
if(value[book[j].name] == 1) break;
}
cout<<"Put \""<<book[i].name<<' ';
if(j != -1) cout<<"after \""<<book[j].name<<'\n';
else cout<<"first"<<'\n';
value[book[i].name] = 1;
}
}
cout<<"END"<<'\n';
}
}
return 0;
}