-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMM_3.cpp
397 lines (376 loc) · 10.1 KB
/
MM_3.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <iostream>
#include <unordered_map>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
// Book structure
class Book {
public:
string title;
string author;
string genre;
float rating;
int pageCount;
Book() {}
Book(string title, string author, string genre, float rating, int pageCount)
{
this->title = title;
this->author = author;
this->genre = genre;
this->rating = rating;
this->pageCount = pageCount;
}
};
// Book Database class
class Database {
public:
unordered_map<string, Book> books;
void addBook(Book newBook);
Book getBook(const string& title);
void displayByGenre();
void displayByAuthor();
void searchBooksByGenre(const string& genre);
void searchBooksByAuthor(const string& author);
};
void Database::addBook(Book newBook)
{
books[newBook.title] = newBook;
}
Book Database::getBook(const string& title)
{
if (books.find(title) != books.end()) {
return books[title];
} else {
cout << "Book with title '" << title << "' not found." << endl;
return Book();
}
}
void Database::searchBooksByGenre(const string& genre)
{
cout << "Books in the genre '" << genre << "':" << endl;
for (const auto& pair : books) {
if (pair.second.genre == genre) {
cout << pair.second.title << " by " << pair.second.author << endl;
}
}
cout << endl;
}
void Database::searchBooksByAuthor(const string& author)
{
cout << "Books by the author '" << author << "':" << endl;
for (const auto& pair : books) {
if (pair.second.author == author) {
cout << pair.second.title << " in the genre " << pair.second.genre << endl;
}
}
cout << endl;
}
void Database::displayByGenre()
{
int ch=1;
while(ch!=0)
{
cout<<"Chose the genre you want to explore:"<<endl;
cout<<"1.Mystery"<<endl;;
cout<<"2.Romance"<<endl;
cout<<"3.Science Fiction"<<endl;
cout<<"4.Fantasy"<<endl;
cout<<"5.Horror"<<endl;
cout<<"6.Historical"<<endl;
cout<<"7.Thriller"<<endl;
cout<<"8.Classics"<<endl;
cout<<"9.Poetry"<<endl;
cout<<"10.Self Help"<<endl;
cout<<"0.Exit menu"<<endl;
cout<<"Enter choice: ";
cin>>ch;
switch (ch)
{
case 0:
break;
case 1:
{
searchBooksByGenre("Mystery");
break;
}
case 2:
{
searchBooksByGenre("Romance");
break;
}
case 3:
{
searchBooksByGenre("Science Fiction");
break;
}
case 4:
{
searchBooksByGenre("Fantasy");
break;
}
case 5:
{
searchBooksByGenre("Horror");
break;
}
case 6:
{
searchBooksByGenre("Historical");
break;
}
case 7:
{
searchBooksByGenre("Thriller");
break;
}
case 8:
{
searchBooksByGenre("Classics");
break;
}
case 9:
{
searchBooksByGenre("Poetry");
break;
}
case 10:
{
searchBooksByGenre("Self Help");
break;
}
default:
cout<<"Please enter a valid number";
break;
}
}
}
void Database::displayByAuthor()
{
int ch=1;
while(ch!=0)
{
cout<<"Chose the author whose book you want to explore:"<<endl;
cout<<"1. Agatha Christie"<<endl;;
cout<<"2. Ana Huang"<<endl;
cout<<"3. Dan Brown"<<endl;
cout<<"4. J K Rowling"<<endl;
cout<<"5. Stephen King"<<endl;
cout<<"6. Charles Dickens"<<endl;
cout<<"7. Paula Hawkins"<<endl;
cout<<"8. Jane Austen"<<endl;
cout<<"9. Sylvia Path"<<endl;
cout<<"10. Mark Manson"<<endl;
cout<<"0.Exit menu"<<endl;
cout<<"Enter choice: ";
cin>>ch;
switch (ch)
{
case 0:
break;
case 1:
{
searchBooksByAuthor("Agatha Christie");
break;
}
case 2:
{
searchBooksByAuthor("Nora Roberts");
break;
}
case 3:
{
searchBooksByAuthor("Dan Brown");
break;
}
case 4:
{
searchBooksByAuthor("J K Rowling");
break;
}
case 5:
{
searchBooksByAuthor("Stephen King");
break;
}
case 6:
{
searchBooksByAuthor("Charles Dickens");
break;
}
case 7:
{
searchBooksByAuthor("Paula Hawkins");
break;
}
case 8:
{
searchBooksByAuthor("Jane Austen");
break;
}
case 9:
{
searchBooksByAuthor("Sylvia Path");
break;
}
case 10:
{
searchBooksByAuthor("Leo Tolstoy");
break;
}
default:
cout<<"Please enter a valid number";
break;
}
}
}
class ReadingListNode
{
public:
string ReadingListName;
vector <string> bookNames;
ReadingListNode *left;
ReadingListNode *right;
ReadingListNode(){}
ReadingListNode(string rl_name)
{
ReadingListName=rl_name;
left=right=NULL;
}
};
class ReadingListTree
{
private:
ReadingListNode *root;
public:
ReadingListTree()
{
root = NULL;
}
void insertReadingList(ReadingListNode*& root, string rl_name);
void printLevelOrder(ReadingListNode* root);
void addBookIntoRL(string bookName, string rl_name);
void removeBookFromRL(string bookName, string rl_name);
void getReadingList(ReadingListNode*& root, string rl_name);
};
// Function to add new ReadingListName in level order
void ReadingListTree :: insertReadingList(ReadingListNode*& root, string rl_name)
{
ReadingListNode* temp = new ReadingListNode(rl_name); // Initialize with ReadingListName
temp->ReadingListName = rl_name;
temp->left = temp->right = NULL;
if (!root)
root = temp;
else {
queue<ReadingListNode*> q;
q.push(root);
while (!q.empty()) {
ReadingListNode* temp = q.front();
q.pop();
if (!temp->left)
{
temp->left = new ReadingListNode;
temp->left->ReadingListName = rl_name;
temp->left->left = temp->left->right = NULL;
break;
}
else
q.push(temp->left);
if (!temp->right)
{
temp->right = new ReadingListNode;
temp->right->ReadingListName = rl_name;
temp->right->left = temp->right->right = NULL;
break;
}
else
q.push(temp->right);
}
}
}
// Function to print Level Order Traversal
void ReadingListTree :: printLevelOrder(ReadingListNode* root) {
if (root == NULL) return;
queue<ReadingListNode*> q;
q.push(root);
while (!q.empty()) {
ReadingListNode* node = q.front();
cout << node->ReadingListName << " ";
q.pop();
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right);
}
}
void ReadingListTree :: addBookIntoRL(string bookName, string rl_name)
{
if (root == NULL) return;
queue<ReadingListNode*> q;
q.push(root);
while (!q.empty()) {
ReadingListNode* node = q.front();
if (node->ReadingListName == rl_name)
{
node->bookNames.push_back(bookName);
break;
}
q.pop();
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right);
}
}
void ReadingListTree :: removeBookFromRL(string bookName, string rl_name)
{
if (root == NULL)
return;
queue<ReadingListNode*> q;
q.push(root);
while (!q.empty()) {
ReadingListNode* node = q.front();
if (node->ReadingListName == rl_name)
{
auto it = find(node->bookNames.begin(), node->bookNames.end(),
bookName);
if (it != node->bookNames.end()) {
node->bookNames.erase(it);
}
break;
}
q.pop();
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right);
}
}
void ReadingListTree :: getReadingList(ReadingListNode*& root, string rl_name) {
{
if (root == NULL) return;
queue<ReadingListNode*> q;
q.push(root);
while (!q.empty())
{
ReadingListNode* node = q.front();
if (node->ReadingListName == rl_name)
{
int n = node->bookNames.size();
for(int i = 0; i < n; i++)
{
cout << node->bookNames[i] << endl;
}
break;
}
q.pop();
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right);
}
}
int main()
{
return 0;
}