-
Notifications
You must be signed in to change notification settings - Fork 0
/
top10.cpp
128 lines (90 loc) · 2.8 KB
/
top10.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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <ctime>
using namespace std;
const int maximumItem = 7335;
const int maximumUser = 35190;
class mainfunc {
public:
int count;
int id;
mainfunc() {
count = 0;
id = 0;
};
void sets(int Id)
{
id = Id;
count++;
}
};
void heapifyfunc(mainfunc arr[], int m, int i){
int smallest = i;
int right = 2 * i + 2;
int left = 2 * i + 1;
if (left < m && arr[left].count < arr[smallest].count)
smallest = left;
if (right < m && arr[right].count < arr[smallest].count)
smallest = right;
if (smallest != i) {
swap(arr[i], arr[smallest]);
heapifyfunc(arr, m, smallest);
}
}
void heapSortfunc(mainfunc arr[], int n){
for (int i = n / 2 - 1; i >= 0; i--)
heapifyfunc(arr, n, i);
for (int i = n - 1; i >= 0; i--) {
swap(arr[0], arr[i]);
heapifyfunc(arr, i, 0);
}
}
int main()
{
string rating;
string itemid;
string line;
string userid;
int maxUser = 0;
int maxItem = 0;
int lines = 0;
mainfunc* userRatingCounts = new mainfunc[maximumUser];
mainfunc* movieRatingCounts = new mainfunc[maximumItem];
ifstream train("TXT_TRAIN_FILE_PATH");
getline(train, line);
while (getline(train, line))
{
stringstream strstream(line);
getline(strstream, userid, ',');
getline(strstream, itemid, ',');
getline(strstream, rating, ',');
int user_id = stoi(userid);
int item_id = stoi(itemid);
if (item_id > maxItem)maxItem = item_id;
if (user_id > maxUser)maxUser = user_id;
movieRatingCounts[item_id].sets(item_id);
userRatingCounts[user_id].sets(user_id);
lines++;
}
train.close();
heapSortfunc(movieRatingCounts, maximumItem);
heapSortfunc(userRatingCounts, maximumUser);
cout << "\nTop 10 Users: " << endl;
for (int k = 0; k < maximumUser; k++) {
std::cout <<k+1<< "-" << " User id = " << userRatingCounts[k].id << " -- count = " << userRatingCounts[k].count << std::endl;
if (k >= 9)
break;
}
cout << "\nTop 10 Items:" << endl;
for (int k = 0; k < maximumItem; k++) {
std::cout << k+1<< "-" << " Item id = " << movieRatingCounts[k].id << " -- count = " << movieRatingCounts[k].count << std::endl;
if (k >= 9)
break;
}
cout << "\n MaxUserId = " << maxUser << "\n MaxItemId = " << maxItem << "\n [MConverter.eu].txt lines = " << lines << endl;
delete[] movieRatingCounts;
delete[] userRatingCounts;
return 0;
}