-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ch10_Sec4_setBook.cpp
63 lines (63 loc) · 2.07 KB
/
Ch10_Sec4_setBook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// C++ Primer 4th Edition Chapter 10 Exercises Section 10.4.2 Exercise 10.25
#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
vector<string> books;
set<string> readedBooks;
string name;
// Generate Booklist
cout << "Enter names for books you'd like to read (Ctrl+Z to quit)" << endl;
while ( cin >> name )
books.push_back( name );
cin.clear();
bool timeOver = false;
string answer, bookName;
// Set seed for random generator
srand( (unsigned) time(NULL) );
// Simulation for months gone
while ( !timeOver && !books.empty() )
{
cout << "Would you like to read a book? (Y/N)" << endl;
cin >> answer;
if ( answer[0] == 'y' || answer[0] == 'Y' )
{
int i = rand() % books.size(); // Generate a pseudo random number
bookName = books[i];
cout << "You can read this book (radom pick): " << bookName << endl;
readedBooks.insert( bookName );
books.erase( books.begin() + i );
// Read Confirmation
cout << "Did you read it? (Y/N)" << endl;
cin >> answer;
if ( answer[0] == 'n' || answer[0] == 'N' ) // The book is not read
{
readedBooks.erase( bookName );
books.push_back( bookName );
}
}
cout << "Time Over? (Y/N)" << endl;
cin >> answer;
if ( answer[0] == 'y' || answer[0] == 'Y' )
{
timeOver = true; // Simulation for virtual 6 months gone
}
}
if ( timeOver )
{
cout << "books read: " << endl;
for ( set<string>::iterator sit = readedBooks.begin(); sit != readedBooks.end(); ++sit )
cout << *sit << endl;
cout << "books not read: " << endl;
for ( vector<string>::iterator vit = books.begin(); vit != books.end(); ++vit )
cout << *vit << endl;
}
else
cout << "Congratulations! You have read all these books." << endl;
return 0;
}