Advertisement
Mary_99

bsearch do oddania aw wrescie

Jun 9th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5.  
  6.  
  7. #define ELEMENTS_NUMBER 5
  8.  
  9. typedef struct {
  10.     char *name;
  11.     int inegerValue;
  12.     double doubleValue;
  13. }Data;
  14.  
  15. void searchingForIntegers();
  16. void searchingForDoubles();
  17. void searchingForStrings();
  18. void searchingForStructures();
  19.  
  20.  
  21. void searchingForNames(Data structureToFind, Data structure[]);
  22. void searchingForIntegersValues(Data structureToFind, Data structure[]);
  23. void searchingForDoublesValues(Data structureToFind, Data structure[]);
  24.  
  25.  
  26. void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
  27.  
  28. int compareIntegers(const void *key, const void *base);
  29. int compareDouble(const void *key, const void *base);
  30. int compareString(const void *key, const void *base);
  31.  
  32.  
  33. int compareNames(const void *key, const void *base);
  34. int compareIntegersValues(const void *key, const void *base);
  35. int compareDoubleValues(const void *key, const void *base);
  36.  
  37.  
  38.  
  39. int main(int argc, char* argv[])
  40. {
  41.     searchingForIntegers();
  42.     printf("\n");
  43.     searchingForDoubles();
  44.     printf("\n");
  45.     searchingForStrings();
  46.     printf("\n");
  47.     searchingForStructures();
  48.     return 0;
  49. }
  50.  
  51.  
  52. void searchingForIntegers()
  53. {
  54.     int integerKeys[] = {1, 22, 45, 455, 5555, 34444, 222222};
  55.     int integerArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  56.     int i ;
  57.     for (i = 0; i < sizeof(integerKeys) / sizeof(integerKeys[0]); i++)
  58.     {
  59.         printf("Searching for integer %d\n", integerKeys[i]);
  60.         int* found = (int*)bsearch(&integerKeys[i], integerArray, sizeof(integerArray)/sizeof(integerArray[0]), sizeof(integerArray[0]), compareIntegers);
  61.         if(found == NULL)
  62.         {
  63.             printf("Not Found integer \n\n");
  64.         }
  65.         else
  66.         {
  67.             printf("Found ineger = %d\n\n", *found);
  68.         }
  69.     }
  70. }
  71.  
  72.  
  73. void searchingForDoubles()
  74. {
  75.     double doubleKeys[] = {100000, 2.2222, 0.3344, 44.00002, 90909};
  76.     double doubleArray[] = {0.3344, 0.678, 2.2222, 44, 34.567};
  77.    
  78.     for(int i = 0; i< sizeof(doubleKeys) / sizeof(doubleKeys[0]); i++)
  79.     {
  80.         printf("Searching for double %lf\n", doubleKeys[i]);
  81.         double *found = (double*)bsearch(&doubleKeys[i], doubleArray, sizeof(doubleArray)/sizeof(doubleArray[0]), sizeof(doubleArray[0]), compareDouble);
  82.         if(found == NULL)
  83.         {
  84.             printf("Not Found double \n\n");
  85.         }
  86.         else
  87.         {
  88.             printf("Found double = %lf\n\n", *found);
  89.         }
  90.     }
  91. }
  92.  
  93. void searchingForStrings()
  94. {
  95.     char* stringKeys[] = {"Marta", "Alex", "Ola", "Tomek", "Krzys"};
  96.     const char *stringArray[] = {"Antek", "Alex", "Kasia", "Tomek"};
  97.    
  98.     for(int i = 0; i< sizeof(stringKeys) / sizeof(stringKeys[0]); i++)
  99.     {
  100.         printf("Searching for string %s\n", stringKeys[i]);
  101.         char **found = bsearch(&stringKeys[i], stringArray, sizeof(stringArray)/sizeof(stringArray[0]), sizeof(stringArray[0]), compareString);
  102.         if(found == NULL)
  103.         {
  104.             printf("Not Found Name \n\n");
  105.         }
  106.         else
  107.         {
  108.             printf("Found name =  %s\n\n", *found);
  109.         }
  110.     }
  111. }
  112.  
  113.  
  114. void searchingForStructures()
  115. {
  116.     char* names[ELEMENTS_NUMBER] = {"Ania", "Kasia", "Zuzia", "Monika", "Renata"};
  117.     int integerValues[ELEMENTS_NUMBER] = {1, 20, 200, 2019, 20019};
  118.     double doubleValues[ELEMENTS_NUMBER] = {2000.5, 100000.99, 51000, 70000, 20500.98};
  119.     Data structure [ELEMENTS_NUMBER];
  120.    
  121.     for(int i = 0; i  <ELEMENTS_NUMBER; i++)
  122.     {
  123.         structure[i].name = names[i];
  124.         structure[i].inegerValue = integerValues[i];
  125.         structure[i].doubleValue = doubleValues[i];
  126.     }
  127.    
  128.     Data structureToFind = {"Zuzia", 200, 20500.98};
  129.    
  130.     searchingForNames(structureToFind, structure);
  131.     searchingForIntegersValues(structureToFind, structure);
  132.     searchingForDoublesValues(structureToFind, structure);
  133. }
  134.  
  135.  
  136.  
  137. void searchingForNames(Data structureToFind, Data structure[])
  138. {
  139.     qsort(structure, ELEMENTS_NUMBER, sizeof(Data), compareNames);
  140.     Data *foundName = (Data*)bsearch(&structureToFind, structure, ELEMENTS_NUMBER, sizeof(Data), compareNames);
  141.     printf("Searching for names %s\n", structureToFind.name);
  142.     if(foundName == NULL)
  143.         {
  144.             printf("Not found name \n\n");
  145.         }
  146.         else
  147.         {
  148.             printf("Found name = %s\n\n", (*foundName).name);
  149.         }
  150. }
  151.  
  152.  
  153.  
  154. void searchingForIntegersValues(Data structureToFind, Data structure[])
  155. {
  156.     qsort(structure, ELEMENTS_NUMBER, sizeof(Data), compareIntegersValues);
  157.     Data *foundIntegersValues = (Data*)bsearch(&structureToFind, structure, ELEMENTS_NUMBER, sizeof(Data), compareIntegersValues);
  158.     printf("Searching for inegerValue %d\n", structureToFind.inegerValue);
  159.     if(foundIntegersValues == NULL)
  160.         {
  161.             printf("Not found integer\n\n");
  162.         }
  163.         else
  164.         {
  165.             printf("Found integer = %d\n\n", (*foundIntegersValues).inegerValue);
  166.         }
  167. }
  168.  
  169. void searchingForDoublesValues(Data structureToFind, Data structure[])
  170. {
  171.     qsort(structure, ELEMENTS_NUMBER, sizeof(Data), compareDoubleValues);
  172.     Data *foundDoubleValue = (Data*)bsearch(&structureToFind, structure, ELEMENTS_NUMBER, sizeof(Data), compareDoubleValues);
  173.     printf("Searching for Price %lf\n", structureToFind.doubleValue);
  174.     if(foundDoubleValue == NULL)
  175.         {
  176.             printf("Not found double\n\n");
  177.         }
  178.         else
  179.         {
  180.             printf("Found double = %lf\n\n", (*foundDoubleValue).doubleValue);
  181.         }
  182. }
  183.  
  184.  
  185. void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *))
  186. {
  187.     assert(base);
  188.     assert(key);
  189.     int end = nmemb;
  190.     int start = 0;
  191.     while(end > start)
  192.     {
  193.         int middle = start + (end - start) / 2;
  194.         if (compar(key, base + middle * size) == 0)
  195.         {    
  196.             return ((void*)base + middle * size);
  197.         }
  198.         else if  (compar(key, base + middle * size) < 0)
  199.         {
  200.            
  201.             end = middle;
  202.         }
  203.         else
  204.         {
  205.             start = middle + 1;
  206.            
  207.         }
  208.     }
  209.     return NULL;
  210.  
  211. }
  212.  
  213.  
  214. int compareIntegers(const void *key, const void *base)
  215. {
  216.     if((*(int*)key - *(int*)base) > 0)
  217.     {
  218.          return 1;
  219.     }
  220.      else  if((*(int*)key - *(int*)base) < 0)
  221.      {
  222.          return -1;
  223.      }
  224.      else
  225.      {
  226.          return 0;
  227.      }
  228. }
  229.  
  230.  
  231. int compareDouble(const void *key, const void *base)
  232. {
  233.      if((*(double*)key - *(double*)base) > 0)
  234.      {
  235.          return 1;
  236.      }
  237.      else  if((*(double*)key - *(double*)base) < 0)
  238.      {
  239.          return -1;
  240.      }
  241.      else
  242.      {
  243.          return 0;
  244.      }
  245.  }
  246.  
  247. int compareString(const void *key, const void *base)
  248. {
  249.     return strcmp(key, base);
  250. }
  251.  
  252. int compareNames(const void *key, const void *base)
  253. {
  254.     return strcmp(((Data *) key) -> name, ((Data *) base) -> name);
  255. }
  256.  
  257. int compareIntegersValues(const void *key, const void *base)
  258. {
  259.     return ((Data *) key) -> inegerValue - ((Data *) base) -> inegerValue;
  260. }
  261.  
  262.  
  263. int compareDoubleValues(const void *key, const void *base)
  264. {
  265.     if(((Data *) key) -> doubleValue - ((Data *) base) -> doubleValue > 0)
  266.     {
  267.          return 1;
  268.     }
  269.      else  if((((Data *) key) -> doubleValue - ((Data *) base) -> doubleValue) < 0)
  270.      {
  271.          return -1;
  272.      }
  273.      else
  274.      {
  275.          return 0;
  276.      }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement