-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathass1-soln-2020.c
436 lines (369 loc) · 10 KB
/
ass1-soln-2020.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
/* Sample solution comp20005 Assignment 1, 2020.
Alistair Moffat, April 2020
(c) The University of Melbourne, 2020
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
/*******************************************************************/
#define MAX_WAGONS 999
#define MIN_TONNAGE 375
#define MIN_CONCENT 52.5
#define HEADER_LINES 1
#define STAGE1 1
#define STAGE2 2
#define STAGE3 3
#define DEBUG_INT(x) \
printf("line %3d: %s = %d\n", __LINE__, #x, x)
#define DEBUG_DBL(x) \
printf("line %3d: %s = %.2f\n", __LINE__, #x, x)
/* data structure for recording information about one wagon */
typedef struct {
int num;
double tonnes, percent;
} wagon_t;
int read_data(wagon_t *wagons, int maxwagons);
void do_stage1(wagon_t *wagons, int nwagons, int stage);
void do_stage2(wagon_t *wagons, int nwagons, int stage);
void do_stage3(wagon_t *wagons, int nwagons, int stage);
wagon_t sum_wagons(wagon_t *wagons, int nwagons);
void swap_wagons(wagon_t *w1, wagon_t *w2);
void swap_weakest_last(wagon_t *active, int nactive);
void sort_by_num(wagon_t *active, int nactive);
void print_stage(int stage);
void error_and_exit(char *err, int line);
void remove_lines(int n);
void print_S1_line(wagon_t wagon);
void print_S2S3_line(wagon_t *wagons,
int frst, int top, int consignment, int stage);
void print_wagonnums(wagon_t *wagons, int nwagons, int stage);
void blank_line(void);
void ta_daa(void);
/*******************************************************************/
/* traffic control for the whole computation
*/
int
main(int argc, char *argv[]) {
wagon_t wagons[MAX_WAGONS];
int nwagons;
remove_lines(HEADER_LINES);
nwagons = read_data(wagons, MAX_WAGONS);
do_stage1(wagons, nwagons, STAGE1);
do_stage2(wagons, nwagons, STAGE2);
do_stage3(wagons, nwagons, STAGE3);
ta_daa();
return 0;
}
/*******************************************************************/
/* throw away the indicated number of complete lines from the start
of the input stream
*/
void
remove_lines(int n) {
int c;
while ((c=getchar()) != EOF) {
if (c=='\n') {
n--;
if (n==0) return;
}
}
/* if get here, something is wrong */
error_and_exit("Unexpected end of input", __LINE__);
}
/*******************************************************************/
/* read all of the two-number input rows into the supplied array
of struct
*/
int read_data(wagon_t *wagons, int maxwagons) {
double tonnes, percent, num=1;
int nwagons=0;
while (scanf("%lf%lf", &tonnes, &percent)==2) {
if (nwagons==maxwagons) {
error_and_exit("Too much input data", __LINE__);
}
/* ok to add to the array */
wagons[nwagons].num = num++;
wagons[nwagons].tonnes = tonnes;
wagons[nwagons].percent = percent;
nwagons++;
}
/* natural end of function, all ok */
return nwagons;
}
/*******************************************************************/
/* the required stage 1 output:
-- number of data lines read into the array
-- details for the first data line
-- details for the last data line
-- total of input data lines
*/
void
do_stage1(wagon_t *wagons, int nwagons, int stage) {
wagon_t total;
/* this is the only thing that needs to be computed */
total = sum_wagons(wagons, nwagons);
/* and can then generate required output */
print_stage(stage);
print_S1_line(wagons[0]);
print_stage(stage);
print_S1_line(wagons[nwagons-1]);
print_stage(stage);
printf(" whole train, ");
print_S1_line(total);
blank_line();
return;
}
/*******************************************************************/
/* helper function for stage 1, to make sure the formatting of the
three output lines is consistent
*/
void
print_S1_line(wagon_t wagon) {
if (wagon.num>0) {
printf(" wagon %3d, ", wagon.num);
}
printf("tonnes=%7.1f, %%=%5.1f\n", wagon.tonnes, wagon.percent);
return;
}
/*******************************************************************/
/* the required stage 2 output:
-- lists of consignments
-- consignment weights and percentages
-- whether accepted or rejected
-- overall summary of ore wastage
*/
void
do_stage2(wagon_t *wagons, int nwagons, int stage) {
int curr, frst;
double totw;
wagon_t total;
int i;
wagon_t wasted[MAX_WAGONS];
int nwasted=0;
int consignment=1;
frst = 0;
totw = 0.0;
curr = 0;
/* loop considers one wagon at a time */
while (curr<nwagons) {
/* should we add the current wagon to the consignment? */
if (totw < MIN_TONNAGE) {
/* yes... */
totw += wagons[curr].tonnes;
curr++;
}
/* and, with that done, have we reached the desired weight? */
if (totw < MIN_TONNAGE) {
/* nope, so go round the loop and get another wagon */
continue;
}
/* at this point have a possible consignment using
wagons[frst] to wagons[curr-1]? */
total = sum_wagons(wagons+frst, curr-frst);
/* can it be accepted? */
if (total.percent>=MIN_CONCENT) {
/* yes, can accept, hooray */
print_S2S3_line(wagons, frst, curr,
consignment, stage);
consignment += 1;
} else {
/* nope, so need to record the wasted wagons */
for (i=frst; i<curr; i++) {
wasted[nwasted++] = wagons[i];
}
}
/* and either way, start collecting all over again */
frst = curr;
totw = 0.0;
}
/* hmm, there may be a partial consignment also wasted */
if (frst<curr) {
for (i=frst; i<curr; i++) {
wasted[nwasted++] = wagons[i];
}
}
if (nwasted>0) {
total = sum_wagons(wasted, nwasted);
print_stage(stage);
printf(" total dumped, ");
print_S1_line(total);
print_wagonnums(wasted, nwasted, stage);
}
blank_line();
return;
}
/*******************************************************************/
void
print_S2S3_line(wagon_t *wagons, int frst, int top,
int consignment, int stage) {
wagon_t total;
total = sum_wagons(wagons+frst, top-frst);
print_stage(stage);
printf("consignment %2d, ", consignment);
print_S1_line(total);
print_wagonnums(wagons+frst, top-frst, stage);
return;
}
/*******************************************************************/
void
print_wagonnums(wagon_t *wagons, int nwagons, int stage) {
int i;
sort_by_num(wagons, nwagons);
print_stage(stage);
printf(" wagons= ");
for (i=0; i<nwagons; i++) {
printf("%4d", wagons[i].num);
}
blank_line();
return;
}
/*******************************************************************/
/* the required stage 3 output:
-- list of wagons in order delivered
-- details of wagons that get discarded
-- details of consignments formed
-- summary of overall wastage
*/
void
do_stage3(wagon_t *wagons, int nwagons, int stage) {
int curr;
double totw;
wagon_t total;
int i;
wagon_t wasted[MAX_WAGONS];
wagon_t active[MAX_WAGONS];
int nwasted=0;
int nactive=0;
int consignment=1;
totw = 0.0;
curr = 0;
/* loop considers one wagon at a time */
while (curr<nwagons) {
/* should we add the current wagon to the consignment? */
if (totw < MIN_TONNAGE) {
/* yes... */
active[nactive++] = wagons[curr];
totw += wagons[curr].tonnes;
curr++;
}
/* and, with that done, have we reached the desired weight? */
if (totw < MIN_TONNAGE) {
/* nope, so go round the loop and get another wagon */
continue;
}
/* at this point have a possible consignment using
active[0] to active[nactive-1] */
total = sum_wagons(active, nactive);
/* can it be accepted? */
if (total.percent>=MIN_CONCENT) {
/* yes, can accept, hooray */
print_S2S3_line(active, 0, nactive,
consignment, stage);
consignment += 1;
nactive = 0;
totw = 0.0;
} else {
/* nope, so need to remove least-strength active wagon,
by locating it and swapping it to last position */
swap_weakest_last(active, nactive);
nactive -= 1;
wasted[nwasted++] = active[nactive];
totw -= active[nactive].tonnes;
}
/* and either way, start collecting all over again */
}
/* hmm, there may be a partial consignment also wasted */
for (i=0; i<nactive; i++) {
wasted[nwasted++] = active[i];
}
if (nwasted>0) {
total = sum_wagons(wasted, nwasted);
print_stage(stage);
printf(" total dumped, ");
print_S1_line(total);
print_wagonnums(wasted, nwasted, stage);
}
blank_line();
return;
}
/*******************************************************************/
/* sum up the weight and calculate overall percentage concentration
of an array of individual wagons */
wagon_t
sum_wagons(wagon_t *wagons, int nwagons) {
wagon_t total={0.0,0.0};
int i;
for (i=0; i<nwagons; i++) {
total.tonnes += wagons[i].tonnes;
total.percent += wagons[i].tonnes * wagons[i].percent;
}
total.percent /= total.tonnes;
return total;
}
/*******************************************************************/
/* swap over the contents of two wagons in an array
*/
void
swap_wagons(wagon_t *w1, wagon_t *w2) {
wagon_t tmp;
tmp = *w1;
*w1 = *w2;
*w2 = tmp;
return;
}
/*******************************************************************/
/* find the weakest-concentration wagon in the active set and swap
it to be the last one on the active set */
void
swap_weakest_last(wagon_t *active, int nactive) {
int least=0;
double minp;
int i;
assert(nactive>0);
minp=active[0].percent;
for (i=1; i<nactive; i++) {
if (active[i].percent < minp) {
least = i;
minp = active[i].percent;
}
}
swap_wagons(active+least, active+nactive-1);
return;
}
/*******************************************************************/
/* sort an array of wagons into increasing order by wagon number
*/
void
sort_by_num(wagon_t *wagons, int nwagons) {
int i, j;
for (i=1; i<nwagons; i++) {
for (j=i-1; j>0 && wagons[j-1].num > wagons[j].num; j--) {
swap_wagons(wagons+j-1, wagons+j);
}
}
return;
}
/*******************************************************************/
/* print the supplied error message and then exit the program
*/
void
error_and_exit(char *err, int line) {
printf("Problem at line %3d: %s\n", line, err);
exit(EXIT_FAILURE);
}
/* other helper functions */
void print_stage(int stage) {
printf("S%d, ", stage);
return;
}
void
blank_line(void) {
printf("\n");
}
void
ta_daa(void) {
printf("Ta daa!\n");
}
/*******************************************************************/