这是uva230
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;
struct book
{
string author;
int status;
};
map<string, book> books;//这里面还有用到map需要学习一下,map的类型可以很多,有结构体,数组,string,int double
vector<string> name;//一个数组的每一个元素都是string类型的
bool compare(string a, string b)
{
if(books[a].author == books[b].author) return a < b;
else return books[a].author < books[b].author;
}
int main()
{
string x,z,m;
book y;
while(getline(cin,m))
{
if(m == "END") break;
x = m.substr(0, m.find_last_of("\"")+1);
y.author = m.substr(m.find_last_of("\"")+1);这是两个查找函数,可以参考一下
name.push_back(x);//这是一个插入的函数
books[x]= y;
}
sort(name.begin(), name.end(), compare);//自己编写一个sort函数这个比较函数是对string内的元素进行排序
for(int i = 0; i < name.size(); i++)
books[name[i]].status = 1; //表示状态
while(cin >> x)
{
if(x == "END") break;
if(x == "BORROW")
{
getchar();
getline(cin, z);
books[z].status = 0;
}
if(x == "RETURN")
{
getchar();
getline(cin, z);
books[z].status = -1;
}
if(x == "SHELVE")
{
for(int i = 0; i < name.size(); i++)
if(books[name[i]].status == -1)
{
int j;
for(j = i; j >= 0; --j)
if(books[name[j]].status == 1) break;
if(j > -1) cout << "Put " << name[i] << " after " << name[j] << endl;
else cout << "Put " << name[i] << " first" << endl;
books[name[i]].status = 1;
}
cout << "END" << endl;
}
}
return 0;
}