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
- #define STRUCTURES 5
- typedef struct
- {
- int integer;
- double doubleValue;
- char name [ARRAY_SIZE];
- }Data;
- 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 *));
- void foundInteger(Data structureToFind, Data structure[])
- {
- qsort(structure, STRUCTURES, sizeof(Data),compereInt);
- Data *foundInteger = (Data*)bsearch(&structureToFind, structure, STRUCTURES, sizeof(Data), compereInt);
- printf("Searching for integer %d\n", structureToFind.integer);
- if(foundInteger == NULL)
- {
- printf("not found");
- }
- else
- {
- printf("Found = %d", (*foundInteger).integer);
- }
- }
- void foundDouble(Data structureToFind, Data structure[])
- {
- qsort(structure, STRUCTURES, sizeof(Data),compereDouble);
- Data *foundDouble = (Data*)bsearch(&structureToFind, structure, STRUCTURES, sizeof(Data), compereDouble);
- printf("Searching for double %f\n", structureToFind.doubleValue);
- if(foundDouble == NULL)
- {
- printf("not found");
- }
- else
- {
- printf("Found = %f", (*foundDouble).doubleValue);
- }
- }
- void foundName(Data structureToFind, Data structure[])
- {
- qsort(structure, STRUCTURES, sizeof(Data),compereNames);
- Data *foundName = (Data*)bsearch(&structureToFind, structure, STRUCTURES, sizeof(Data), compereNames);
- printf("Searching for integer %s\n", structureToFind.name);
- if(foundName == NULL)
- {
- printf("not found");
- }
- else
- {
- printf("Found = %s", (*foundName).name);
- }
- }
- int main()
- {
- char names[STRUCTURES][ARRAY_SIZE] = {"arek", "bartek", "krzys", "ola", "ania"};
- int integers[STRUCTURES] = {1, 2, 3, 4, 5};
- double doubles[STRUCTURES] = {1.67, 2.32, 4.23, 6.14, 11.57};
- int i;
- Data structure[STRUCTURES];
- for(i = 0; i < STRUCTURES; i++)
- {
- structure[i].integer = integers[i];
- structure[i].doubleValue = doubles[i];
- //this line structure[i].name = names[i];
- }
- Data structureToFind = {1, 1.67, "arek"};
- foundInteger();
- foundDouble();
- foundName();
- 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* 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