SHARE
TWEET

bsearch kopia

Mary_99 Jun 5th, 2019 68 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 names [ARRAY_SIZE];
  13. }Data;
  14.  
  15. int compereInt(const void *integer1, const void *integer2);
  16. int compereDouble(const void *doubleValue1, const void *doubleValue2) ;
  17. int compereNames(const void *name1, const void *name2);
  18. void* bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
  19.  
  20.  
  21.  
  22. void foundInteger(Data structureToFind, Data structure[])
  23. {
  24.     qsort(structure, STRUCTURES, sizeof(Data),compereInt());
  25.     Data *foundInteger = (Data*)bsearch(&structureToFind, structure, STRUCTURES, sizeof(Data, compereInt);
  26.     printf("Searching for integer %d\n",  structureToFind.integer);
  27.     if(foundInteger == NULL)
  28.     {
  29.         printf("not found");
  30.     }
  31.     else
  32.     {
  33.         printf("Found = %d", (*foundInteger.integer);
  34.     }
  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.  
  54. void foundName(Data structureToFind, Data structure[])
  55. {
  56.     qsort(structure, STRUCTURES, sizeof(Data),compereInt());
  57.     Data *foundName = (Data*)bsearch(&structureToFind, structure, STRUCTURES, sizeof(Data, compereNames);
  58.     printf("Searching for integer %s\n",  structureToFind.names);
  59.     if(foundName == NULL)
  60.     {
  61.         printf("not found");
  62.     }
  63.     else
  64.     {
  65.         printf("Found = %s", (*foundName.name);
  66.     }
  67.    
  68. }
  69.  
  70.  
  71. int main()
  72. {   const void *integer1, const void *integer2
  73.     char names[STRUCTURES][ARRAY_SIZE] = {"arek", "bartek", "krzys", "ola", "ania"};
  74.     int integers[STRUCTURES] = {1, 2, 3, 4, 5};
  75.     double doubles[STRUCTURES] = {1.67, 2.32, 4.23, 6.14, 11.57};
  76.    
  77.     int i;
  78.    
  79.     Data structure[STRUCTURES];
  80.    
  81.     for(i = 0; i < STRUCTURES; i++)
  82.     {
  83.         structure[i].integer = integers[i];
  84.         structure[i].doubleValue = doubles[i];
  85.         structure[i].name = names[i];
  86.     }
  87.    
  88.     Data structureToFind = {1, 1.67, "arek"};
  89.     foundInteger();
  90.     foundDouble();
  91.     foundName();
  92.    
  93.    
  94.    // int *foundInteger = NULL;
  95.    // double *foundDouble = NULL;
  96.    // char *foundName = NULL;
  97.    
  98.     //int currentElement;
  99.        
  100.    /* for(currentElement = 0; currentElement < ELEMENTS; currentElement++)
  101.     {
  102.           foundInteger = (int*)bsearch( &findInteger[currentElement], data.integers, sizeof(data.integers)/sizeof(int), sizeof(int), compereInt);
  103.           foundDouble = (double*)bsearch(&findDouble[currentElement], data.doubleValues, sizeof(data.doubleValues)/sizeof(double), sizeof(double), compereDouble);
  104.           foundName = (char*)bsearch(&findName[currentElement], data.names, sizeof(data.names)/sizeof(*data.names), sizeof(*data.names), compereNames);
  105.          
  106.           if(foundInteger != NULL)
  107.             printf("Found integer %d\n", findInteger[currentElement]);
  108.           else
  109.             printf("Integer %d not found\n", findInteger[currentElement]);
  110.          
  111.           if(foundDouble != NULL)
  112.             printf("Found double %f\n", findDouble[currentElement]);
  113.           else
  114.             printf("Double %f not found\n", findDouble[currentElement]);
  115.          
  116.           if (foundName!= NULL)
  117.             printf("Found name %s\n\n", findName[currentElement]);
  118.           else
  119.             printf("Name %s not found\n\n", findName[currentElement]);
  120.     }*/
  121.    
  122.     return 0;
  123. }
  124.  
  125.  
  126. int compereInt(const void *integer1, const void *integer2)
  127. {
  128.     if (*(int*)integer1 == *(int*)integer2)
  129.     {
  130.         return 0;
  131.     }
  132.     else if (*(int*)integer1 > *(int*)integer2)
  133.     {
  134.         return 1;
  135.     }
  136.     else
  137.     {
  138.         return -1;
  139.     }
  140. }
  141.  
  142. int compereDouble(const void *doubleValue1, const void *doubleValue2)
  143. {
  144.     if (*(double*)doubleValue1 == *(double*)doubleValue2)
  145.     {
  146.         return 0;
  147.     }
  148.     else if (*(double*)doubleValue1 > *(double*)doubleValue2)
  149.     {
  150.         return 1;
  151.     }
  152.     else
  153.     {
  154.         return -1;
  155.     }
  156. }
  157.  
  158. int compereNames(const void *name1, const void *name2)
  159. {
  160.     return strcmp(name1, name2);
  161. }
  162.  
  163. void* bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *))
  164. {
  165.     const void* currentElement = 0;
  166.     int start = 0;
  167.     int end = nmemb;
  168.     int middle;
  169.     int searchElement;
  170.  
  171.     while (start <= end)
  172.     {
  173.         middle = start + (end - start) / 2;
  174.         currentElement = (char*)base + middle* size;
  175.         searchElement = (*compar)(key, currentElement);
  176.  
  177.         if (searchElement == 0)
  178.         {
  179.             return ((void*)currentElement);
  180.         }
  181.         else if (searchElement < 0)
  182.         {
  183.             end = middle - 1;
  184.         }
  185.         else
  186.         {
  187.             start = middle + 1;
  188.         }
  189.     }
  190.  
  191.     return NULL;
  192. }
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