-
Notifications
You must be signed in to change notification settings - Fork 0
/
diseaseMonitorMain.cpp
305 lines (268 loc) · 12.3 KB
/
diseaseMonitorMain.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//Kanellaki Maria Anna - 1115201400060
#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#include "PatientList.h"
#include "HashTable.h"
#include "MaxHeap.h"
using namespace std;
int main(int argc, char * argv[])
{
if (argc != 9) //program runs only with input in the format requested
{
cout << "Error. Please input from the command line input file, num of disease and country entries and bucket size."<<endl;
exit(-1);
}
FILE * patientFile = fopen(argv[2], "r");
if (!patientFile)
{
cout << "Error. Cannot find input file."<<endl;
exit(-1);
}
//initializes values for hash tables
int diseaseEntries = atoi(argv[4]);
int countryEntries = atoi(argv[6]);
int bucketSize = atoi(argv[8]);
//initializes data structures
PatientList * patients = new PatientList(); //creates empty list of patients
HashTable * diseaseTable = new HashTable(diseaseEntries, bucketSize); //create empty disease hash table
HashTable * countryTable = new HashTable(countryEntries, bucketSize); //create empty country hash table
//reads file line by line
size_t lsize = 0;
char * line = NULL;
while(getline(&line, &lsize, patientFile) != -1)
{
PatientRecord * rec = new PatientRecord(line); //creates PatientRecord from line of file read
if (!rec->isValid()) //if record is not valid deletes it and continues to next line
{
delete rec;
continue;
}
PatientNode * nodeL = new PatientNode(rec); //creates PatientNode with rec
patients->push(nodeL); //inserts node in list
diseaseTable->InsertToTable(rec, rec->getDiseaseId()); //inserts record in disease table
countryTable->InsertToTable(rec, rec->getCountry()); //inserts record in country table
}
fclose(patientFile);
cout <<endl<< "Your options are the following:"<<endl; //menu of options
cout << "1. /globalDiseaseStats (optional: dates)"<<endl;
cout << "2. /diseaseFrequency for a disease with dates (optional: country)"<<endl;
cout << "3. /topk-Diseases k for a country (optional: dates)"<<endl;
cout << "4. /topk-Countries k for a disease (optional: dates)"<<endl;
cout << "5. /insertPatientRecord with recordID, firstName, lastName, diseaseID, country, entryDate, exitDate(optional)"<<endl;
cout << "6. /recordPatientExit with recordID and exitDate"<<endl;
cout << "7. /numCurrentPatients (optional: disease)"<<endl;
cout << "8. /exit"<<endl;
string input;
char *date1, *date2, *country, *disease;
char delimit[]=" \t\n";
do{
cout << "\nEnter your input:\n";
getline(cin, input);
strcpy(line, input.c_str());
cout << endl;
if (input.find("/globalDiseaseStats") != string::npos) //prints count of diseased people for every disease
{
strtok(line, delimit); //splits command name
if (!(date1 = strtok(NULL, delimit))) //no date arguments
diseaseTable->PrintCountOfDiseased();
else //counts cases between given dates
{
if (!(date2 = strtok(NULL, delimit))) //if there is only one date then the argument is not valid
{
cout << "Please input 2 dates or none. Request aborted."<< endl;
continue;
}
Date * d1 = new Date(date1);
Date * d2 = new Date(date2);
if (!d1->isValid() || !d2->isValid())
{
delete d1;
delete d2;
continue;
}
diseaseTable->PrintCountOfDiseasedDates(d1, d2); //prints count of diseased people for every disease between the 2 dates
}
}
else if (input.find("/diseaseFrequency") != string::npos) //prints count of diseased people for given disease and given dates
{
strtok(line, delimit); //splits command name
if (!(disease = strtok(NULL, delimit))) {
cout << "Needs name of virus to execute" << endl;
continue;
}
if (!(date1 = strtok(NULL, delimit))) {
cout << "Needs dates to execute" << endl;
continue;
}
if (!(date2 = strtok(NULL, delimit))) {
cout << "Needs two dates to execute" << endl;
continue;
}
country = strtok(NULL, delimit);
Date *d1 = new Date(date1);
Date *d2 = new Date(date2);
if (!d1->isValid() || !d2->isValid())
{
delete d1;
delete d2;
continue;
}
if (!country)
diseaseTable->PrintCountOfADiseaseDates(disease, d1, d2);
else //given disease for given country
countryTable->PrintCountOfCountryOfADiseaseDates(country, disease, d1, d2);
}
else if (input.find("/topk-Diseases") != string::npos)
{
strtok(line, delimit); //splits command name
int k;
MaxHeap * diseaseHeap = new MaxHeap();
if (!(country = strtok(NULL, delimit)))
{
cout << "Needs number to execute" << endl;
continue;
}
else
k = atoi(country);
if (!(country = strtok(NULL, delimit)))
{
cout << "Needs country to execute" << endl;
continue;
}
if (!(date1 = strtok(NULL, delimit)))
{
cout << "Top "<< k <<" for " << country << " are:" << endl;
countryTable->CopyToHeap(country, diseaseHeap);
}
else
{
if (!(date2 = strtok(NULL, delimit))) //if there is only one date then the argument is not valid
{
cout << "Please input 2 dates or none. Request aborted."<< endl;
continue;
}
Date * d1 = new Date(date1);
Date * d2 = new Date(date2);
if (!d1->isValid() || !d2->isValid())
{
delete d1;
delete d2;
delete diseaseHeap;
continue;
}
countryTable->CopyToHeapDates(country, diseaseHeap, d1, d2);
cout << "Top "<< k <<" for " << country << " between " << d1->getAsString() << " and " << d2->getAsString() <<" are:" << endl;
}
diseaseHeap->PrintTopK(k, country);
delete diseaseHeap;
}
else if (input.find("/topk-Countries") != string::npos)
{
strtok(line, delimit); //splits command name
int k;
MaxHeap * countryHeap = new MaxHeap();
if (!(disease = strtok(NULL, delimit)))
{
cout << "Needs number to execute" << endl;
continue;
}
else
k = atoi(disease);
if (!(disease = strtok(NULL, delimit)))
{
cout << "Needs disease to execute" << endl;
continue;
}
if (!(date1 = strtok(NULL, delimit)))
{
cout << "Top "<< k <<" for " << disease << " are:" << endl;
diseaseTable->CopyToHeap(disease, countryHeap);
}
else
{
if (!(date2 = strtok(NULL, delimit))) //if there is only one date then the argument is not valid
{
cout << "Please input 2 dates or none. Request aborted."<< endl;
continue;
}
Date * d1 = new Date(date1);
Date * d2 = new Date(date2);
if (!d1->isValid() || !d2->isValid())
{
delete d1;
delete d2;
delete countryHeap;
continue;
}
diseaseTable->CopyToHeapDates(disease, countryHeap, d1, d2);
cout << "Top "<< k <<" for " << disease << " between " << date1 << " and " << date2 <<" are:" << endl;
}
countryHeap->PrintTopK(k, disease);
delete countryHeap;
}
else if (input.find("/insertPatientRecord") != string::npos) //creates new patient record and inserts it in every data structure
{
strtok(line, delimit); //splits command name
PatientRecord * record = new PatientRecord(strtok(NULL, "\n")); //creates new record from remaining line
if (!record->isValid())
{
delete record;
continue;
}
PatientNode * nodeL = new PatientNode(record); //creates PatientNode with rec
patients->push(nodeL); //inserts node in list
diseaseTable->InsertToTable(record, record->getDiseaseId()); //inserts record in disease table
countryTable->InsertToTable(record, record->getCountry()); //inserts record in country table
cout << "Record "<< record->getRecordId() <<" inserted successfully." << endl;
}
else if (input.find("/recordPatientExit") != string::npos) //adds exit date to patient with given record id
{
strtok(line, delimit); //splits command name
string id = strtok(NULL, delimit);
date1 = strtok(NULL, delimit);
PatientRecord * record = patients->SearchRecord(id); //since no structure uses the id as their key and offers quicker access, the search is implemented in the list
if (record)
{
if (record->getExitDate())
cout << "Patient is already checked out."<< endl;
else
{
Date * exit = new Date(date1);
if (!exit->isValid())
continue;
if (exit->earlierThan(record->getEntryDate()))
cout << "Exit date invalid." << endl;
else
{
record->setExitDate(exit);
cout << "Exit date updated successfully."<< endl;
}
}
}
else
cout << "Patient not found."<< endl;
}
else if (input.find("/numCurrentPatients") != string::npos) //prints count of people that are still admitted
{
strtok(line, delimit); //splits command name
if (!(disease = strtok(NULL, delimit)))
diseaseTable->PrintCountOfAdmitted();
else
diseaseTable->PrintCountOfAdmittedOfADisease(disease);
}
else if (input == "/exit")
break;
else
cout << "Wrong input" << endl;
}
while (strcmp(line, "/exit") != 0);
//deletes every structure (trees created and deleted by hash tables)
delete diseaseTable;
delete countryTable;
delete patients;
free(line);
return 0;
}