Advertisement
Monika__

bsearch- pray for me

Jun 4th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. typedef struct
  8. {
  9.   char name[21];
  10.   float balance;
  11.   int accNo;
  12. }
  13. CustAccount;
  14.  
  15. CustAccount accounts[] = {
  16.   {"White", 123.5, 1234},
  17.   {"Barry", -12.75, 1235},
  18.   {"Agnew", 24.55, 1236},
  19.   {"Achison", 12.45, 1237},
  20.   {"Barry", -38.49, 1238},
  21.   {"Brown", -18.23, 1239},
  22.   {"Cunningham", 19.32, 1240}
  23. };
  24.  
  25. void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compare)(const void *, const void *));
  26. int compAccNo (const void *p1, const void *p2);
  27. int compName (const void *p1, const void *p2);
  28. int compBalance(const void *p1, const void *p2);
  29. void printAccount (CustAccount * a);
  30. CustAccount * findByName (const char *name);
  31. CustAccount * findByAccNo (int accNo);
  32. CustAccount * findBalance (float balance);
  33.  
  34.    
  35. /*
  36. void displayAccount (CustAccount * a, unsigned int n)
  37. {
  38.   unsigned int i;
  39.   printf ("Printing array\n");
  40.   for (i = 0; i < n; i++)
  41.     printAccount (a + i);
  42. }
  43. */
  44.  
  45. int main (int argc, char *argv[])
  46. {
  47.   char searchingForName[] = "Barry";
  48.   int searchingForAccNo = 1238;
  49.   float searchingForBalance = 123.5;
  50.  
  51.  
  52.   qsort (accounts, sizeof (accounts) / sizeof (*accounts), sizeof (*accounts), compName);
  53.   printf ("Searching for %s:\n", searchingForName);
  54.   printAccount (findByName(searchingForName));
  55.  
  56.  
  57.   qsort (accounts, sizeof (accounts) / sizeof (*accounts), sizeof (*accounts), compAccNo);
  58.   printf ("Searching for %d:\n", searchingForAccNo);
  59.   printAccount (findByAccNo(searchingForAccNo));
  60.  
  61.   qsort (accounts, sizeof (accounts) / sizeof (*accounts), sizeof (*accounts), compBalance);
  62.   printf ("Searching for %f:\n", searchingForBalance);
  63.   printAccount (findBalance(searchingForBalance));
  64.  
  65.   return 0;
  66. }
  67.  
  68. void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compare)(const void *, const void *))
  69. {
  70.     const void* currentElement = 0;
  71.     int start = 0;
  72.     int end = nmemb;
  73.     int middle;
  74.     int searchElement;
  75.  
  76.     while (start <= end)
  77.     {
  78.         middle = start + (end - start) / 2;
  79.         currentElement = (char*)base + middle* size;
  80.         searchElement = (*compare)(key, currentElement);
  81.  
  82.         if (searchElement == 0)
  83.         {
  84.             return ((void*)currentElement);
  85.         }
  86.         else if (searchElement < 0)
  87.         {
  88.             end = middle - 1;
  89.         }
  90.         else
  91.         {
  92.             start = middle + 1;
  93.         }
  94.     }
  95.  
  96.     return NULL;
  97. }
  98.  
  99. int compAccNo (const void *p1, const void *p2)
  100. {
  101.   return ((CustAccount *) p1)->accNo - ((CustAccount *) p2)->accNo;
  102. }
  103.  
  104.  
  105. int compName (const void *p1, const void *p2)
  106. {
  107.   return strcmp (((CustAccount *) p1)->name, ((CustAccount *) p2)->name);
  108. }
  109.  
  110.  
  111. int compBalance(const void *p1, const void *p2)
  112. {  
  113.     if (fabs(((CustAccount *) p1)->balance - ((CustAccount *) p2)->balance) < 0.00001)
  114.     {
  115.         return 0;    
  116.     }
  117.     else
  118.     {
  119.         if ((((CustAccount *) p1)->balance - ((CustAccount *) p2)->balance) > 0)
  120.         {
  121.             return 1;
  122.         } else {
  123.             return -1;
  124.         }
  125.     }
  126.  
  127. }
  128.  
  129. void printAccount (CustAccount * a)
  130. {
  131.   if (a)
  132.     printf ("Name: %20s\t AccNo: %d\t Balance: %f\n", a->name,
  133.         a->accNo, a->balance);
  134.  
  135. }
  136.  
  137. CustAccount * findByName (const char *name)
  138. {
  139.   CustAccount reference;
  140.   strcpy (reference.name, name);
  141.   if (bsearch(&reference, accounts, sizeof (accounts) / sizeof (*accounts), sizeof (*accounts), compName) == NULL)
  142.   {
  143.       printf("No accound matching criteria: %s\n", name);
  144.       return NULL;
  145.   } else {
  146.       return bsearch (&reference, accounts, sizeof (accounts) / sizeof (*accounts), sizeof (*accounts), compName);
  147.   }
  148. }
  149.  
  150. CustAccount * findByAccNo (int accNo)
  151. {
  152.   CustAccount reference;
  153.   reference.accNo = accNo;
  154.   if (bsearch(&reference, accounts, sizeof (accounts) / sizeof (*accounts), sizeof (*accounts), compAccNo) == NULL)
  155.   {
  156.       printf("No accound matching criteria: %d\n", accNo);
  157.       return NULL;
  158.   } else {
  159.       return bsearch (&reference, accounts, sizeof (accounts) / sizeof (*accounts), sizeof (*accounts), compAccNo);
  160.   }
  161. }
  162.  
  163. CustAccount * findBalance (float balance)
  164. {
  165.     CustAccount reference;
  166.     reference.balance = balance;
  167.     if (bsearch(&reference, accounts, sizeof (accounts) / sizeof (*accounts), sizeof (*accounts), compBalance) == NULL)
  168.     {
  169.         printf("No accound matching criteria: %f\n", balance);
  170.         return NULL;
  171.     } else {
  172.         return bsearch (&reference, accounts, sizeof (accounts) / sizeof (*accounts), sizeof (*accounts), compBalance);
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement