#include <iostream>
#include <string>
#include <set>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
/*
* 定义一个vector容器,存储你在未来六个月里要阅读的书,再定义一个set,用于记录你已经看过的书名。
* 编写程序从vector中为你选择一本没有读过而现在要读的书。当它为你返回选中的书名后,应该讲该书名放入记录已读书目的set中。
* 如果实际上你把这本书放在一边没有看,则本程序应该支持从已读书目的set中删除该书的记录。
* 在虚拟的六个月后,输出已读书目和还没有读的书目。
*
*/
vector<string> books;
set<string> hasRead;
string name;
cout << "Enter names for books you'd like to read(ctrl+Z to end)" << endl;
while (cin >> name)
{
books.push_back(name);
}
cin.clear();
bool timeOver = false;
string answer, bookName;
//设置种子
srand(time(0));
while (!timeOver && !books.empty())
{
cout << "Would you like to read a book?(Yes/NO)" << endl;
cin >> answer;
if (answer[0]=='y'||answer[0]=='Y')
{
int i = rand() % books.size();
//要读的书名
bookName = books[i];
cout << "You can read this book: " << bookName << endl;
hasRead.insert(bookName);
//从书架上删除已读的书
books.erase(books.begin() + i);
cout << "Did you read it?(Yes/No)" << endl;
cin >> answer;
//如果没有读这本书,将其放回书架
if (answer[0] == 'n' || answer[0] == 'N')
{
hasRead.erase(bookName);
books.push_back(bookName);
}
}
cout << "Time over?(Yes/No)" << endl;
cin >> answer;
if (answer[0]=='y'||answer[0]=='Y')
{
timeOver = true;
}
}
if (timeOver)
{
cout << "books read: " << endl;
for (set<string>::iterator iter = hasRead.begin(); iter != hasRead.end(); iter++)
{
cout << *iter << endl;
}
cout << "books not read:" << endl;
for (vector<string>::iterator iter = books.begin(); iter != books.end(); iter++)
{
cout << *iter << endl;
}
}
//如果时间没有超时,那么书被全部读完
else
{
cout << "Congratulations! You have read all these books." << endl;
}
system("pause");
return 0;
}
读书模拟
最新推荐文章于 2024-07-30 09:13:15 发布