-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparators.c
79 lines (65 loc) · 1.97 KB
/
comparators.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
#include "comparators.h"
int CompareDates(const void* FDate,const void* SDate){
const char* FirstDate = *(char**) FDate;
const char* SecondDate = *(char**) SDate;
char* temp = malloc((strlen(DATE)+1)*sizeof(char)); ;
char day1[3],day2[3],month1[3],month2[3],year1[5],year2[5];
const char s[2] = "-";
char *token;
strcpy(temp,FirstDate);
token = strtok(temp, s);
for(int i=0;i<3;i++){ // split the first date
if(i==0)
strcpy(day1,token);
if(i==1)
strcpy(month1,token);
if(i==2)
strcpy(year1,token);
token = strtok(NULL, s);
}
strcpy(temp,SecondDate);
token = strtok(temp, s);
for(int i=0;i<3;i++){ // split the second date
if(i==0)
strcpy(day2,token);
if(i==1)
strcpy(month2,token);
if(i==2)
strcpy(year2,token);
token = strtok(NULL, s);
}
free(temp);
if(atoi(year2)<atoi(year1))
return 1;
else if(atoi(year2)>atoi(year1))
return -1;
else if(atoi(year2)==atoi(year1) && atoi(month2)<atoi(month1))
return 1;
else if(atoi(year2)==atoi(year1) && atoi(month2)>atoi(month1))
return -1;
else if(atoi(year2)==atoi(year1) && atoi(month2)==atoi(month1) && atoi(day2)<atoi(day1))
return 1;
else if(atoi(year2)==atoi(year1) && atoi(month2)==atoi(month1) && atoi(day2)>atoi(day1))
return -1;
else if(atoi(year2)==atoi(year1) && atoi(month2)==atoi(month1) && atoi(day2)==atoi(day1))
return 0;
}
int ComparePatientsID(const void* pat1,const void* pat2){
char id1[10],id2[10];
strcpy(id1,((Patient*) pat1)->recordID);
strcpy(id2,((Patient*) pat2)->recordID);
return strcmp(id1,id2);
}
int ComparePatientsDates(const void* pat1,const void* pat2){
return CompareDates(&(((Patient*) pat1)->entryDate),(&((Patient*) pat2)->entryDate));
}
int CompareAges(const void* pat1,const void* pat2){
int age1,age2;
age1 = ((Patient*) pat1)->age;
age2 = ((Patient*) pat2)->age;
if(age1>age2)
return 1;
else if(age1<age2)
return -1;
else return 0;
}