-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.cpp
More file actions
113 lines (96 loc) · 3.19 KB
/
parse.cpp
File metadata and controls
113 lines (96 loc) · 3.19 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
103
104
105
106
107
108
109
110
111
112
113
#include "calcs/calc.hpp"
#include <queue>
#include <memory>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <stack>
class chart {
public:
double sa_points;
std::string song_name;
int level;
int score;
chart(std::string n, int s, int l) { //constructor
song_name = n;
level = l;
score = s;
sa_points = calculate_sa(level, score);
}
std::string formatPrint() {
return song_name + "\t" + std::to_string(level) + "\t" + std::to_string(score) + "\t" + std::to_string(sa_points) + "\n";
}
};
std::vector<std::string> parseCSVLine(const std::string& line) {
std::vector<std::string> fields;
std::stringstream ss(line);
std::string field;
while (std::getline(ss, field)) {
fields.push_back(field);
}
return fields;
}
struct CompareCharts {
bool operator()(const std::shared_ptr<chart>& a,const std::shared_ptr<chart>& b) {
return a->sa_points > b->sa_points; //> = min heap. < = max heap.
}
};
int main() {
std::ifstream file("output.csv"); // Open your CSV file
std::priority_queue<std::shared_ptr<chart>, std::vector<std::shared_ptr<chart>>, CompareCharts> min_heap; //heap to keep track of max 30 in a more memory efficient way
std::stack<std::shared_ptr<chart>> top30;
if (!file.is_open()) {
std::cerr << "Error opening file!" << std::endl;
return 1;
}
std::string line;
bool firstline = true;
while (std::getline(file, line)) { // Read line by line
std::vector<std::string> fields = parseCSVLine(line);
// Process the fields into chart
int col = 0;
std::string name;
int score;
int level;
for (const std::string& field : fields) {
if(!firstline) {
std::stringstream ss(field);
std::string segment;
std::vector<std::string> segments;
while (std::getline(ss, segment, '\t')) { // Use ',' as the delimiter
segments.push_back(segment);
}
name = segments[1];
level = std::stoi(segments[3]);
score = std::stoi(segments[4]);
auto c = std::make_shared<chart>(name, score, level);
if(min_heap.size() == 30) {
if(c->sa_points > min_heap.top()->sa_points) {
min_heap.pop();
min_heap.push(c);
}
}
else {
min_heap.push(c);
}
}
else firstline = false;
}
}
while(!min_heap.empty()) {
top30.push(min_heap.top());
min_heap.pop();
}
double sum = 0;
while(!top30.empty()) {
std::cout << top30.top()->formatPrint() << std::endl;
sum+=top30.top()->sa_points;
top30.pop();
}
double points = sum/30.0;
std::cout << "your total SA is: " + std::to_string(sum) +". And your avg SA in top 30 is:" + std::to_string(points) << std::endl;
file.close();
return 0;
}