Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- #define ELEMENTS_NUMBER 5
- typedef struct {
- char *name;
- int inegerValue;
- double doubleValue;
- }Data;
- void searchingForIntegers();
- void searchingForDoubles();
- void searchingForStrings();
- void searchingForStructures();
- void searchingForNames(Data structureToFind, Data structure[]);
- void searchingForIntegersValues(Data structureToFind, Data structure[]);
- void searchingForDoublesValues(Data structureToFind, Data structure[]);
- void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
- int compareIntegers(const void *key, const void *base);
- int compareDouble(const void *key, const void *base);
- int compareString(const void *key, const void *base);
- int compareNames(const void *key, const void *base);
- int compareIntegersValues(const void *key, const void *base);
- int compareDoubleValues(const void *key, const void *base);
- int main(int argc, char* argv[])
- {
- searchingForIntegers();
- printf("\n");
- searchingForDoubles();
- printf("\n");
- searchingForStrings();
- printf("\n");
- searchingForStructures();
- return 0;
- }
- void searchingForIntegers()
- {
- int integerKeys[] = {1, 22, 45, 455, 5555, 34444, 222222};
- int integerArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- int i ;
- for (i = 0; i < sizeof(integerKeys) / sizeof(integerKeys[0]); i++)
- {
- printf("Searching for integer %d\n", integerKeys[i]);
- int* found = (int*)bsearch(&integerKeys[i], integerArray, sizeof(integerArray)/sizeof(integerArray[0]), sizeof(integerArray[0]), compareIntegers);
- if(found == NULL)
- {
- printf("Not Found integer \n\n");
- }
- else
- {
- printf("Found ineger = %d\n\n", *found);
- }
- }
- }
- void searchingForDoubles()
- {
- double doubleKeys[] = {100000, 2.2222, 0.3344, 44.00002, 90909};
- double doubleArray[] = {0.3344, 0.678, 2.2222, 44, 34.567};
- for(int i = 0; i< sizeof(doubleKeys) / sizeof(doubleKeys[0]); i++)
- {
- printf("Searching for double %lf\n", doubleKeys[i]);
- double *found = (double*)bsearch(&doubleKeys[i], doubleArray, sizeof(doubleArray)/sizeof(doubleArray[0]), sizeof(doubleArray[0]), compareDouble);
- if(found == NULL)
- {
- printf("Not Found double \n\n");
- }
- else
- {
- printf("Found double = %lf\n\n", *found);
- }
- }
- }
- void searchingForStrings()
- {
- char* stringKeys[] = {"Marta", "Alex", "Ola", "Tomek", "Krzys"};
- const char *stringArray[] = {"Antek", "Alex", "Kasia", "Tomek"};
- for(int i = 0; i< sizeof(stringKeys) / sizeof(stringKeys[0]); i++)
- {
- printf("Searching for string %s\n", stringKeys[i]);
- char **found = bsearch(&stringKeys[i], stringArray, sizeof(stringArray)/sizeof(stringArray[0]), sizeof(stringArray[0]), compareString);
- if(found == NULL)
- {
- printf("Not Found Name \n\n");
- }
- else
- {
- printf("Found name = %s\n\n", *found);
- }
- }
- }
- void searchingForStructures()
- {
- char* names[ELEMENTS_NUMBER] = {"Ania", "Kasia", "Zuzia", "Monika", "Renata"};
- int integerValues[ELEMENTS_NUMBER] = {1, 20, 200, 2019, 20019};
- double doubleValues[ELEMENTS_NUMBER] = {2000.5, 100000.99, 51000, 70000, 20500.98};
- Data structure [ELEMENTS_NUMBER];
- for(int i = 0; i <ELEMENTS_NUMBER; i++)
- {
- structure[i].name = names[i];
- structure[i].inegerValue = integerValues[i];
- structure[i].doubleValue = doubleValues[i];
- }
- Data structureToFind = {"Zuzia", 200, 20500.98};
- searchingForNames(structureToFind, structure);
- searchingForIntegersValues(structureToFind, structure);
- searchingForDoublesValues(structureToFind, structure);
- }
- void searchingForNames(Data structureToFind, Data structure[])
- {
- qsort(structure, ELEMENTS_NUMBER, sizeof(Data), compareNames);
- Data *foundName = (Data*)bsearch(&structureToFind, structure, ELEMENTS_NUMBER, sizeof(Data), compareNames);
- printf("Searching for names %s\n", structureToFind.name);
- if(foundName == NULL)
- {
- printf("Not found name \n\n");
- }
- else
- {
- printf("Found name = %s\n\n", (*foundName).name);
- }
- }
- void searchingForIntegersValues(Data structureToFind, Data structure[])
- {
- qsort(structure, ELEMENTS_NUMBER, sizeof(Data), compareIntegersValues);
- Data *foundIntegersValues = (Data*)bsearch(&structureToFind, structure, ELEMENTS_NUMBER, sizeof(Data), compareIntegersValues);
- printf("Searching for inegerValue %d\n", structureToFind.inegerValue);
- if(foundIntegersValues == NULL)
- {
- printf("Not found integer\n\n");
- }
- else
- {
- printf("Found integer = %d\n\n", (*foundIntegersValues).inegerValue);
- }
- }
- void searchingForDoublesValues(Data structureToFind, Data structure[])
- {
- qsort(structure, ELEMENTS_NUMBER, sizeof(Data), compareDoubleValues);
- Data *foundDoubleValue = (Data*)bsearch(&structureToFind, structure, ELEMENTS_NUMBER, sizeof(Data), compareDoubleValues);
- printf("Searching for Price %lf\n", structureToFind.doubleValue);
- if(foundDoubleValue == NULL)
- {
- printf("Not found double\n\n");
- }
- else
- {
- printf("Found double = %lf\n\n", (*foundDoubleValue).doubleValue);
- }
- }
- void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *))
- {
- assert(base);
- assert(key);
- int end = nmemb;
- int start = 0;
- while(end > start)
- {
- int middle = start + (end - start) / 2;
- if (compar(key, base + middle * size) == 0)
- {
- return ((void*)base + middle * size);
- }
- else if (compar(key, base + middle * size) < 0)
- {
- end = middle;
- }
- else
- {
- start = middle + 1;
- }
- }
- return NULL;
- }
- int compareIntegers(const void *key, const void *base)
- {
- if((*(int*)key - *(int*)base) > 0)
- {
- return 1;
- }
- else if((*(int*)key - *(int*)base) < 0)
- {
- return -1;
- }
- else
- {
- return 0;
- }
- }
- int compareDouble(const void *key, const void *base)
- {
- if((*(double*)key - *(double*)base) > 0)
- {
- return 1;
- }
- else if((*(double*)key - *(double*)base) < 0)
- {
- return -1;
- }
- else
- {
- return 0;
- }
- }
- int compareString(const void *key, const void *base)
- {
- return strcmp(key, base);
- }
- int compareNames(const void *key, const void *base)
- {
- return strcmp(((Data *) key) -> name, ((Data *) base) -> name);
- }
- int compareIntegersValues(const void *key, const void *base)
- {
- return ((Data *) key) -> inegerValue - ((Data *) base) -> inegerValue;
- }
- int compareDoubleValues(const void *key, const void *base)
- {
- if(((Data *) key) -> doubleValue - ((Data *) base) -> doubleValue > 0)
- {
- return 1;
- }
- else if((((Data *) key) -> doubleValue - ((Data *) base) -> doubleValue) < 0)
- {
- return -1;
- }
- else
- {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement