-
Notifications
You must be signed in to change notification settings - Fork 1
/
HMS.c
214 lines (178 loc) · 5.66 KB
/
HMS.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[50];
char bloodGroup[4];
char severity[10];
} Patient;
void swap(Patient* a, Patient* b) {
Patient temp = *a;
*a = *b;
*b = temp;
}
int partition(Patient arr[], int low, int high) {
Patient pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (strcmp(arr[j].bloodGroup, pivot.bloodGroup) < 0) {
i++;
swap(&arr[i], &arr[j]);
} else if (strcmp(arr[j].bloodGroup, pivot.bloodGroup) == 0) {
if (strcmp(arr[j].severity, pivot.severity) < 0) {
i++;
swap(&arr[i], &arr[j]);
}
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
int part(Patient arr[], int low, int high) {
Patient pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (strcmp(arr[j].severity, pivot.severity) < 0) {
i++;
swap(&arr[i], &arr[j]);
} else if (strcmp(arr[j].severity, pivot.severity) == 0) {
if (strcmp(arr[j].bloodGroup, pivot.bloodGroup) < 0) {
i++;
swap(&arr[i], &arr[j]);
}
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quicksort(Patient arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}
void qc(Patient arr[], int low, int high) {
if (low < high) {
int pi = part(arr, low, high);
qc(arr, low, pi - 1);
qc(arr, pi + 1, high);
}
}
int binarySearch(Patient arr[], int left, int right, char* bloodGroup) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (strcmp(arr[mid].bloodGroup, bloodGroup) == 0)
return mid;
if (strcmp(arr[mid].bloodGroup, bloodGroup) < 0)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
int bs(Patient arr[], int left, int right, char* severity) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (strcmp(arr[mid].severity, severity) == 0)
return mid;
if (strcmp(arr[mid].severity, severity) < 0)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
int main() {
int numPatients;
printf("Enter the number of patients: ");
scanf("%d", &numPatients);
if (numPatients<=0)
{
printf("Invalid number of patients");
return 0;
}
Patient* patients = (Patient*)malloc(numPatients * sizeof(Patient));
printf("\nEnter patient details:\n");
for (int i = 0; i < numPatients; i++) {
printf("Patient %d:\n", i + 1);
printf("Name: ");
scanf("%s", patients[i].name);
printf("Blood Group: ");
scanf("%s", patients[i].bloodGroup);
printf("Severity: ");
scanf("%s", patients[i].severity);
}
// Sort the patients using quicksort
printf("\n--- Menu ---\n");
printf("1. Search for a patient by blood group or severity\n");
printf("2. Exit\n");
int choice;
char searchBloodGroup[4];
char searchSeverity[10];
printf("\nEnter your choice 1 for blood group and 2 for severity: ");
scanf("%d", &choice);
if (choice==1)
{
quicksort(patients, 0, numPatients - 1);
while (1) {
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the blood group to search: ");
scanf("%s", searchBloodGroup);
int result = binarySearch(patients, 0, numPatients - 1, searchBloodGroup);
if (result == -1) {
printf("No patients found with the given blood group.\n");
} else {
printf("Found patient:\n");
printf("Name: %s\n", patients[result].name);
printf("Blood Group: %s\n", patients[result].bloodGroup);
}
break;
case 2:
free(patients);
printf("\nProgram exited.\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
}
else if (choice==2)
{
qc(patients, 0, numPatients - 1);
while (1) {
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the severity to search: ");
scanf("%s", searchSeverity);
int result = bs(patients, 0, numPatients - 1, searchSeverity);
if (result == -1) {
printf("No patients found with the given severity.\n");
} else {
printf("Found patient:\n");
printf("Name: %s\n", patients[result].name);
printf("Severity: %s\n", patients[result].severity);
}
break;
case 2:
free(patients);
printf("\nProgram exited.\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
}
else
{
printf("Invalid choice. Please try again.\n");
}
return 0;
}