Skip to content

Commit ff4ef90

Browse files
author
OĞUZHAN AGKUŞ
committed
HW05
1 parent d945364 commit ff4ef90

File tree

9 files changed

+1058
-0
lines changed

9 files changed

+1058
-0
lines changed

HW05.pdf

49.2 KB
Binary file not shown.

HW05/data

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Ayse (10,25; 1.5) : orchid, rose, violet
2+
Fatma (-10,-15; 1.3) : clove, rose, daffodil
3+
Murat (-10,8; 1.1) : violet, daffodil, orchid
4+
5+
client1 (0,4): orchid
6+
client2 (1,5): clove
7+
client3 (2,10): daffodil
8+
client4 (4,15): orchid
9+
client5 (8,-21): violet
10+
client6 (-1,21): orchid
11+
client7 (-6,20): rose
12+
client8 (-16,18): rose
13+
client9 (-12,-3): rose
14+
client10 (23,0): violet
15+
client11 (5,1): orchid
16+
client12 (7,-8): violet
17+
client13 (8,-3): clove
18+
client14 (9,8): orchid
19+
client15 (6,5): orchid
20+
client16 (2,6): clove
21+
client17 (-6,-4): daffodil
22+
client18 (-9,-6): daffodil
23+
client19 (-4,16): rose
24+
client20 (-9,26): orchid
25+
client21 (-4,-12): daffodil
26+
client22 (9,13): rose
27+
client23 (12,18): rose
28+
client24 (11,15): orchid

HW05/helper.c

