-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
208 lines (173 loc) · 5.92 KB
/
main.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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <functional>
using namespace std;
// Store one review:
class Review
{
public:
int movieID;
int userID;
int movieRating;
Review(int movie, int user, int rating)
{
movieID = movie;
userID = user;
movieRating = rating;
}
};
// Stores one movie:
class Movie
{
public:
int movieID;
string movieName;
Movie(int movie, string name)
{
movieID = movie;
movieName = name;
totalRating = 0;
numRatings = 0;
avgRating = 0;
}
//fields & functions I added to movie class
int totalRating;
int numRatings;
double avgRating;
int ratings[4] = {0,0,0,0};
double agverage(){
return (double)totalRating/(double)numRatings;
}
};
//stores user data
class User
{
public:
int id;
int numRatings;
User(int i){
id = i;
numRatings = 1;
}
void addReview(){
numRatings++;
}
};
// Parses one line of the reviews file and returns a Review object:
Review ParseReview(string line, vector<Movie>& vec, vector<User>& users)
{
istringstream strstream(line);
string movieIDstr, userIDstr, ratingstr;
getline(strstream, movieIDstr, ',');
getline(strstream, userIDstr, ',');
getline(strstream, ratingstr, ',');
int movieID = atoi(movieIDstr.c_str());
int userID = atoi(userIDstr.c_str());
int movieRating = atoi(ratingstr.c_str());
//update movie list
vec[movieID-1].totalRating += movieRating;
vec[movieID-1].numRatings++;
vec[movieID-1].avgRating = vec[movieID-1].agverage();
vec[movieID-1].ratings[movieRating-1]++;
//return review
return Review(movieID, userID, movieRating);
}
// Parses one line of the movies file and returns a Movie object:
Movie ParseMovie(string line)
{
size_t comma = line.find(',');
line = line.substr(0, line.size()-1);
int movieID = atoi(line.substr(0, comma).c_str());
string movieName = line.substr(comma + 1);
return Movie(movieID, movieName);
}
//start my functions' prototyping (below main)
bool movie_sort (Movie left, Movie right) { return (left.avgRating>right.avgRating); }
bool review_sort (Review left, Review right) { return (left.userID>right.userID); }
bool user_sort (User left, User right) { return (left.numRatings>right.numRatings); }
int searchMovie(vector<Movie>m);
void getTop10(vector<Movie> m);
void getTopUsers(vector<Review> r, vector<User>& u);
int main(int argc, const char * argv[]) {
//read in movies
ifstream input("movies.txt");
string line;
vector<Movie> movies;
while(getline(input, line))
{
movies.push_back(ParseMovie(line));
}
input.close();
//read in reviews
ifstream input2("reviews1.txt");
vector<Review> reviews;
vector<User> users;
while(getline(input2, line))
{
reviews.push_back(ParseReview(line, movies, users));
}
//get and output the top 10 movies
getTop10(movies);
//get and output the top 10 users
getTopUsers(reviews, users);
//propt user for movie id and give back movie info
cout << endl << endl << endl << "SEARCH FOR MOVIES (or type 0 to quit):" << endl;
while (searchMovie(movies)) {
continue;
}
}
int searchMovie(vector<Movie>m){
//prompt fot movie id
int mId;
cout << endl << "Enter a movie id: ";
cin >> mId;
if (mId == 0) { //if 0 is entered quit loop.
return 0;
}
if (mId > m.size() && mId < 0) { //if movie does not exist, say so
cout << endl << "Movie with id " << mId << " does not exist!" << endl;
}else{ //if movie is selected ouput info
cout << endl << left <<setw(6) << "ID" << setw(40) << "Name" << setw(10) << "Rating" << setw(14) << "# of Ratings" <<setw(8) << "1 Star" << setw(8) << "2 Star" << setw(8) << "3 Star" << setw(8) << "4 Star" << setw(8) << "5 Star" << endl;
cout << "----- --------------------------------------- --------- ------------- ------- ------- ------- ------- ------- " << endl;
cout << left <<setw(6) << mId << setw(40) << m[mId-1].movieName << setw(10) << m[mId-1].avgRating << m[mId-1].numRatings << setw(8) << setw(14) << m[mId-1].ratings[0] << setw(8) << m[mId-1].ratings[1] << setw(8) << m[mId-1].ratings[2] << setw(8) << m[mId-1].ratings[3] << setw(8) << m[mId-1].ratings[4] << endl;
}
return 1;
}
void getTop10(vector<Movie> m){
partial_sort(m.begin(), m.begin()+10, m.end(), movie_sort);
cout << "TOP 10 MOVIES:" << endl << endl;
cout << left << setw(5) << "Rank" <<setw(6) << "ID" << setw(40) << "Name" << setw(10) << "Rating" << "# of Ratings" << endl;
cout << "---- ----- --------------------------------------- --------- ------------" << endl;
int i;
for (i=0; i<10; i++) {
cout << left << setw(5) << i+1 <<setw(6) << m[i].movieID << setw(40) << m[i].movieName << setw(10) << m[i].avgRating << m[i].numRatings << endl;
}
}
void getTopUsers(vector<Review> r, vector<User>& u){
//sort reviews by username
sort(r.begin(), r.end(), review_sort);
int i;
//initialize user vector
for ( i = 0; i < r.size(); i++) {
if (i == 0){
u.push_back(User(r[i].userID));
}else if (r[i].userID == r[i-1].userID){
u[u.size()-1].addReview();
}else{
u.push_back(User(r[i].userID));
}
}
partial_sort(u.begin(), u.begin()+10, u.end(), user_sort);
//output results
cout << endl << endl << endl << "TOP 10 USERS:" << endl;
cout << endl << setw(5) << "Rank" << setw(10) << "ID" << "Number of Reviews"<< endl;
cout << "---- --------- -----------------" <<endl;
for (i=0; i<10; i++) {
cout << setw(5) << i+1 << left << setw(10) << u[i].id << u[i].numRatings << endl;
}
}