C
x
70
1
2
3
4
5
6
typedef struct box {
7
float x, y, z; //width x, height y, depth z
8
}Box;
9
10
int cmpfunc (const void *a, const void *b) {
11
Box *boxA = (Box *)a;
12
Box *boxB = (Box *)b;
13
return (boxA->x - boxB->x);
14
}
15
16
void maxBoxes (Box *box, int n) {
17
qsort (box, n, sizeof(Box), cmpfunc);
18
19
Box *max_boxes;
20
21
max_boxes = (Box*)malloc(n * sizeof(Box));
22
assert (max_boxes != NULL);
23
24
int n_boxes = 0;
25
26
for (int i=0; i<n; i++) {
27
for (int j=0; j < i; j++) {
28
if (box[i].x < box[j].x && box[i].y < box[j].y && box[i].z < box[j].z) {
29
max_boxes[i] = box[i];
30
n_boxes++;
31
}
32
}
33
}
34
35
printf ("%d boxes\n", n_boxes);
36
37
for (int i=0; i<n_boxes; i++) {
38
printf("Box %d: %.2f %.2f %.2f\n", i, max_boxes[i].x, max_boxes[i].y, max_boxes[i].z);
39
}
40
}
41
42
int main (int argc, char *argv[]) {
43
FILE *filein = stdin;
44
Box *box;
45
int n = 0;
46
47
if (argc > 1) {
48
filein = fopen(argv[1], "r");
49
if (filein == NULL) {
50
fprintf (stderr, "Can not open %s\n", argv[1]);
51
return EXIT_FAILURE;
52
}
53
}
54
55
fscanf (filein, "%d", &n);
56
if (n<=0) {
57
fprintf (stderr, "ERROR: arguments number equal to 0\n");
58
exit (EXIT_FAILURE);
59
}
60
61
box = (Box*)malloc(n * sizeof(Box));
62
assert (box != NULL);
63
64
for (int i=0; i<n; i++) {
65
fscanf (filein, "%f %f %f", &box[i].x, &box[i].y, &box[i].z);
66
}
67
68
maxBoxes (box, n);
69
fclose(filein);
70
}
$ gcc prog.c -Wall -Wextra -std=gnu11
Start
0 boxes
0
Finish