Lines changed: 344 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,344 @@
1+
/* CSE344 - Systems Programming Course - HW05 - Threads and Mutexes
2+
3+
Oğuzhan Agkuş - 161044003 */
4+
5+
#include "helper.h"
6+
7+
/* ---------------------------------------- */
8+
9+
void error_exit(char *message, int error) {
10+
int count;
11+
char buffer[256];
12+
13+
if (error == -1)
14+
count = sprintf(buffer, "%s\n", message);
15+
else
16+
count = sprintf(buffer, "%s: %s\n", message, strerror(error));
17+
18+
write(STDERR_FILENO, buffer, count);
19+
exit(EXIT_FAILURE);
20+
}
21+
22+
/* ---------------------------------------- */
23+
24+
double absolute(double x) {
25+
if (x < 0)
26+
return x * -1;
27+
else
28+
return x;
29+
}
30+
31+
/* ---------------------------------------- */
32+
33+
double cheb_distance(coordinate_t u, coordinate_t v) {
34+
double temp_x, temp_y;
35+
36+
temp_x = abs(u.x - v.x);
37+
temp_y = abs(u.y - v.y);
38+
39+
if (temp_x > temp_y)
40+
return temp_x;
41+
else
42+
return temp_y;
43+
}
44+
45+
/* ---------------------------------------- */
46+
47+
char *flower_type(const data_t *data, size_t i) {
48+
return data->flowers[i];
49+
}
50+
51+
/* ---------------------------------------- */
52+
53+
int flower_index(const data_t *data, const char *flower) {
54+
size_t i;
55+
56+
if (data->flower_count == 0)
57+
return -1;
58+
59+
for (i = 0; i < data->flower_count; i++) {
60+
if (strcmp(data->flowers[i], flower) == 0)
61+
return i;
62+
}
63+
64+
return -1;
65+
}
66+
67+
/* ---------------------------------------- */
68+
69+
size_t read_line(int fd, char *buffer) {
70+
size_t count = 0;
71+
char read_buffer = '\0';
72+
73+
while (read(fd, &read_buffer, 1) == 1) {
74+
if (read_buffer != '\n')
75+
buffer[count++] = read_buffer;
76+
else
77+
break;
78+
}
79+
buffer[count] = '\0';
80+
81+
return count;
82+
};
83+
84+
/* ---------------------------------------- */
85+
86+
data_t *read_data(int fd) {
87+
void *temp;
88+
data_t *data;
89+
size_t florist_size = 4, flower_size = 4, stock_size = 4, bytes_read = 0, info = 0, i, j;
90+
char line[128], *token, *saved, *delimeters = " ,;:()";
91+
int type;
92+
93+
/* ---------- */
94+
95+
data = (data_t *) malloc(sizeof(data_t));
96+
if (data == NULL)
97+
return NULL;
98+
99+
data->florist_count = 0;
100+
data->flower_count = 0;
101+
data->stock_table = NULL;
102+
103+
data->florists = (florist_t *) malloc(florist_size * sizeof(florist_t));
104+
if (data->florists == NULL) {
105+
free(data);
106+
return NULL;
107+
}
108+
109+
data->flowers = (char (*)[]) malloc(sizeof(char[flower_size][STR_LEN]));
110+
if (data->flowers == NULL) {
111+
free(data->florists);
112+
free(data);
113+
return NULL;
114+
}
115+
116+
/* ---------- */
117+
118+
while ((bytes_read = read_line(fd, line)) != 0) {
119+
if (data->florist_count == florist_size) {
120+
florist_size += florist_size;
121+
temp = realloc(data->florists, florist_size * sizeof(florist_t));
122+
if (temp == NULL) {
123+
free(data->florists);
124+
free(data->flowers);
125+
free(data);
126+
return NULL;
127+
}
128+
else
129+
data->florists = (florist_t *) temp;
130+
}
131+
132+
/* ----- */
133+
134+
saved = line;
135+
data->florists[data->florist_count].stock = 0;
136+
data->florists[data->florist_count].flowers = (int *) malloc(stock_size * sizeof(int));
137+
if (data->florists[data->florist_count].flowers == NULL)
138+
free_data(data);
139+
140+
while ((token = strtok_r(saved, delimeters, &saved)) != NULL) {
141+
switch (info) {
142+
case 0:
143+
strcpy(data->florists[data->florist_count].name, token); break;
144+
case 1:
145+
data->florists[data->florist_count].location.x = atof(token); break;
146+
case 2:
147+
data->florists[data->florist_count].location.y = atof(token); break;
148+
case 3:
149+
data->florists[data->florist_count].speed = atof(token); break;
150+
default:
151+
type = flower_index(data, token);
152+
if (type == -1) {
153+
if (data->flower_count == flower_size) {
154+
flower_size += flower_size;
155+
temp = realloc(data->flowers, sizeof(char[flower_size][STR_LEN]));
156+
if (temp == NULL) {
157+
free_data(data);
158+
return NULL;
159+
}
160+
else
161+
data->flowers = (char (*)[]) temp;
162+
}
163+
164+
type = data->flower_count;
165+
strcpy(data->flowers[data->flower_count], token);
166+
data->flower_count += 1;
167+
}
168+
169+
if (data->florists[data->florist_count].stock == stock_size) {
170+
stock_size += stock_size;
171+
temp = realloc(data->florists[data->florist_count].flowers, stock_size * sizeof(int));
172+
if (temp == NULL)
173+
free_data(data);
174+
else
175+
data->florists[data->florist_count].flowers = (int *) temp;
176+
}
177+
178+
data->florists[data->florist_count].flowers[data->florists[data->florist_count].stock] = type;
179+
data->florists[data->florist_count].stock += 1;
180+
break;
181+
}
182+
info += 1;
183+
}
184+
if (data->florists[data->florist_count].stock < stock_size) {
185+
temp = realloc(data->florists[data->florist_count].flowers, data->florists[data->florist_count].stock * sizeof(int));
186+
if (temp == NULL)
187+
free_data(data);
188+
else
189+
data->florists[data->florist_count].flowers = (int *) temp;
190+
}
191+
192+
data->florist_count += 1;
193+
stock_size = 4;
194+
info = 0;
195+
}
196+
197+
/* ---------- */
198+
199+
if (data->florist_count < florist_size) {
200+
temp = realloc(data->florists, florist_size * sizeof(florist_t));
201+
if (temp == NULL) {
202+
free_data(data);
203+
return NULL;
204+
}
205+
else
206+
data->florists = (florist_t *) temp;
207+
}
208+
209+
if (data->flower_count < flower_size) {
210+
temp = realloc(data->flowers, sizeof(char[data->flower_count][STR_LEN]));
211+
if (temp == NULL) {
212+
free_data(data);
213+
return NULL;
214+
}
215+
else
216+
data->flowers = (char (*)[]) temp;
217+
}
218+
219+
/* ---------- */
220+
221+
data->stock_table = (int **) malloc(data->florist_count * sizeof(int *));
222+
if (data->stock_table == NULL) {
223+
free_data(data);
224+
return NULL;
225+
}
226+
227+
for (i = 0; i < data->florist_count; i++) {
228+
data->stock_table[i] = (int *) malloc(data->flower_count * sizeof(int));
229+
if (data->stock_table[i] == NULL) {
230+
for (j = 0; j < i; j++)
231+
free(data->stock_table[j]);
232+
free(data->stock_table);
233+
234+
data->stock_table = NULL;
235+
free_data(data);
236+
}
237+
}
238+
239+
for (i = 0; i < data->florist_count; i++) {
240+
for (j = 0; j < data->flower_count; j++)
241+
data->stock_table[i][j] = 0;
242+
}
243+
244+
for (i = 0; i < data->florist_count; i++) {
245+
for (j = 0; j < data->florists[i].stock; j++)
246+
data->stock_table[i][data->florists[i].flowers[j]] = 1;
247+
}
248+
249+
return data;
250+
}
251+
252+
/* ---------------------------------------- */
253+
254+
void free_data(data_t *data) {
255+
size_t i;
256+
257+
if (data->stock_table != NULL) {
258+
for (i = 0; i < data->florist_count; i++)
259+
free(data->stock_table[i]);
260+
free(data->stock_table);
261+
}
262+
263+
for (i = 0; i < data->florist_count; i++)
264+
free(data->florists[i].flowers);
265+
free(data->florists);
266+
free(data->flowers);
267+
free(data);
268+
}
269+
270+
/* ---------------------------------------- */
271+
272+
void show_data(const data_t *data) {
273+
size_t i, j;
274+
int temp;
275+
276+
printf("Florists:\n");
277+
for (i = 0; i < data->florist_count; i++) {
278+
printf(" -> %s, (%.2lf,%.2lf), %.2lf: ", data->florists[i].name, data->florists[i].location.x,
279+
data->florists[i].location.y, data->florists[i].speed);
280+
for (j = 0; j < data->florists[i].stock - 1; j++)
281+
printf("%s, ", flower_type(data, data->florists[i].flowers[j]));
282+
printf("%s\n", flower_type(data, data->florists[i].flowers[j]));
283+
}
284+
285+
printf("\nFlower types:\n");
286+
for (i = 0; i < data->flower_count; i++)
287+
printf(" -> %s\n", data->flowers[i]);
288+
289+
printf("\nStock table:\n");
290+
for (i = 0; i < data->florist_count; i++) {
291+
printf(" ");
292+
for (j = 0; j < data->flower_count; j++) {
293+
temp = data->stock_table[i][j];
294+
printf("%d ", temp);
295+
}
296+
printf("\n");
297+
}
298+
}
299+
300+
/* ---------------------------------------- */
301+
302+
order_t *read_order(const data_t *data, char *line) {
303+
order_t *order;
304+
char *token, *saved, *delimeters = " ,:()";
305+
size_t info = 0;
306+
307+
order = (order_t *) malloc(sizeof(order_t));
308+
if (order == NULL)
309+
return NULL;
310+
311+
saved = line;
312+
313+
while((token = strtok_r(saved, delimeters, &saved)) != NULL) {
314+
switch (info) {
315+
case 0:
316+
strcpy(order->client, token); break;
317+
case 1:
318+
order->location.x = atof(token); break;
319+
case 2:
320+
order->location.y = atof(token); break;
321+
case 3:
322+
order->request = flower_index(data, token); break;
323+
default:
324+
break;
325+
}
326+
info += 1;
327+
}
328+
329+
return order;
330+
}
331+
332+
/* ---------------------------------------- */
333+
334+
void print_reports(report_t *reports, size_t count) {
335+
size_t i;
336+
char *line = "------------------------------------------------------------";
337+
338+
printf("\n%s\n", line);
339+
printf("%-16s\t%-16s\t%-16s\n", "Florists", "# of sales", "Total time");
340+
printf("%s\n", line);
341+
for (i = 0; i < count; i++)
342+
printf("%-16s\t%-16d\t%.0lfms\n", reports[i].name, reports[i].delivered, reports[i].time);
343+
printf("%s\n", line);
344+
}

0 commit comments

Comments
 (0)