SHARE
TWEET

bserach niue dziala

Mary_99 Jun 9th, 2019 73 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. int main()
  23. {  
  24.     char names[STRUCTURES][ARRAY_SIZE] = {"arek", "bartek", "krzys", "ola", "ania"};
  25.     int integers[STRUCTURES] = {1, 2, 3, 4, 5};
  26.     double doubles[STRUCTURES] = {1.67, 2.32, 4.23, 6.14, 11.57};
  27.    
  28.     int i;
  29.    
  30.     Data structure[STRUCTURES];
  31.    
  32.     for(i = 0; i < STRUCTURES; i++)
  33.     {
  34.         structure[i].integer = integers[i];
  35.         structure[i].doubleValue = doubles[i];
  36.         strcpy(structure[i].name, names[i]);
  37.     }
  38.    
  39.     Data structureToFind = {1, 1.67, "arek"};
  40.      
  41.     foundInteger(integers, structureToFind);
  42.     foundDouble(doubles, structureToFind);
  43.     foundName( names, structureToFind);
  44.    
  45.    
  46.     return 0;
  47. }
  48.  
  49. void foundInteger(Data structureToFind, Data structure[])
  50. {
  51.     qsort(structure, STRUCTURES, sizeof(Data),compereInt);
  52.     Data *foundInteger = (Data*)bsearch(&structureToFind, structure, STRUCTURES, sizeof(Data), compereInt);
  53.     printf("Searching for integer %d\n",  structureToFind.integer);
  54.     if(foundInteger == NULL)
  55.     {
  56.         printf("not found\n");
  57.     }
  58.     else
  59.     {
  60.         printf("Found = %d\n", (*foundInteger).integer);
  61.     }
  62. }
  63.  
  64. void foundDouble(Data structureToFind, Data structure[])
  65. {
  66.     qsort(structure, STRUCTURES, sizeof(Data),compereDouble);
  67.     Data *foundDouble = (Data*)bsearch(&structureToFind, structure, STRUCTURES, sizeof(Data), compereDouble);
  68.     printf("Searching for double %f\n",  structureToFind.doubleValue);
  69.     if(foundDouble == NULL)
  70.     {
  71.         printf("not found\n");
  72.     }
  73.     else
  74.     {
  75.         printf("Found = %f\n", (*foundDouble).doubleValue);
  76.     }
  77. }
  78.  
  79. void foundName(Data structureToFind, Data structure[])
  80. {
  81.     qsort(structure, STRUCTURES, sizeof(Data),compereNames);
  82.     Data *foundName = (Data*)bsearch(&structureToFind, structure, STRUCTURES, sizeof(Data), compereNames);
  83.     printf("Searching for name %s\n",  structureToFind.name);
  84.     if(foundName == NULL)
  85.     {
  86.         printf("not found\n");
  87.     }
  88.     else
  89.     {
  90.         printf("Found = %s\n", (*foundName).name);
  91.     }
  92. }
  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