C++关联容器之小小程序

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
	//第一步:创造一个存储书本的容器
	vector<string>strVecBook;
	string bName;
	cout << "Input book's name you wanna read(ctrl + z to end): " << endl;
	while (cin >> bName)
	{
		strVecBook.push_back(bName);
	}
	size_t NumOfBook = strVecBook.size();
	//完成对存储书本容器初始化;
	//第二步:创造一个set记录已经看过的书名
	set<string>strSetReadedBook;
	string strChioce, bookName;
	bool _6MonthLater = false;
	//完成初始化
	//第三步:取系统任意时间为种子,判断读者目前处于读书的哪个阶段
	srand((unsigned)time(NULL));
	while (!_6MonthLater && !strVecBook.empty())//不是六个月而且非空
	{
		cin.clear();
		cout << "Do you wanna read a book: " << endl;
		cin >> strChioce;
		if (strChioce[0] == 'Y' || strChioce[0] == 'y')
		{
			int i = rand() % strVecBook.size();//任给一本书
			bookName = strVecBook[i];
			cout << "We choose this book for you: " << bookName << endl;
			strSetReadedBook.insert(bookName);//导入读的这本书
			strVecBook.erase(strVecBook.begin() + i);//拿出来了就要删除
			cout << "One months later, Did you read this book:" << endl;
			cin >> strChioce;
			if (strChioce[0] == 'N' || strChioce[0] == 'n')//放在一边没有读这本书
			{
				strSetReadedBook.erase(bookName);
				strVecBook.push_back(bookName);//从已读书目的set中删除该书的记录

			}

		}
		//6个月后,到了吗?
		cout << "6 month later?(yes or no):";
		cin >> strChioce;
		if (strChioce[0] == 'Y' || strChioce[0] == 'y')
		{
			_6MonthLater = true;//总算是可以跳出小循环了
		}
	}
	//跳出循环你该干嘛?
	//报告读的书和没有读的书
	if (_6MonthLater)//6个月得报告了
	{
		if (!strSetReadedBook.empty())
		{
			cout << "These books you have read: " << endl;
			for (set<string>::iterator iter = strSetReadedBook.begin(); iter != strSetReadedBook.end(); ++iter)
			{
				cout << *iter << " ";
			}
		}
		cout << endl;
		if (!strVecBook.empty())
		{
			cout << "These books you have no read: " << endl;
			for (vector<string>::iterator iter = strVecBook.begin(); iter != strVecBook.end(); ++iter)
			{
				cout << *iter << " ";
			}
		}
		cout << endl;
	}
	if (strSetReadedBook.size() == NumOfBook)
	{
		cout << "Congratulations! You have read all these books!" << endl;
	}
	//总算是完工了
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值