#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;
}
C++关联容器之小小程序
最新推荐文章于 2024-06-24 10:11:54 发布