SHARE
TWEET

bsearch bez double

Mary_99 Jun 9th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define ARRAY_SIZE 20
  5. #define ELEMENTS 7
  6. #define STRUCTURES 5
  7.  
  8. typedef struct
  9. {  
  10.     int integer;
  11.     double doubleValue;
  12.     char name [ARRAY_SIZE];
  13. }Data;
  14.  
  15.  
  16. int compereInt(const void *integer1, const void *integer2);
  17. int compereDouble(const void *doubleValue1, const void *doubleValue2) ;
  18. int compereNames(const void *name1, const void *name2);
  19. void foundInteger(Data structureToFind, Data structure[]);
  20. void foundDouble(Data structureToFind, Data structure[]);
  21. void foundName(Data structureToFind, Data structure[]);
  22. void* bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
  23.  
  24.  
  25. int main()
  26. {  
  27.     char names[STRUCTURES][ARRAY_SIZE] = {"arek", "bartek", "krzys", "ola", "ania"};
  28.     int integers[STRUCTURES] = {1, 2, 3, 4, 5};
  29.     double doubles[STRUCTURES] = {1.67, 2.32, 4.23, 6.14, 11.57};
  30.    
  31.     int i;
  32.  
  33.     Data structure[STRUCTURES];
  34.    
  35.     for(i = 0; i < STRUCTURES; i++)
  36.     {
  37.         structure[i].integer = integers[i];
  38.         structure[i].doubleValue = doubles[i];
  39.         strcpy(structure[i].name, names[i]);
  40.     }
  41.    
  42.     Data structureToFind = {1, 1.67, "ola"};
  43.      
  44.     foundInteger( structureToFind, structure);
  45.     foundDouble( structureToFind, structure);
  46.     foundName( structureToFind, structure);
  47.      
  48.    
  49.     return 0;
  50. }
  51.  
  52. void foundInteger(Data structureToFind, Data structure[])
  53. {
  54.     qsort(structure, STRUCTURES, sizeof(Data),compereInt);
  55.     Data *foundInteger = (Data*)bsearch(structure, &structureToFind, STRUCTURES, sizeof(Data), compereInt);
  56.     printf("Searching for integer %d\n",  structureToFind.integer);
  57.     if(foundInteger == NULL)
  58.     {
  59.         printf("not found\n");
  60.     }
  61.     else
  62.     {
  63.         printf("Found = %d\n", (*foundInteger).integer);
  64.     }
  65. }
  66.  
  67. void foundDouble(Data structureToFind, Data structure[])
  68. {
  69.     qsort(structure, STRUCTURES, sizeof(Data),compereDouble);
  70.     Data *foundDouble = (Data*)bsearch(structure, &structureToFind, STRUCTURES, sizeof(Data), compereDouble);
  71.     printf("Searching for double %f\n",  structureToFind.doubleValue);
  72.     if(foundDouble == NULL)
  73.     {
  74.         printf("not found\n");
  75.     }
  76.     else
  77.     {
  78.         printf("Found = %f\n", (*foundDouble).doubleValue);
  79.     }
  80. }
  81.  
  82. void foundName(Data structureToFind, Data structure[])
  83. {
  84.     qsort(structure, STRUCTURES, sizeof(Data),compereNames);
  85.     Data *foundName = (Data*)bsearch(structure, &structureToFind, STRUCTURES, sizeof(Data), compereNames);
  86.     printf("Searching for name %s\n",  structureToFind.name);
  87.     if(foundName == NULL)
  88.     {
  89.         printf("not found\n");
  90.     }
  91.     else
  92.     {
  93.         printf("Found = %s\n", (*foundName).name);
  94.     }
  95. }
  96.  
  97.  
  98.  
  99. int compereInt(const void *integer1, const void *integer2)
  100. {
  101.     if (*(int*)integer1 == *(int*)integer2)
  102.     {
  103.         return 0;
  104.     }
  105.     else if (*(int*)integer1 > *(int*)integer2)
  106.     {
  107.         return 1;
  108.     }
  109.     else
  110.     {
  111.         return -1;
  112.     }
  113. }
  114.  
  115. int compereDouble(const void *doubleValue1, const void *doubleValue2)
  116. {
  117.     if (*(double*)doubleValue1 == *(double*)doubleValue2)
  118.     {
  119.         return 0;
  120.     }
  121.     else if (*(double*)doubleValue1 > *(double*)doubleValue2)
  122.     {
  123.         return 1;
  124.     }
  125.     else
  126.     {
  127.         return -1;
  128.     }
  129. }
  130.  
  131. int compereNames(const void *name1, const void *name2)
  132. {
  133.     return strcmp(name1, name2);
  134. }
  135.  
  136. void* bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *))
  137. {
  138.     const void* currentElement = 0;
  139.     int start = 0;
  140.     int end = nmemb;
  141.     int middle;
  142.     int searchElement;
  143.  
  144.     while (start <= end)
  145.     {
  146.         middle = start + (end - start) / 2;
  147.         currentElement = (char*)base + middle* size;
  148.         searchElement = (*compar)(key, currentElement);
  149.  
  150.         if (searchElement == 0)
  151.         {
  152.             return ((void*)currentElement);
  153.         }
  154.         else if (searchElement < 0)
  155.         {
  156.             end = middle - 1;
  157.         }
  158.         else
  159.         {
  160.             start = middle + 1;
  161.         }
  162.     }
  163.  
  164.     return NULL;
  165. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top