Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define ARRAY_SIZE 20
- #define ELEMENTS 7
- struct data
- {
- int integers[10];
- double doubleValues[8];
- char names[5][ARRAY_SIZE];
- };
- int compereInt(const void *integer1, const void *integer2);
- int compereDouble(const void *doubleValue1, const void *doubleValue2) ;
- int compereNames(const void *name1, const void *name2);
- void* bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
- int main()
- {
- struct data data = {
- .integers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
- .doubleValues = {1.67, 2.32, 4.23, 6.14, 11.57, 13.11, 21.37, 23.23},
- .names = {"arek", "bartek", "krzys", "ola", "ania"}
- };
- int *foundInteger = NULL;
- double *foundDouble = NULL;
- char *foundName = NULL;
- int currentElement;
- int findInteger[ELEMENTS] = {1, 4, 7, 8, 11, 18, 20};
- double findDouble[ELEMENTS] = {2.22,21.37, 6.78, 4.23, 13.12, 23.23, 24.44};
- char findName[ELEMENTS][ARRAY_SIZE] = {"arek", "krystian", "sebastian", "ola", "maciek","bartek", "antek"};
- for(currentElement = 0; currentElement < ELEMENTS; currentElement++)
- {
- foundInteger = (int*)bsearch( &findInteger[currentElement], data.integers, sizeof(data.integers)/sizeof(int), sizeof(int), compereInt);
- foundDouble = (double*)bsearch(&findDouble[currentElement], data.doubleValues, sizeof(data.doubleValues)/sizeof(double), sizeof(double), compereDouble);
- foundName = (char*)bsearch(&findName[currentElement], data.names, sizeof(data.names)/sizeof(*data.names), sizeof(*data.names), compereNames);
- if(foundInteger != NULL)
- printf("Found integer %d\n", findInteger[currentElement]);
- else
- printf("Integer %d not found\n", findInteger[currentElement]);
- if(foundDouble != NULL)
- printf("Found double %f\n", findDouble[currentElement]);
- else
- printf("Double %f not found\n", findDouble[currentElement]);
- if (foundName!= NULL)
- printf("Found name %s\n\n", findName[currentElement]);
- else
- printf("Name %s not found\n\n", findName[currentElement]);
- }
- return 0;
- }
- int compereInt(const void *integer1, const void *integer2)
- {
- if (*(int*)integer1 == *(int*)integer2)
- {
- return 0;
- }
- else if (*(int*)integer1 > *(int*)integer2)
- {
- return 1;
- }
- else
- {
- return -1;
- }
- }
- int compereDouble(const void *doubleValue1, const void *doubleValue2)
- {
- if (*(double*)doubleValue1 == *(double*)doubleValue2)
- {
- return 0;
- }
- else if (*(double*)doubleValue1 > *(double*)doubleValue2)
- {
- return 1;
- }
- else
- {
- return -1;
- }
- }
- int compereNames(const void *name1, const void *name2)
- {
- return strcmp(name1, name2);
- }
- void* bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *))
- {
- /*
- const void *key Poszukiwany klucz.
- const void *base Wskaźnik na posortowaną tablicę, która ma zostać przeszukana.
- size_t nmemb Liczba elementów w tablicy.
- size_t size Liczba bajtów zajmowanych przez jeden element tablicy.
- int (*compare ) ( const void *, const void *) Funkcja porównująca klucze. Do pierwszego argumentu przedmiotowej funkcji trafia wskaźnik na element poszukiwany key, natomiast do drugiego wskaźnik na element tablicy base z którym ma nastąpić porównanie.*/
- const void* currentElement = 0;
- int start = 0;
- int end = nmemb;
- int middle;
- int searchElement;
- while (start <= end)
- {
- middle = start + (end - start) / 2;
- currentElement = (char*)base + middle* size;
- searchElement = (*compar)(key, currentElement);
- if (searchElement == 0)
- {
- return ((void*)currentElement);
- }
- else if (searchElement < 0)
- {
- end = middle - 1;
- }
- else
- {
- start = middle + 1;
- }
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement