230 - Borrowers

本文介绍了一个简单的图书管理系统模拟程序,该程序使用 C++ 实现,并利用标准模板库(STL)中的 vector 和 map 完成图书信息的存储与检索。程序通过命令行接收输入,实现了书籍的借阅、归还及上架等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不一定真的插入删除才能完成模拟过程。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
struct book{
	string name,author;
	int tag;
	book(const string & n ="n",const string & a="a",int t = 1):name(n),author(a),tag(t){}
	bool operator<(const book & b)const{
		if(author != b.author)return author < b.author;
		return name < b.name;
	}
};
int main(){
	string s,name,author,cmd;
	vector<book> books;
	map<string,int> mmap;
	while(getline(cin,s) && s != "END"){
		name = s.substr(0,s.find_last_of('\"')+1);
		author = s.substr(s.find_last_of('\"')+1);
		books.push_back(book(name,author));
	}
	sort(books.begin(),books.end());
	for(int i=0;i<books.size();i++){
		mmap[books[i].name]=i;
	}
	while(cin>>cmd && cmd != "END"){
		if(cmd == "BORROW"){
			getchar();
			getline(cin,name);
			books[mmap[name]].tag = -1;
		}else if(cmd =="RETURN"){
			getchar();
			getline(cin,name);
			books[mmap[name]].tag = 0;
		}else if(cmd =="SHELVE"){
			for(int i=0;i<books.size();i++){
				if(books[i].tag == 0){
					int j;
					for(j=i-1;j>=0;j--){
						if(books[j].tag == 1)break;
					}
					if(j >= 0)printf("Put %s after %s\n",books[i].name.c_str(),books[j].name.c_str());
					else printf("Put %s first\n",books[i].name.c_str());
					books[i].tag = 1;
				}
			}
			cout<<"END\n";
		}
	}
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值