-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
656 lines (613 loc) · 21.3 KB
/
main.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<stdlib.h>
#include<bits/stdc++.h>
#include<cstring>
#include<sstream>
#include<ctime>
using namespace std;
const int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct date
{
int dd;
int mm;
int yy;
};
struct PatientData
{
char FirstName[20];
char LastName[20];
char Gender; //M for male, F for Female, O for others
char BloodGroup[4];
int age;
char PinCode[7];
struct date Entry; //date of entry
struct date Exit; //date of exit
char HospitalName[20];
char contact[11]; //Contact Number
};
struct LogIn
{
string Username;
string Password;
};
int countLeapYears(int d,int m,int y)
{
int years = y;
// Check if the current year needs to be considered for the count of leap years or not
if (m <= 2)
years--;
// An year is a leap year if it is a multiple of 4 multiple of 400 and not a multiple of 100.
return years / 4 - years / 100 + years / 400;
}
int getDifference(int d1, int m1 , int y1)
{
// COUNT TOTAL NUMBER OF DAYS BEFORE FIRST DATE 'dt1'
time_t now = time(0);
tm *ltm = localtime(&now);
int y2=1900 + ltm->tm_year;
int m2= 1 + ltm->tm_mon;
int d2= ltm->tm_mday;
// initialize count using years and day
long int n1 = y1 * 365 + d1;
// Add days for months in given date
for (int i = 0; i < m1 - 1; i++)
n1 += monthDays[i];
// Since every leap year is of 366 days, Add a day for every leap year
n1 += countLeapYears(d1,m1,y1);
// SIMILARLY, COUNT TOTAL NUMBER OF DAYS BEFORE 'dt2'
long int n2 = y2 * 365 + d2;
for (int i = 0; i < m2 - 1; i++)
n2 += monthDays[i];
n2 += countLeapYears(d2,m2,y2);
// return difference between two counts
return (n2 - n1);
}
template<typename T> void printElement(T t, const int& width)
{
cout << left << setw(width) << setfill(' ') << t;
}
class Hash
{
int n = 98;
// Pointer to an array containing buckets
vector<vector<int>> v;
public:
Hash(); // Constructor
// inserts a key into hash table
void insertItem(int x, string arr[][13]);
// hash function to map values to key
int hashFunction(int x)
{
return (x - 110000);
}
void displayHash(string arr[][13], int x);
};
Hash::Hash()
{
v = vector<vector<int>>(98);
}
void Hash::insertItem(int x, string arr[][13]) //We store index in hash table
{
int index = hashFunction(stoi(arr[x][0]));
v[index].push_back(x);
}
void Hash::displayHash(string arr[][13], int x)
{
const int nameWidth = 30;
const int numWidth = 15;
printElement("Patient's Name", nameWidth);
printElement("Age", numWidth);
printElement("Gender", numWidth);
printElement("Duration*", numWidth);
printElement("Pincode", numWidth);
printElement("Contact Number", numWidth);
cout<<"\n________________________________________________________________________________________________________";
cout<<"\n\n";
int prev, next, SizeofPrev, SizeofNext, DiffofPrev, DiffofNext, duration;
string FullName;
prev = hashFunction(x);
next = hashFunction(x);
SizeofPrev = v[prev].size();
if(SizeofPrev != 0)
{
for(int j = 0; j<SizeofPrev; j++)
{
FullName = arr[v[prev][j]][1] + " " + arr[v[prev][j]][2];
duration = getDifference(stoi(arr[v[prev][j]][10]), stoi(arr[v[prev][j]][11]), stoi(arr[v[prev][j]][12]));
printElement(FullName, nameWidth);
printElement(arr[v[prev][j]][3], numWidth);
printElement(arr[v[prev][j]][4], numWidth);
printElement(duration, numWidth);
printElement(arr[v[prev][j]][0], numWidth);
printElement(arr[v[prev][j]][5], numWidth);
cout << endl;
}
}
prev--;
next++;
do
{
SizeofPrev = v[prev].size();
SizeofNext = v[next].size();
while(SizeofPrev == 0)
{
prev--;
if(prev < 0)
break;
SizeofPrev = v[prev].size();
}
while(SizeofNext == 0)
{
next++;
if(next > 97)
break;
SizeofNext = v[next].size();
}
if(prev < 0 || next > 97)
break;
DiffofPrev = hashFunction(x) - prev;
DiffofNext = next - hashFunction(x);
if(DiffofPrev < DiffofNext)
{
for(int j = 0; j<v[prev].size(); j++)
{
FullName = arr[v[prev][j]][1] + " " + arr[v[prev][j]][2];
duration = getDifference(stoi(arr[v[prev][j]][10]), stoi(arr[v[prev][j]][11]), stoi(arr[v[prev][j]][12]));
printElement(FullName, nameWidth);
printElement(arr[v[prev][j]][3], numWidth);
printElement(arr[v[prev][j]][4], numWidth);
printElement(duration, numWidth);
printElement(arr[v[prev][j]][0], numWidth);
printElement(arr[v[prev][j]][5], numWidth);
cout << endl;
}
prev--;
}
else
{
for(int j = 0; j<v[next].size(); j++)
{
FullName = arr[v[next][j]][1] + " " + arr[v[next][j]][2];
duration = getDifference(stoi(arr[v[next][j]][10]), stoi(arr[v[next][j]][11]), stoi(arr[v[next][j]][12]));
printElement(FullName, nameWidth);
printElement(arr[v[next][j]][3], numWidth);
printElement(arr[v[next][j]][4], numWidth);
printElement(duration, numWidth);
printElement(arr[v[next][j]][0], numWidth);
printElement(arr[v[next][j]][5], numWidth);
cout << endl;
}
next++;
}
}while(prev >= 0 || next <= 97);
while(prev >= 0)
{
if(v[prev].size() != 0)
{
for(int j = 0; j<v[prev].size(); j++)
{
FullName = arr[v[prev][j]][1] + " " + arr[v[prev][j]][2];
duration = getDifference(stoi(arr[v[prev][j]][10]), stoi(arr[v[prev][j]][11]), stoi(arr[v[prev][j]][12]));
printElement(FullName, nameWidth);
printElement(arr[v[prev][j]][3], numWidth);
printElement(arr[v[prev][j]][4], numWidth);
printElement(duration, numWidth);
printElement(arr[v[prev][j]][0], numWidth);
printElement(arr[v[prev][j]][5], numWidth);
cout << endl;
}
}
prev--;
}
while(next <= 97)
{
if(v[next].size() != 0)
{
for(int j = 0; j<v[next].size(); j++)
{
FullName = arr[v[next][j]][1] + " " + arr[v[next][j]][2];
duration = getDifference(stoi(arr[v[next][j]][10]), stoi(arr[v[next][j]][11]), stoi(arr[v[next][j]][12]));
printElement(FullName, nameWidth);
printElement(arr[v[next][j]][3], numWidth);
printElement(arr[v[next][j]][4], numWidth);
printElement(duration, numWidth);
printElement(arr[v[next][j]][0], numWidth);
printElement(arr[v[next][j]][5], numWidth);
cout << endl;
}
}
next++;
}
cout<<"*Duration is the number of Days since the Patient was Discharged from the Hospital.\n";
}
void LoginPage();
void MainScreen();
void WelcomeNote()
{
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
cout<<"\t\t\t\t\t@@ _______________________________________________________________________________________ @@\n";
cout<<"\t\t\t\t\t@@| |@@\n";
cout<<"\t\t\t\t\t@@| |@@\n";
cout<<"\t\t\t\t\t@@| |@@\n";
cout<<"\t\t\t\t\t@@| |@@\n";
cout<<"\t\t\t\t\t@@| |@@\n";
cout<<"\t\t\t\t\t@@| |@@\n";
cout<<"\t\t\t\t\t@@| WELCOME TO |@@\n";
cout<<"\t\t\t\t\t@@| |@@\n";
cout<<"\t\t\t\t\t@@| COVID DATABASE MANAGEMENT SYSTEM |@@\n";
cout<<"\t\t\t\t\t@@| |@@\n";
cout<<"\t\t\t\t\t@@| |@@\n";
cout<<"\t\t\t\t\t@@| |@@\n";
cout<<"\t\t\t\t\t@@| |@@\n";
cout<<"\t\t\t\t\t@@| |@@\n";
cout<<"\t\t\t\t\t@@| |@@\n";
cout<<"\t\t\t\t\t@@|_______________________________________________________________________________________|@@\n";
cout<<"\t\t\t\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n\n\n\t\t\t\t\t";
system("pause");
system("cls");
}
int compareStrings(string arr[], string str, int last)
{
for(int j = 0; j < last; j++)
{
if(arr[j] == str)
return 0;
}
return -1;
}
void Login()
{
string array[100];
int loop = 0;
string line;
char y;
string UserName, PassWord, UserPassInput;
ifstream myfile("Info.txt", ios::in);
if(myfile.is_open())
{
while(!myfile.eof())
{
getline(myfile, line, '\n');
array[loop] = line;
loop++;
}
myfile.close();
}
else
{
cout<<"Unable to open File";
}
cout << "==============================================" << endl;
cout << "PLEASE ENTER LOGIN DETAILS:" << endl;
cout << "==============================================" << endl;
y = getchar();
cout << "ENTER USERNAME: ";
getline(cin, UserName, '\n');
cout << "ENTER PASSWORD: ";
getline(cin, PassWord, '\n');
UserName.append(PassWord);
int n = sizeof(array)/sizeof(array[0]);
int result = compareStrings(array, UserName, n);
if(result == -1)
{
cout<<"Wrong Username or Password.\nPlease Login Again.\n";
system("pause");
system("cls");
Login();
}
else
{
system("pause");
MainScreen();
}
}
void Signup()
{
char y;
LogIn ID;
system("cls");
ofstream outfile;
outfile.open("Info.txt", ios::app);
if(!outfile)
{
cout<<"Error in opening file";
return;
}
cout << "==============================================" << endl;
cout << "REGISTER BELOW:" << endl;
cout << "==============================================" << endl;
y = getchar();
cout << "ENTER USERNAME (UP TO 10 CHARACTERS LONG): ";
getline(cin, ID.Username, '\n');
outfile << ID.Username;
cout << "ENTER PASSWORD (UP TO 10 CHARACTERS LONG): ";
getline(cin, ID.Password, '\n');
outfile << ID.Password << endl;
cout << "==============================================" << endl;
cout << endl;
cout<<"You Have Successfully Registered.\nLogin to Continue.\n";
outfile.close();
system("pause");
system("cls");
LoginPage();
}
void LoginPage()
{
int ch;
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t COVID DATABASE MANAGEMENT SYSTEM \n\n";
cout<<"\n\n\t\t\t\t\t\tPlease, Choose from the following Options: \n\n";
cout<<"\t\t\t\t\t\t _________________________________________________________________ \n";
cout<<"\t\t\t\t\t\t| |\n";
cout<<"\t\t\t\t\t\t| 1 >> LOGIN, if already registered |\n";
cout<<"\t\t\t\t\t\t| 2 >> SIGNUP, if you are a new user |\n";
cout<<"\t\t\t\t\t\t| 3 >> EXIT |\n";
cout<<"\t\t\t\t\t\t|_________________________________________________________________|\n\n\n\n";
a:
cout<<"\t\t\t\t\t\tEnter your choice: ";
cin>>ch;
system("cls");
switch(ch)
{
case 1:
Login();
break;
case 2:
cout<<"\n\n\n\n\t\t\t\t\t\t";
Signup();
break;
case 3:
cout<<"\n\n\n\n\t\t\t\t\t\t";
cout<<"Thank you for Using this Application.";
exit(0);
break;
default:
cout<<"Invalid Choice\n\n\n";
goto a;
break;
}
cout<<"\n\n\n\n\n";
system("pause");
}
bool isLeap(int year)
{
return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
}
bool CheckDate(int d, int m, int y)
{
if (m < 1 || m > 12)
return false;
if (d < 1 || d > 31)
return false;
if (m == 2)
{
if (isLeap(y))
return (d <= 29);
else
return (d <= 28);
}
if (m == 4 || m == 6 || m == 9 || m == 11)
return (d <= 30);
return true;
}
void FileInput()
{
int EntryDuration, ExitDuration;
char ch, x;
int n, i;
struct PatientData p;
string y, filename;
do
{
x = getchar();
cout<<"Enter Blood Group (in Captials): ";
cin.get(p.BloodGroup, 4);
while (strcmp(p.BloodGroup, "A+") != 0 && strcmp(p.BloodGroup, "A-") != 0 && strcmp(p.BloodGroup, "B+") != 0 && strcmp(p.BloodGroup, "B-") != 0 && strcmp(p.BloodGroup, "AB+") != 0 && strcmp(p.BloodGroup, "AB-") != 0 && strcmp(p.BloodGroup, "O+") != 0 && strcmp(p.BloodGroup, "O-") != 0)
{
cout<<"Please enter a Correct Blood Group.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Enter Blood Group (in Capitals): ";
cin.get(p.BloodGroup, 3);
}
filename.clear();
filename.append(p.BloodGroup).append(".csv");
fstream outfile;
outfile.open(filename, ios::app);
//check for file opening
if(!outfile)
{
cout<<"Error in opening file";
return;
}
x = getchar();
cout<<"\nEnter Patient's First Name: ";
cin.get(p.FirstName, 20);
x = getchar();
cout<<"Enter Patient's Last Name: ";
cin.get(p.LastName, 20);
cout<<"Enter Gender(M:Male,F:Female,O:Others): ";
cin>>p.Gender;
p.Gender = toupper(p.Gender);
while (p.Gender != 'M' && p.Gender != 'F' && p.Gender != 'O')
{
cout<<"Please enter a Correct Gender.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Enter Gender: ";
cin>>p.Gender;
p.Gender = toupper(p.Gender);
}
cout<<"Enter Patient's Age: ";
cin>>p.age;
while(p.age<=0)
{
cout<<"Please enter a valid age.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Enter Patient's Age: ";
cin>>p.age;
}
x = getchar();
cout<<"Enter Pincode: ";
cin.get(p.PinCode, 7);
while(stoi(p.PinCode) <= 110000 || stoi(p.PinCode) >= 110098)
{
cout<<"Please enter a correct Pincode.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Enter Pincode: ";
cin.get(p.PinCode, 7);
}
a:
cout<<"Enter date when admitted in the Hospital in the form dd[space]mm[space]yyyy: ";
cin>>p.Entry.dd>>p.Entry.mm>>p.Entry.yy;
while(!CheckDate(p.Entry.dd, p.Entry.mm, p.Entry.yy))
{
cout<<"Please enter a valid Date.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Enter date of Entry in the form dd[space]mm[space]yyyy: ";
cin>>p.Entry.dd>>p.Entry.mm>>p.Entry.yy;
}
EntryDuration = getDifference(p.Entry.dd, p.Entry.mm, p.Entry.yy);
cout<<"Enter date when discharged from the hospital in the form dd[space]mm[space]yyyy: ";
cin>>p.Exit.dd>>p.Exit.mm>>p.Exit.yy;
while(!CheckDate(p.Exit.dd, p.Exit.mm, p.Exit.yy))
{
cout<<"Please enter a valid Date.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Enter date of Exit in the form dd[space]mm[space]yyyy: ";
cin>>p.Exit.dd>>p.Exit.mm>>p.Exit.yy;
}
ExitDuration = getDifference(p.Exit.dd, p.Exit.mm, p.Exit.yy);
if(EntryDuration < ExitDuration)
{
cout<<"Please Check the Dates.\n";
goto a;
}
x = getchar();
cout<<"Enter Hospital Name: ";
cin.get(p.HospitalName, 20);
x = getchar();
cout<<"Enter Contact Number: ";
cin.get(p.contact, 11);
outfile<<p.PinCode<<","<<p.FirstName<<","<<p.LastName<<","<<p.age<<","<<p.Gender<<","<<p.contact<<","<<p.HospitalName<<","<<p.Entry.dd<<","<<p.Entry.mm<<","<<p.Entry.yy<<","<<p.Exit.dd<<","<<p.Exit.mm<<","<<p.Exit.yy<<"\n";
cout<<"\n\n";
cout<<"Do you want to enter more(y/n)";
cin>>ch;
cout<<"\n";
outfile.close();
}while(ch=='y'||ch=='Y');
system("pause");
MainScreen();
}
void read_record()
{
Hash h;
char ch, BloodGroup[4], x;
string filename;
PatientData p;
int PinCode;
x = getchar();
cout<<"Enter the Blood Group for which you want Plasma (in Capitals): ";
cin.get(BloodGroup, 4);
while (strcmp(BloodGroup, "A+") != 0 && strcmp(BloodGroup, "A-") != 0 && strcmp(BloodGroup, "B+") != 0 && strcmp(BloodGroup, "B-") != 0 && strcmp(BloodGroup, "AB+") != 0 && strcmp(BloodGroup, "AB-") != 0 && strcmp(BloodGroup, "O+") != 0 && strcmp(BloodGroup, "O-") != 0)
{
cout<<"Please enter a Correct Blood Group.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Enter the Blood Group for which you want Plasma (in Capitals): ";
cin.get(p.BloodGroup, 3);
}
filename.append(BloodGroup).append(".csv");
fstream infile;
infile.open(filename,ios::in);
if(!infile)
{
cout<<"Error in opening file";
return;
}
cout<<"Enter your Pincode: ";
cin>>PinCode;
while(PinCode <= 110000 || PinCode >= 110098)
{
cout<<"Please enter a correct Pincode.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Enter Pincode: ";
cin>>PinCode;
}
cout<<endl;
// Read the Data from the file
// as String Vector
system("pause");
system("cls");
string data[100][13];
string word, temp;
int i =0, j = 0, c =0;
while (infile >> temp)
{
// used for breaking words
stringstream s(temp);
// read every column data of a row and
// store it in a string variable, 'word'
while (getline(s, word, ','))
{
// add all the column data
// of a row to a vector
data[i][c] = word;
c++;
}
h.insertItem(j, data);
j++;
s.clear();
}
h.displayHash(data, PinCode);
cout<<"\n\n";
system("pause");
MainScreen();
}
void MainScreen()
{
int ch;
system("cls");
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t COVID DATABASE MANAGEMENT SYSTEM \n\n";
cout<<"\n\n\t\t\t\t\t\tPlease, Choose from the following Options: \n\n";
cout<<"\t\t\t\t\t\t _________________________________________________________________ \n";
cout<<"\t\t\t\t\t\t| |\n";
cout<<"\t\t\t\t\t\t| 1 >> Add New Patient Record |\n";
cout<<"\t\t\t\t\t\t| 2 >> Search for Plasma Donors |\n";
cout<<"\t\t\t\t\t\t| 3 >> Exit |\n";
cout<<"\t\t\t\t\t\t|_________________________________________________________________|\n\n\n\n";
b:
cout<<"t\t\t\t\t\tEnter your choice: ";
cin>>ch;
system("cls");
switch(ch)
{
case 1:
FileInput();
break;
case 2:
read_record();
break;
case 3:
cout<<"\n\n\n\n\t\t\t\tThank You for Using this Application.";
exit(0);
break;
default:
cout<<"Invalid Choice.\n\n\n";
goto b;
break;
}
}
int main()
{
WelcomeNote();
LoginPage();
return 0;
}