读书模拟

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值