-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOOKratings.cpp
More file actions
81 lines (68 loc) · 1.79 KB
/
BOOKratings.cpp
File metadata and controls
81 lines (68 loc) · 1.79 KB
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
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
class Book {
public:
int id;
string title;
string genre;
};
class User {
public:
int id;
string name;
map<int, int> ratings; // Book ID to rating mapping
};
vector<Book> books;
vector<User> users;
void loadBooks() {
// Load book data from a file or database
// Fill the 'books' vector with Book objects
}
void loadUsers() {
// Load user data from a file or database
// Fill the 'users' vector with User objects
}
double predictRating(User& user, Book& book) {
// Simplified prediction function
// Calculate the average user rating and return it as the predicted rating
int totalRatings = 0;
for (const auto& rating : user.ratings) {
totalRatings += rating.second;
}
double averageRating = totalRatings / static_cast<double>(user.ratings.size());
return averageRating;
}
int main() {
loadBooks();
loadUsers();
int userId, bookId;
cout << "Enter user ID: ";
cin >> userId;
cout << "Enter book ID: ";
cin >> bookId;
// Find the user and book based on input IDs
User* user = nullptr;
for (auto& u : users) {
if (u.id == userId) {
user = &u;
break;
}
}
Book* book = nullptr;
for (auto& b : books) {
if (b.id == bookId) {
book = &b;
break;
}
}
if (user && book) {
double predictedRating = predictRating(*user, *book);
cout << "Predicted rating for book '" << book->title << "' by user '" << user->name << "': " << predictedRating << endl;
} else {
cout << "User or book not found." << endl;
}
return 0;
}