search.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Searching and Sorting Example
  2. Copyright (C) 1991-2019 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. /* Define an array of critters to sort. */
  18. struct critter
  19. {
  20. const char *name;
  21. const char *species;
  22. };
  23. struct critter muppets[] =
  24. {
  25. {"Kermit", "frog"},
  26. {"Piggy", "pig"},
  27. {"Gonzo", "whatever"},
  28. {"Fozzie", "bear"},
  29. {"Sam", "eagle"},
  30. {"Robin", "frog"},
  31. {"Animal", "animal"},
  32. {"Camilla", "chicken"},
  33. {"Sweetums", "monster"},
  34. {"Dr. Strangepork", "pig"},
  35. {"Link Hogthrob", "pig"},
  36. {"Zoot", "human"},
  37. {"Dr. Bunsen Honeydew", "human"},
  38. {"Beaker", "human"},
  39. {"Swedish Chef", "human"}
  40. };
  41. int count = sizeof (muppets) / sizeof (struct critter);
  42. /* This is the comparison function used for sorting and searching. */
  43. int
  44. critter_cmp (const void *v1, const void *v2)
  45. {
  46. const struct critter *c1 = v1;
  47. const struct critter *c2 = v2;
  48. return strcmp (c1->name, c2->name);
  49. }
  50. /* Print information about a critter. */
  51. void
  52. print_critter (const struct critter *c)
  53. {
  54. printf ("%s, the %s\n", c->name, c->species);
  55. }
  56. /*@group*/
  57. /* Do the lookup into the sorted array. */
  58. void
  59. find_critter (const char *name)
  60. {
  61. struct critter target, *result;
  62. target.name = name;
  63. result = bsearch (&target, muppets, count, sizeof (struct critter),
  64. critter_cmp);
  65. if (result)
  66. print_critter (result);
  67. else
  68. printf ("Couldn't find %s.\n", name);
  69. }
  70. /*@end group*/
  71. /* Main program. */
  72. int
  73. main (void)
  74. {
  75. int i;
  76. for (i = 0; i < count; i++)
  77. print_critter (&muppets[i]);
  78. printf ("\n");
  79. qsort (muppets, count, sizeof (struct critter), critter_cmp);
  80. for (i = 0; i < count; i++)
  81. print_critter (&muppets[i]);
  82. printf ("\n");
  83. find_critter ("Kermit");
  84. find_critter ("Gonzo");
  85. find_critter ("Janice");
  86. return 0;
  87. }