-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogrecent.cpp
More file actions
102 lines (88 loc) · 2.48 KB
/
logrecent.cpp
File metadata and controls
102 lines (88 loc) · 2.48 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class LogProcessor {
struct User {
string userId;
string timestamp;
};
User* users;
int size;
struct Latest {
string userId;
string timestamp;
};
Latest* latestUsers;
int latestCount;
public:
LogProcessor() {
size = 1000;
users = new User[size];
latestUsers = new Latest[1000];
latestCount = 0;
}
void readLogFile(const string& filename) {
ifstream file(filename);
string uid, ts;
int index = 0;
while (file >> uid >> ts) {
users[index].userId = uid;
users[index].timestamp = ts;
index++;
}
size = index;
file.close();
}
bool isNewer(string t1, string t2) {
return t1 > t2; // ISO format strings can be compared lexicographically
}
void processLatest() {
for (int i = 0; i < size; i++) {
bool found = false;
for (int j = 0; j < latestCount; j++) {
if (users[i].userId == latestUsers[j].userId) {
found = true;
if (isNewer(users[i].timestamp, latestUsers[j].timestamp)) {
latestUsers[j].timestamp = users[i].timestamp;
}
break;
}
}
if (!found) {
latestUsers[latestCount].userId = users[i].userId;
latestUsers[latestCount].timestamp = users[i].timestamp;
latestCount++;
}
}
}
void sortLatest() {
for (int i = 0; i < latestCount - 1; i++) {
for (int j = i + 1; j < latestCount; j++) {
if (isNewer(latestUsers[j].timestamp, latestUsers[i].timestamp)) {
Latest temp = latestUsers[i];
latestUsers[i] = latestUsers[j];
latestUsers[j] = temp;
}
}
}
}
void displayTopFive() {
cout << "Top 5 most recently active users:\n";
for (int i = 0; i < 5 && i < latestCount; i++) {
cout << latestUsers[i].userId << " " << latestUsers[i].timestamp << endl;
}
}
~LogProcessor() {
delete[] users;
delete[] latestUsers;
}
};
int main() {
LogProcessor lp;
lp.readLogFile("log.txt");
lp.processLatest();
lp.sortLatest();
lp.displayTopFive();
return 0;
}