-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab3.c
246 lines (221 loc) · 6.37 KB
/
lab3.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
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mylib.h"
#define MAXLENGTH 64
typedef struct {
int x;
int y;
} coordinate_t;
typedef struct person {
char surname[MAXLENGTH];
coordinate_t location;
struct person *next;
} person_t;
typedef struct hospital {
int hospital_number;
int places_number;
int free_places_number;
int (*dist) (coordinate_t, coordinate_t);
coordinate_t location;
person_t *head_p;
person_t *last_p;
struct hospital *next;
} hospital_t;
char myget_arg(int, char**);
int create_hospital_list(hospital_t**, hospital_t**);
int print_hospital_list(hospital_t*);
int create_person(hospital_t*);
int find_nearest_hospital(hospital_t*, person_t*);
int bind_person_to_hospital(hospital_t*, person_t*, int);
int discharge_patients(hospital_t*);
int calculate_distance(coordinate_t, coordinate_t);
// ------------------------------------------
int main(int argc, char **argv) {
hospital_t *head_h = NULL;
hospital_t *last_h = NULL;
if(myget_arg(argc, argv) == 'h') {
print_manual();
return 0;
}
create_hospital_list(&head_h, &last_h);
printf("Would you like to print hospital list? (y/n) ");
if(confirm_choice()) {
print_hospital_list(head_h);
}
if(create_person(head_h)) {
printf("\n--->>>> Unable to create person...");
}
printf("\nWould you like to print hospital list again? (y/n) ");
if(confirm_choice()) {
print_hospital_list(head_h);
}
printf("\n\n--->>>> Shutting down...\n");
return 0;
}
// ------------------------------------------
char myget_arg(int argc, char **argv) {
if(argc >= 2) {
if(!(strcmp(argv[1], "-h"))) return 'h';
else {
printf("Please, enter the correct argument:\n");
printf("[-h] to print README file\n");
}
}
return '0';
}
// ------------------------------------------
int create_hospital_list(hospital_t **head_h, hospital_t **last_h) {
hospital_t *hospital = NULL;
int i=0;
while(1) {
if(!(hospital = (hospital_t*)malloc(sizeof(hospital_t)))) {
printf("Out of memory...");
return 1;
}
i++;
printf("\n[%dth hospital]", i);
printf("\nEnter the hospital #: ");
hospital->hospital_number = myget_int();
printf("Enter the hospital's location...\n");
printf("x: ");
hospital->location.x = myget_int_range(1, 300);
printf("y: ");
hospital->location.y = myget_int_range(1, 300);
printf("Enter the number of places: ");
hospital->places_number = myget_int();
while(1) {
printf("Enter the number of free places (n<=%d): ", hospital->places_number);
hospital->free_places_number = myget_int();
if((hospital->free_places_number)>(hospital->places_number)) continue;
else break;
}
hospital->dist = calculate_distance;
if(!*head_h) {
(*head_h) = hospital;
} else {
(*last_h)->next = hospital;
}
(*last_h) = hospital;
printf("\nWould you like to add one more hospital? (y/n) ");
if(!confirm_choice()) break;
}
return 0;
}
// ------------------------------------------
int print_hospital_list(hospital_t *hospitals) {
while(hospitals) {
printf("\n\n[Hospital #%d]", hospitals->hospital_number);
printf("\nPlaces: %d", hospitals->places_number);
printf("\nFree places: %d", hospitals->free_places_number);
printf("\nLocation (x,y): %d,%d", hospitals->location.x, hospitals->location.y);
if(hospitals->head_p) {
printf("\nPatients:\n");
while(hospitals->head_p) {
printf("------> %s\n", hospitals->head_p->surname);
hospitals->head_p = hospitals->head_p->next;
}
}
hospitals = hospitals->next;
}
return 0;
}
// ------------------------------------------
int create_person(hospital_t *hospitals) {
person_t *person = NULL;
int i=0, nearest_hospital_number=0;
while(1) {
if(!(person = (person_t*)malloc(sizeof(person_t)))) {
printf("Out of memory...");
return 1;
}
i++;
printf("\n\n[%dth patient]", i);
printf("\nEnter the surname: ");
mygets(person->surname);
printf("Enter the patient's location...\n");
printf("x: ");
person->location.x = myget_int_range(1, 300);
printf("y: ");
person->location.y = myget_int_range(1, 300);
nearest_hospital_number = find_nearest_hospital(hospitals, person);
if(!nearest_hospital_number) {
return 1;
}
bind_person_to_hospital(hospitals, person, nearest_hospital_number);
printf("\n\nWould you like to add one more patient? (y/n) ");
if(!confirm_choice()) break;
}
return 0;
}
// ------------------------------------------
int find_nearest_hospital(hospital_t *hospitals, person_t *person) {
int nearest_hospital_number=0 , shortest=0, distance=0;
while(1) {
while(hospitals) {
if(hospitals->free_places_number == 0) {
printf("\n--->>>> Nearest hospital is #%d, but there is no free places!", hospitals->hospital_number);
printf("\nSearching for other nearby hospitals...");
hospitals = hospitals->next;
continue;
} else {
distance = hospitals->dist(hospitals->location, person->location);
if(distance<shortest || shortest == 0) {
shortest = distance;
nearest_hospital_number = hospitals->hospital_number;
}
hospitals = hospitals->next;
}
}
if(!nearest_hospital_number) {
printf("\n\n--->>>> ALL hospitals is full!");
return 0;
} else {
printf("\nNearest available hospital is #%d", nearest_hospital_number);
return nearest_hospital_number;
break;
}
}
return 0;
}
// ------------------------------------------
int bind_person_to_hospital(hospital_t *hospitals, person_t *person, int nearest_hospital_number) {
int success = 0;
if(!hospitals) {
printf("\n--->>>> Out of memory...");
}
while(hospitals) {
if(hospitals->hospital_number == nearest_hospital_number) {
if(!(hospitals->head_p)) {
hospitals->head_p = person;
} else {
hospitals->last_p->next = person;
}
hospitals->last_p = person;
printf("\nBinding successful! [Person '%s', Hospital #%d]", hospitals->last_p->surname, hospitals->hospital_number);
hospitals->free_places_number--;
success = 1;
break;
}
hospitals = hospitals->next;
}
if(!success) {
printf("\n--->>>> Error! Unable to bind this person to nearest hospital...");
return 1;
}
return 0;
}
// ------------------------------------------
int calculate_distance(coordinate_t a, coordinate_t b) {
double distance = sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
int result = (int)(distance);
return result;
}
// ------------------------------------------
#ifdef __cplusplus
}
#endif