-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovid_doc.c
914 lines (847 loc) · 20 KB
/
covid_doc.c
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
//Program to Handle Covid Patients in C: U19CS048
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct date{
int dd;
int mm;
int yy;
};
struct Patient{
int id;
char fname[20];
char lname[20];
int age;
char gender;
struct date adate;
struct date ddate;
char area[20];
} ;
struct docPat{
int docID;
int patID;
};
struct area{
char area[20];
int c;
};
struct Doctor{
int id;
char fname[20];
char lname[20];
int age;
char gender;
char area[20];
};
//Functions used
void inputP(struct Patient p);
void inputD(struct Doctor d);
void inputdp();
//---------------------
void readD();
void readDP();
void readP();
//---------------------
void countPatients();
void countGender();
void countAge();
void countArea();
//---------------------
int ispresent(int val,int arr[],int n);
int areaPresent(struct Patient p,struct area a[],int i);
void add(struct Patient p,struct area a[],int i);
//---------------------
void adDate();
void disDate();
void list(struct Patient p);
void patientsUnderDoc();
void patientsUnderMaleDoc();
void patientsUnderFemaleDoc();
//--------------------
void leastp();
//MAIN FUNCTION--------
int main(){
int ch,id,c;
struct date date;
struct Patient p;
struct Doctor d;
printf("******COVID PATIENT RECORDS******\n");
printf("1.Add a patient record\n2.Add a doctor record\n3.Add doctor-patient ids\n");
printf("4.Read entries\n5.Doctor having the least number of patients\n6.Count data\n7.");
printf("List all the patient records of the file in ascending order\n8.Exit\n");
while(1){
printf("\n\n================================================\n");
printf("Enter a choice: ");
scanf("%d",&ch);
switch(ch){
case 1:
inputP(p);
break;
case 2:
inputD(d);
break;
case 3:
inputdp();
break;
case 4:
printf("\t1.Doctor file\n\t2.Patient file\n\t3.Common file\n");
printf("\nEnter choice: ");
scanf("%d",&c);
switch(c){
case 1:
readD();
break;
case 2:
readP();
break;
case 3:
readDP();
break;
default:
printf("Enter valid choice.\n");
}
break;
case 5:
leastp();
break;
case 6:
printf("\t1.Count patients doctorwise\n\t2.Count patients gender doctorwise\n");
printf("\t3.Count patients age doctorwise\n\t4.Count patients area doctorwise\n");
printf("\nEnter choice: ");
scanf("%d",&c);
switch(c){
case 1:
countPatients();
break;
case 2:
countGender();
break;
case 3:
countAge();
break;
case 4:
countArea();
break;
default:
printf("Enter valid choice.\n");
}
break;
case 7:
printf("\t1.Admitted on same date\n\t2.Discharged on same date\n");
printf("\t3.Patients treated under the doctor\n\t4.Patients treated under the male doctor\n");
printf("\t5.Patients treated under the female doctor area wise\n");
printf("\nEnter choice: ");
scanf("%d",&c);
switch(c){
case 1:
adDate();
break;
case 2:
disDate();
break;
case 3:
patientsUnderDoc();
break;
case 4:
patientsUnderMaleDoc();
break;
case 5:
patientsUnderFemaleDoc();
break;
default:
printf("Enter valid choice.\n");
}
break;
case 8:
exit(0);
default:
printf("Wrong choice.\n");
}
}
return 0;
}
/*********************************************************/
//Function that helps to take patient input
void inputP(struct Patient p){
FILE *fp;
fp=fopen("patient.txt","a+");
printf("Enter patient id: ");
scanf("%d",&p.id);
printf("Enter patient first name: ");
fflush(stdin);
gets(p.fname);
printf("Enter patient last name: ");
fflush(stdin);
gets(p.lname);
printf("Enter patient age: ");
fflush(stdin);
scanf("%d",&p.age);
printf("Enter patient gender (M/F): ");
fflush(stdin);
scanf(" %c",&p.gender);
printf("Enter Admission date (format : dd mm yyyy ) : ");
fflush(stdin);
scanf("%d %d %d",&p.adate.dd,&p.adate.mm,&p.adate.yy);
printf("Enter Discharge date (format : dd mm yyyy ) : ");
fflush(stdin);
scanf("%d %d %d",&p.ddate.dd,&p.ddate.mm,&p.ddate.yy);
printf("Enter patient area: ");
fflush(stdin);
gets(p.area);
fwrite(&p,sizeof(struct Patient),1,fp);
printf("--------------------------------------\n\n");
fclose(fp);
}
/*********************************************************/
//Function that helps to take doctor input
void inputD(struct Doctor d){
FILE *fp;
fp=fopen("doctor.txt","a+");
printf("Enter doctor id: ");
scanf("%d",&d.id);
printf("Enter doctor first name: ");
fflush(stdin);
gets(d.fname);
printf("Enter doctor last name: ");
fflush(stdin);
gets(d.lname);
printf("Enter doctor age: ");
fflush(stdin);
scanf("%d",&d.age);
printf("Enter doctor gender (M/F): ");
fflush(stdin);
scanf(" %c",&d.gender);
printf("Enter doctor area: ");
fflush(stdin);
gets(d.area);
fwrite(&d,sizeof(struct Doctor),1,fp);
printf("--------------------------------------\n\n");
fclose(fp);
}
/*********************************************************/
//Inputs data in doctor-patient file
void inputdp(){
int flag1=0,flag2=0;
FILE *fp;
struct docPat dp;
struct Patient p;
struct Doctor d;
printf("Enter doctor id: ");
scanf("%d",&dp.docID);
printf("Enter patient id: ");
scanf("%d",&dp.patID);
//Checking if doctor with that id is present
fp=fopen("doctor.txt","a+");
while(fread(&d,sizeof(struct Doctor),1,fp))
{
if(d.id==dp.docID){
flag2=1;
break;
}
}
if(!flag2){
printf("ALERT: ");
printf("Doctor with that id not present. First make an entry in the doctor file.\n");
return;
}
fclose(fp);
//Checking if patient with that id is present
fp=fopen("patient.txt","a+");
while(fread(&p,sizeof(struct Patient),1,fp))
{
if(p.id==dp.patID){
flag1=1;
break;
}
}
if(!flag1){
printf("ALERT: ");
printf("Patient with that id not present. First make an entry in the patient file.\n");
return;
}
fclose(fp);
//Making entry in doctor-patient file
fp=fopen("doctor_patient.txt","a+");
if(fp==NULL){
printf("Unable to open file.\n");
return;
}
fwrite(&dp,sizeof(struct docPat),1,fp);
fclose(fp);
printf("Entry has been done successfully.\n");
}
//##########################################################//
//READ FUNCTIONS
//Reads and displays all the doctor entries
void readD(){
struct Doctor d;
FILE *fp;
fp=fopen("doctor.txt","r+");
if(fp==NULL)
printf("Failed to load!!!");
else{
printf("\nENTRIES ARE:");
while(fread(&d,sizeof(struct Doctor),1,fp))
{
printf("\n----------------------");
printf("\nDoctor id : %d",d.id);
printf("\nFirst Name : %s",d.fname);
printf("\nLast Name : %s",d.lname);
printf("\nAge : %d",d.age);
printf("\nGender : %c",d.gender);
printf("\nArea : %s",d.area);
}
}
fclose(fp);
}
//Reads and displays all the patient entries
void readP()
{
struct Patient p1;
FILE *fp;
fp=fopen("patient.txt","r+");
if(fp==NULL)
printf("Failed to load!!!");
else
{
printf("\nENTRIES ARE:");
while(fread(&p1,sizeof(struct Patient),1,fp))
{
printf("\n----------------------");
printf("\nPatient id : %d",p1.id);
printf("\nFirst Name : %s",p1.fname);
printf("\nLast Name : %s",p1.lname);
printf("\nAge : %d",p1.age);
printf("\nGender : %c",p1.gender);
printf("\nArea : %s",p1.area);
printf("\nAdmission Date : %d / %d / %d",p1.adate.dd,p1.adate.mm,p1.adate.yy);
printf("\nDischarge Date : %d / %d / %d",p1.ddate.dd,p1.ddate.mm,p1.ddate.yy);
}
}
fclose(fp);
}
//Reads and displays all the common file entries
void readDP(){
struct docPat dp;
FILE *fp;
fp=fopen("doctor_patient.txt","r+");
if(fp==NULL){
printf("Unable to open file.\n");
return;
}
else
{
printf("\nENTRIES ARE:");
while(fread(&dp,sizeof(struct docPat),1,fp))
{
printf("\n----------------------");
printf("\nPatient id : %d",dp.patID);
printf("\nDoctor id : %d",dp.docID);
}
}
fclose(fp);
}
//##########################################################//
//COUNT FUNCTIONS
void countPatients(){
struct docPat dp;
struct Doctor d;
FILE *fp,*fp1;
fp=fopen("doctor_patient.txt","r+");
if(fp==NULL){
printf("Unable to open file.\n");
return;
}
else
{
int doc[100]={0};
while(fread(&dp,sizeof(struct docPat),1,fp))
{
doc[dp.docID]++;
}
//========================
int i=0;
while(i<100){
if(doc[i]!=0){
fp1=fopen("doctor.txt","r+");
if(fp1==NULL){
printf("Unable to open doctor file.\n");
return;
}
while(fread(&d,sizeof(struct Doctor),1,fp1)){
if(d.id==i){
printf("Name of Doctor: %s %s",d.fname,d.lname);
printf(" | Patient Count: %d\n",doc[i]);
}
}
fclose(fp1);
}
i++;
}
}
fclose(fp);
}
/******************************************/
//Counting patients based on gender
void countGender(){
struct docPat dp;
struct Doctor d;
struct Patient p;
FILE *fp,*fp1;
fp=fopen("doctor.txt","r+");
while(fread(&d,sizeof(struct Doctor),1,fp)){
int countM=0,countF=0,pat[20]={0},i=0;
printf("Name of doctor: %s %s | ",d.fname,d.lname);
//Reading patients associated with that doctor
fp1=fopen("doctor_patient.txt","r+");
while(fread(&dp,sizeof(struct docPat),1,fp1)){
if(dp.docID==d.id){
pat[i]=dp.patID;
i++;
}
}
fclose(fp1);
//Checking if patient is male or female
fp1=fopen("patient.txt","r+");
while(fread(&p,sizeof(struct Patient),1,fp1)){
if(ispresent(p.id,pat,i)){
if(p.gender=='M'||p.gender=='m') countM++;
else countF++;
}
}
fclose(fp1);
//Printing count
printf("Male Patients - %d | Female Patients - %d\n",countM,countF);
}
fclose(fp);
}
//Auxillary function
int ispresent(int val,int arr[],int n){
int i=0;
for(i=0;i<n;i++){
if(val==arr[i]) return 1;
}
return 0;
}
/******************************************/
//Counting patients based on area
void countArea(){
struct docPat dp;
struct Doctor d;
struct Patient p;
FILE *fp,*fp1;
fp=fopen("doctor.txt","r+");
while(fread(&d,sizeof(struct Doctor),1,fp)){
int pat[20]={0},i=0,j=0,t;
printf("\n-> Name of doctor: %s %s Doctor id: %d\n",d.fname,d.lname,d.id);
//Reading patients associated with that doctor
fp1=fopen("doctor_patient.txt","r+");
while(fread(&dp,sizeof(struct docPat),1,fp1)){
if(dp.docID==d.id){
pat[i]=dp.patID;
i++;
}
}
fclose(fp1);
printf("\tTotal patients under consultation are %d. ",i);
printf("Area of the patients is as follows:\n");
int k=i;
//Checking area of patient
struct area a[20]={0,0};
i=0;
fp1=fopen("patient.txt","r+");
while(fread(&p,sizeof(struct Patient),1,fp1)){
if(ispresent(p.id,pat,k)){
if((t=areaPresent(p,a,i))!=-1){
a[t].c++;
}else{
add(p,a,i);
a[i].c++;
i++;
}
}
}
fclose(fp1);
//Printing count area
for(j=0;j<i;j++){
printf("\t%s : %d patients\n",a[j].area,a[j].c);
}
}
fclose(fp);
}
int areaPresent(struct Patient p,struct area a[],int i){
int j;
for(j=0;j<i;j++){
if(!strcmp(p.area,a[j].area)) return j;
}
return -1;
}
void add(struct Patient p,struct area a[],int i){
strcpy(a[i].area,p.area);
}
/******************************************/
//Counting patients based on age
void countAge(){
struct docPat dp;
struct Doctor d;
struct Patient p;
FILE *fp,*fp1;
fp=fopen("doctor.txt","r+");
while(fread(&d,sizeof(struct Doctor),1,fp)){
int countM=0,countF=0,pat[20]={0},i=0;
printf("\n-> Name of doctor: %s %s\n",d.fname,d.lname);
//Reading patients associated with that doctor
fp1=fopen("doctor_patient.txt","r+");
while(fread(&dp,sizeof(struct docPat),1,fp1)){
if(dp.docID==d.id){
pat[i]=dp.patID;
i++;
}
}
fclose(fp1);
//Checking if patient is male or female
fp1=fopen("patient.txt","r+");
int age[100]={0};
while(fread(&p,sizeof(struct Patient),1,fp1)){
if(ispresent(p.id,pat,i)){
age[p.age]++;
}
}
fclose(fp1);
//Printing count age
i=0;
while(i<100){
if(age[i]!=0){
printf("\tPatients of age %d: %d\n",i,age[i]);
}
i++;
}
}
fclose(fp);
}
//##########################################################//
//ASCENDING ORDER FUNCTIONS
void adDate(){
struct Patient p1,p2[20];
struct Doctor d;
struct date dy;
int i=0;
printf("Enter admission date (dd mm yy): ");
scanf("%d %d %d",&dy.dd,&dy.mm,&dy.yy);
FILE *fp;
fp=fopen("patient.txt","r+");
if(fp==NULL){
printf("Unable to open file.\n");
return;
}
while(fread(&p1,sizeof(struct Patient),1,fp)){
if(p1.adate.dd==dy.dd&&p1.adate.mm==dy.mm&&p1.adate.yy==dy.yy){
p2[i]=p1;
i++;
}
}
fclose(fp);
if(i==0){
printf("No patients were admitted on that date.\n");
return;
}
printf("\nASCENDING ORDER BASED ON PATIENT IDs:");
//Sorting based on id
int j,flag,k;
struct Patient temp;
for(k=0;k<i-1;k++){
flag=0;
for(j=0;j<i-1-k;j++){
if(p2[j].id>p2[j+1].id){
temp=p2[j];
p2[j]=p2[j+1];
p2[j+1]=temp;
flag=1;
}
}
if(flag==0) break;
}
for(j=0;j<i;j++){
list(p2[j]);
}
}
void disDate(){
struct Patient p1,p2[20];
struct Doctor d;
struct date dy;
int i=0;
printf("Enter discharge date (dd mm yy): ");
scanf("%d %d %d",&dy.dd,&dy.mm,&dy.yy);
FILE *fp;
fp=fopen("patient.txt","r+");
if(fp==NULL){
printf("Unable to open file.\n");
return;
}
while(fread(&p1,sizeof(struct Patient),1,fp)){
if(p1.ddate.dd==dy.dd&&p1.ddate.mm==dy.mm&&p1.ddate.yy==dy.yy){
p2[i]=p1;
i++;
}
}
fclose(fp);
if(i==0){
printf("No patients were admitted on that date.\n");
return;
}
printf("\nASCENDING ORDER BASED ON PATIENT IDs:");
//Sorting based on id
int j,flag,k;
struct Patient temp;
for(k=0;k<i-1;k++){
flag=0;
for(j=0;j<i-1-k;j++){
if(p2[j].id>p2[j+1].id){
temp=p2[j];
p2[j]=p2[j+1];
p2[j+1]=temp;
flag=1;
}
}
if(flag==0) break;
}
for(j=0;j<i;j++){
list(p2[j]);
}
}
/********************************************************************/
//Just helps to list all the parameters of the structure entered as argument
void list(struct Patient p){
printf("\n---------------------------------");
printf("\nPatient id : %d",p.id);
printf("\nFirst Name : %s",p.fname);
printf("\nLast Name : %s",p.lname);
printf("\nAge : %d",p.age);
printf("\nGender : %c",p.gender);
printf("\nArea : %s",p.area);
printf("\nAdmission Date : %d / %d / %d",p.adate.dd,p.adate.mm,p.adate.yy);
printf("\nDischarge Date : %d / %d / %d",p.ddate.dd,p.ddate.mm,p.ddate.yy);
}
/********************************************************************/
//Arranges patient under each doc in ascending order
void patientsUnderDoc(){
struct docPat dp;
struct Doctor d;
struct Patient p;
FILE *fp,*fp1;
fp=fopen("doctor.txt","r+");
while(fread(&d,sizeof(struct Doctor),1,fp)){
int pat[20]={0},i=0,j=0;
struct Patient p1[20];
printf("\n\n#########################################\n");
printf("--> NAME OF DOCTOR: %s %s DOCTOR ID: %d\n",d.fname,d.lname,d.id);
//Reading patients associated with that doctor
fp1=fopen("doctor_patient.txt","r+");
while(fread(&dp,sizeof(struct docPat),1,fp1)){
if(dp.docID==d.id){
pat[i]=dp.patID;
i++;
}
}
fclose(fp1);
printf("Total patients under consultation are: %d",i);
printf("\nASCENDING ORDER BASED ON PATIENT IDs:--->");
fp1=fopen("patient.txt","r+");
while(fread(&p,sizeof(struct Patient),1,fp1)){
if(present(p,pat,i)){
p1[j]=p;
j++;
}
}
fclose(fp1);
//Sorting based on id
int l,flag,k;
struct Patient temp;
for(k=0;k<j-1;k++){
flag=0;
for(l=0;l<j-1-k;l++){
if(p1[l].id>p1[l+1].id){
temp=p1[l];
p1[l]=p1[l+1];
p1[l+1]=temp;
flag=1;
}
}
if(flag==0) break;
}
for(k=0;k<j;k++){
list(p1[k]);
}
}
fclose(fp);
}
int present(struct Patient p,int pat[],int i){
int j=0;
for(j=0;j<i;j++){
if(p.id==pat[j]) return 1;
}
return 0;
}
/********************************************************************/
//Arranges patient under each male doc in ascending order
void patientsUnderMaleDoc(){
struct docPat dp;
struct Doctor d;
struct Patient p;
FILE *fp,*fp1;
fp=fopen("doctor.txt","r+");
while(fread(&d,sizeof(struct Doctor),1,fp)){
if(d.gender=='M'||d.gender=='m'){
int pat[20]={0},i=0,j=0;
struct Patient p1[20];
printf("\n\n#########################################\n");
printf("--> NAME OF DOCTOR: %s %s \n--> DOCTOR ID: %d\n",d.fname,d.lname,d.id);
printf("--> GENDER: %c\n",d.gender);
//Reading patients associated with that doctor
fp1=fopen("doctor_patient.txt","r+");
while(fread(&dp,sizeof(struct docPat),1,fp1)){
if(dp.docID==d.id){
pat[i]=dp.patID;
i++;
}
}
fclose(fp1);
printf("Total patients under consultation are: %d",i);
printf("\nASCENDING ORDER BASED ON PATIENT IDs:--->");
fp1=fopen("patient.txt","r+");
while(fread(&p,sizeof(struct Patient),1,fp1)){
if(present(p,pat,i)){
p1[j]=p;
j++;
}
}
fclose(fp1);
//Sorting based on id
int l,flag,k;
struct Patient temp;
for(k=0;k<j-1;k++){
flag=0;
for(l=0;l<j-1-k;l++){
if(p1[l].id>p1[l+1].id){
temp=p1[l];
p1[l]=p1[l+1];
p1[l+1]=temp;
flag=1;
}
}
if(flag==0) break;
}
for(k=0;k<j;k++){
list(p1[k]);
}
}
}
fclose(fp);
}
/********************************************************************/
//Arranges patient under each female doc in areawise ascending order
void patientsUnderFemaleDoc(){
struct docPat dp;
struct Doctor d;
struct Patient p;
FILE *fp,*fp1;
fp=fopen("doctor.txt","r+");
while(fread(&d,sizeof(struct Doctor),1,fp)){
if(d.gender=='F'||d.gender=='f'){
int pat[20]={0},i=0,j=0;
struct Patient p1[20];
printf("\n\n#########################################\n");
printf("--> NAME OF DOCTOR: %s %s \n--> DOCTOR ID: %d\n",d.fname,d.lname,d.id);
printf("--> GENDER: %c\n",d.gender);
//Reading patients associated with that doctor
fp1=fopen("doctor_patient.txt","r+");
while(fread(&dp,sizeof(struct docPat),1,fp1)){
if(dp.docID==d.id){
pat[i]=dp.patID;
i++;
}
}
fclose(fp1);
printf("Total patients under consultation are: %d",i);
printf("\nASCENDING ORDER BASED ON PATIENT AREAs:--->");
fp1=fopen("patient.txt","r+");
while(fread(&p,sizeof(struct Patient),1,fp1)){
if(present(p,pat,i)){
p1[j]=p;
j++;
}
}
fclose(fp1);
//Sorting based on id
int l,flag,k;
struct Patient temp;
for(k=0;k<j-1;k++){
flag=0;
for(l=0;l<j-1-k;l++){
if(strcmp(p1[l].area,p1[l+1].area)>0){
temp=p1[l];
p1[l]=p1[l+1];
p1[l+1]=temp;
flag=1;
}
}
if(flag==0) break;
}
for(k=0;k<j;k++){
list(p1[k]);
}
}
}
fclose(fp);
}
/********************************************************************/
//Finds doc having least number of patients and adds entry
void leastp(){
struct docPat dp;
struct Doctor d,d1[20];
int count[20],i=0,min;
struct Patient p;
FILE *fp,*fp1;
fp=fopen("doctor.txt","r+");
while(fread(&d,sizeof(struct Doctor),1,fp)){
fp1=fopen("doctor_patient.txt","r+");
while(fread(&dp,sizeof(struct docPat),1,fp1)){
if(dp.docID==d.id){
count[i]++;
}
}
fclose(fp1);
d1[i]=d;
i++;
}
fclose(fp);
//Sorting doctor name according to ascending order
int l,flag,k,t;
struct Doctor temp;
for(k=0;k<i-1;k++){
flag=0;
for(l=0;l<i-1-k;l++){
if(strcmp(d1[l].fname,d1[l+1].fname)>0){
temp=d1[l];
d1[l]=d1[l+1];
d1[l+1]=temp;
t=count[l];
count[l]=count[l+1];
count[l+1]=t;
flag=1;
}
}
if(flag==0) break;
}
//Finding doctor with minimum patient count
min=0;
for(k=0;k<i;k++){
if(count[k]<count[min]) min=k;
}
printf("Doctor with minimum patients: %s %s",d1[min].fname,d1[min].lname);
printf(" | Doctor ID: %d\n",d1[min].id);
printf("Make an entry with doctor id - %d and a new patient consulting the doctor.\n\n",d1[min].id);
printf("Enter the information again:\n");
inputdp();
}
/*********END OF PROGRAMME**********/