SHARE
TWEET

bserach he he

Mary_99 Jun 9th, 2019 63 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* bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
  20.  
  21.  
  22.  
  23. void foundInteger(Data structureToFind, Data structure[])
  24. {
  25.     qsort(structure, STRUCTURES, sizeof(Data),compereInt);
  26.     Data *foundInteger = (Data*)bsearch(&structureToFind, structure, STRUCTURES, sizeof(Data), compereInt);
  27.     printf("Searching for integer %d\n",  structureToFind.integer);
  28.     if(foundInteger == NULL)
  29.     {
  30.         printf("not found");
  31.     }
  32.     else
  33.     {
  34.         printf("Found = %d", (*foundInteger).integer);
  35.     }
  36. }
  37.  
  38. void foundDouble(Data structureToFind, Data structure[])
  39. {
  40.     qsort(structure, STRUCTURES, sizeof(Data),compereDouble);
  41.     Data *foundDouble = (Data*)bsearch(&structureToFind, structure, STRUCTURES, sizeof(Data), compereDouble);
  42.     printf("Searching for double %f\n",  structureToFind.doubleValue);
  43.     if(foundDouble == NULL)
  44.     {
  45.         printf("not found");
  46.     }
  47.     else
  48.     {
  49.         printf("Found = %f", (*foundDouble).doubleValue);
  50.     }
  51. }
  52.  
  53. void foundName(Data structureToFind, Data structure[])
  54. {
  55.     qsort(structure, STRUCTURES, sizeof(Data),compereNames);
  56.     Data *foundName = (Data*)bsearch(&structureToFind, structure, STRUCTURES, sizeof(Data), compereNames);
  57.     printf("Searching for integer %s\n",  structureToFind.name);
  58.     if(foundName == NULL)
  59.     {
  60.         printf("not found");
  61.     }
  62.     else
  63.     {
  64.         printf("Found = %s", (*foundName).name);
  65.     }
  66. }
  67.  
  68. int main()
  69. {  
  70.     char names[STRUCTURES][ARRAY_SIZE] = {"arek", "bartek", "krzys", "ola", "ania"};
  71.     int integers[STRUCTURES] = {1, 2, 3, 4, 5};
  72.     double doubles[STRUCTURES] = {1.67, 2.32, 4.23, 6.14, 11.57};
  73.    
  74.     int i;
  75.    
  76.     Data structure[STRUCTURES];
  77.    
  78.     for(i = 0; i < STRUCTURES; i++)
  79.     {
  80.         structure[i].integer = integers[i];
  81.         structure[i].doubleValue = doubles[i];
  82.  //this line       structure[i].name = names[i];
  83.     }
  84.    
  85.     Data structureToFind = {1, 1.67, "arek"};
  86.    
  87.     foundInteger();
  88.     foundDouble();
  89.     foundName();
  90.    
  91.    
  92.     return 0;
  93. }
  94.  
  95.  
  96. int compereInt(const void *integer1, const void *integer2)
  97. {
  98.     if (*(int*)integer1 == *(int*)integer2)
  99.     {
  100.         return 0;
  101.     }
  102.     else if (*(int*)integer1 > *(int*)integer2)
  103.     {
  104.         return 1;
  105.     }
  106.     else
  107.     {
  108.         return -1;
  109.     }
  110. }
  111.  
  112. int compereDouble(const void *doubleValue1, const void *doubleValue2)
  113. {
  114.     if (*(double*)doubleValue1 == *(double*)doubleValue2)
  115.     {
  116.         return 0;
  117.     }
  118.     else if (*(double*)doubleValue1 > *(double*)doubleValue2)
  119.     {
  120.         return 1;
  121.     }
  122.     else
  123.     {
  124.         return -1;
  125.     }
  126. }
  127.  
  128. int compereNames(const void *name1, const void *name2)
  129. {
  130.     return strcmp(name1, name2);
  131. }
  132.  
  133. void* bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *))
  134. {
  135.     const void* currentElement = 0;
  136.     int start = 0;
  137.     int end = nmemb;
  138.     int middle;
  139.     int searchElement;
  140.  
  141.     while (start <= end)
  142.     {
  143.         middle = start + (end - start) / 2;
  144.         currentElement = (char*)base + middle* size;
  145.         searchElement = (*compar)(key, currentElement);
  146.  
  147.         if (searchElement == 0)
  148.         {
  149.             return ((void*)currentElement);
  150.         }
  151.         else if (searchElement < 0)
  152.         {
  153.             end = middle - 1;
  154.         }
  155.         else
  156.         {
  157.             start = middle + 1;
  158.         }
  159.     }
  160.  
  161.     return NULL;
  162. }
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