diff options
Diffstat (limited to 'simclist-1.5/examples')
| -rw-r--r-- | simclist-1.5/examples/._ex1.c | bin | 0 -> 229 bytes | |||
| -rw-r--r-- | simclist-1.5/examples/._ex2.c | bin | 0 -> 229 bytes | |||
| -rw-r--r-- | simclist-1.5/examples/._ex3.c | bin | 0 -> 229 bytes | |||
| -rw-r--r-- | simclist-1.5/examples/ex1.c | 28 | ||||
| -rw-r--r-- | simclist-1.5/examples/ex2.c | 38 | ||||
| -rw-r--r-- | simclist-1.5/examples/ex3.c | 58 |
6 files changed, 124 insertions, 0 deletions
diff --git a/simclist-1.5/examples/._ex1.c b/simclist-1.5/examples/._ex1.c Binary files differnew file mode 100644 index 0000000..3a559d8 --- /dev/null +++ b/simclist-1.5/examples/._ex1.c diff --git a/simclist-1.5/examples/._ex2.c b/simclist-1.5/examples/._ex2.c Binary files differnew file mode 100644 index 0000000..536216c --- /dev/null +++ b/simclist-1.5/examples/._ex2.c diff --git a/simclist-1.5/examples/._ex3.c b/simclist-1.5/examples/._ex3.c Binary files differnew file mode 100644 index 0000000..89542cf --- /dev/null +++ b/simclist-1.5/examples/._ex3.c diff --git a/simclist-1.5/examples/ex1.c b/simclist-1.5/examples/ex1.c new file mode 100644 index 0000000..13c67c3 --- /dev/null +++ b/simclist-1.5/examples/ex1.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +#include <simclist.h> /* use the SimCList library */ + + +int main() { + list_t mylist; /* declare a list */ + int userval; + + + list_init(& mylist); /* initialize the list */ + + printf("Insert your number: "); + scanf("%d", & userval); + + list_append(& mylist, & userval); /* add an element to the list */ + + printf("The list now holds %u elements.\n", \ + list_size(& mylist)); /* get the size of the list */ + + printf("Your number was: %d\n", \ + * (int*)list_get_at(& mylist, 0)); /* extract the first element of the list */ + + list_destroy(&mylist); + + return 0; +} + diff --git a/simclist-1.5/examples/ex2.c b/simclist-1.5/examples/ex2.c new file mode 100644 index 0000000..3fc2c5e --- /dev/null +++ b/simclist-1.5/examples/ex2.c @@ -0,0 +1,38 @@ +#include <stdio.h> + +#include <simclist.h> + +int main() { + int val; + list_t l; + + list_init(&l); + + /* request to store copies, and provide the metric function */ + list_attributes_copy(&l, list_meter_int32_t, 1); + + printf("Give numbers. Terminate with one negative.\n"); + scanf("%d", &val); + while (val > 0) { + list_append(&l, &val); + scanf("%d", &val); + } + + /* setting the comparator, so the list can sort, find the min, max etc */ + list_attributes_comparator(&l, list_comparator_int32_t); + list_sort(&l, -1); /* sorting the list in descending (-1) order */ + + /* printing out the result */ + printf("Sorted values:\n"); + + list_iterator_start(&l); /* starting an iteration "session" */ + while (list_iterator_hasnext(&l)) { /* tell whether more values available */ + printf("%d\n", *(int *)list_iterator_next(&l)); /* get the next value */ + } + list_iterator_stop(&l); /* starting the iteration "session" */ + + list_destroy(&l); + + return 0; +} + diff --git a/simclist-1.5/examples/ex3.c b/simclist-1.5/examples/ex3.c new file mode 100644 index 0000000..74d3091 --- /dev/null +++ b/simclist-1.5/examples/ex3.c @@ -0,0 +1,58 @@ +#include <stdio.h> + +#include <simclist.h> + +typedef struct { + int x, y; +} point2D; + +typedef struct { + point2D a, b, c, d; +} rectangle; /* custom data type to store in list */ + +/* this function returns the size of elements */ +size_t mymeter(const void *el) { + /* every element has the constant size of a rectangle structure */ + return sizeof(rectangle); +} + +/* + * compare rectangles by area + * + * this function compares two elements: + * <0: a greater than b + * 0: a equivalent to b + * >0: b greater than a + */ +int mycomparator(const void *a, const void *b) { + /* compare areas */ + const rectangle *A = (rectangle *) a; + const rectangle *B = (rectangle *) b; + unsigned int areaA, areaB; + areaA = ((A->c.y - A->b.y) * (A->b.x - A->a.x)); + areaB = ((B->c.y - B->b.y) * (B->b.x - B->a.x)); + return (areaA < areaB) - (areaA > areaB); +} + +int main() { + rectangle rect; + list_t l; + + list_init(&l); + + /* setting the custom spanning function */ + list_attributes_copy(&l, mymeter, 1); + + /* acquire rectangles and insert in list ... */ + + /* setting the custom area comparator */ + list_attributes_comparator(&l, mycomparator); + list_sort(&l, -1); /* sorting by area (descending) */ + + /* [display list ...] */ + + list_destroy(&l); + + return 0; +} + |
