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
| int eq_int(int lhs, int rhs) { return lhs == rhs; }
int lt_int(int lhs, int rhs) { return lhs < rhs; }
int gt_int(int lhs, int rhs) { return lhs > rhs; }
int gz_int(int n) { return n > 0; }
int lz_int(int n) { return n < 0; }
int all_by_pair_int(int const* start, int const* end, int(*cmp)(int, int)) {
if (end - start < 2) {
return 1;
}
--end;
while (start != end && cmp(start[0], start[1])) {
++start;
}
return start == end;
}
int all_int(int const* start, int const* end, int(fct)(int)) {
if (start >= end) {
return 1;
}
while (start != end && fct(*start)) {
++start;
}
return start == end;
}
void test(int const* start, unsigned size) {
printf("[ ");
int i = 0;
for (; i < size; ++i) {
printf("%d ", start[i]);
}
printf("]: ");
int const *end = start + size;
int croissant = all_by_pair_int(start, end, lt_int);
int decroissant = all_by_pair_int(start, end, gt_int);
int egal = all_by_pair_int(start, end, eq_int);
int gz = all_int(start, end, gz_int);
int lz = all_int(start, end, lz_int);
printf("croissant: %d; decroissant: %d; egal: %d, >0: %d, <0: %d\n", croissant, decroissant, egal, gz, lz);
}
int main() {
int c0[] = { 1, 2, 3 };
int c1[] = { -2, -2, -2 };
int c2[] = { 1, 0, -1 };
int c3[] = { 0, 2, 1 };
test(c0, 0);
test(c0, 1);
test(c0, 3);
test(c1, 3);
test(c2, 3);
test(c3, 3);
return 0;
} |
Partager