UVA-230 图书管理系统

这道题目是考察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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值