-
Notifications
You must be signed in to change notification settings - Fork 0
/
query2.h
174 lines (129 loc) · 4.07 KB
/
query2.h
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
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <queue>
#include "Queue.h"
using namespace std;
struct countryData2 {
string day;
string country;
string countryName;
string region;
int confirmed;
int cumConfirmed;
int dateDiff; // difference between two cumulatives
int prevC; // this holds confirmed cases on the first date
int nextC; // this holds confirmed cases on the second date
countryData2() {
day = country = countryName = region = "";
confirmed = cumConfirmed = -1;
prevC = nextC = 0;
}
bool operator() (countryData2 const& c1, countryData2 const& c2) {
return c1.dateDiff > c2.dateDiff;
}
bool operator < (const countryData2& obj) const {
return this->dateDiff > obj.dateDiff;
}
};
class query2 {
string date1;
string date2;
priority_queue<countryData2> countries;
public:
query2() {
cout << "Enter the first date (DD-MM-YY): ";
cin >> date1;
cout << "Enter the second date (DD-MM-YY): ";
cin >> date2;
}
void fileRead() {
ifstream in("WHO-COVID-19.csv");
countryData2 pendingCountry;
if (in) {
countryData2 temp;
string str;
getline(in, str); // reading the column headings of the csv
while (!in.eof()) {
getline(in, str, ',');
temp.day = str;
if (str.size() == 0)
break;
getline(in, str, ',');
temp.country = str;
// special case for reading "Bonaire, Sint Eustatius and Saba"
// and "occupied Palestinian territory, including east Jerusalem"
// as they have a comma ',' in the string
if (temp.country == "BQ" or temp.country == "PS") {
getline(in, str, ',');
temp.countryName = str;
getline(in, str, ',');
temp.countryName += ',';
temp.countryName += str;
}
else {
getline(in, str, ',');
temp.countryName = str;
}
if (temp.countryName == "undefined")
continue;
getline(in, str, ',');
temp.region = str;
getline(in, str, ',');
getline(in, str, ',');
getline(in, str, ',');
temp.confirmed = stoi(str);
getline(in, str, '\n');
temp.cumConfirmed = stoi(str);
if (temp.day == date1) { // record for first date
pendingCountry = temp;
pendingCountry.prevC = temp.cumConfirmed;
}
if (temp.day == date2 and temp.country == pendingCountry.country) { // a match for second date + same country
pendingCountry.nextC = temp.cumConfirmed;
pendingCountry.dateDiff = pendingCountry.nextC - pendingCountry.prevC; // subtracting the two cumulatives
pendingCountry.dateDiff += pendingCountry.confirmed;
if (countries.empty())
countries.push(pendingCountry);
else {
temp = countries.top(); // peeking to see if queue contains lesser priority items
if (temp.dateDiff == pendingCountry.dateDiff) // if equal priorities
countries.push(pendingCountry);
else {
bool flag = true;
while (temp.dateDiff < pendingCountry.dateDiff) { // removing lower priority items from the queue
countries.pop();
if (flag) { // adding the new country just once
countries.push(pendingCountry);
flag = false;
}
temp = countries.top(); // extracting next country
}
}
}
}
}
in.close();
}
}
void runAll() {
this->fileRead();
if (countries.empty()) {
cout << "\nNo data available for these dates.\n";
return;
}
cout << "\nMaximum new cases between '" << date1 << "' and " << date2 << "': " << countries.top().dateDiff << "\n";
while (!countries.empty()) {
countryData2 temp = countries.top();
countries.pop();
cout << "\n-------------------------" << "\n\n";
cout << "Country: " << temp.country << "\n";
cout << "Country Name: " << temp.countryName << "\n";
cout << "Region: " << temp.region << "\n";
cout << "Cases (" << date1 << "): " << temp.prevC << "\n";
cout << "Cases (" << date2 << "): " << temp.nextC << "\n";
}
}
};