Blame manual/examples/search.c

Packit 6c4009
/* Searching and Sorting Example
Packit 6c4009
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
   This program is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU General Public License
Packit 6c4009
   as published by the Free Software Foundation; either version 2
Packit 6c4009
   of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   This program is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6c4009
   GNU General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU General Public License
Packit 6c4009
   along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
Packit 6c4009
*/
Packit 6c4009
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
/* Define an array of critters to sort. */
Packit 6c4009
Packit 6c4009
struct critter
Packit 6c4009
  {
Packit 6c4009
    const char *name;
Packit 6c4009
    const char *species;
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
struct critter muppets[] =
Packit 6c4009
  {
Packit 6c4009
    {"Kermit", "frog"},
Packit 6c4009
    {"Piggy", "pig"},
Packit 6c4009
    {"Gonzo", "whatever"},
Packit 6c4009
    {"Fozzie", "bear"},
Packit 6c4009
    {"Sam", "eagle"},
Packit 6c4009
    {"Robin", "frog"},
Packit 6c4009
    {"Animal", "animal"},
Packit 6c4009
    {"Camilla", "chicken"},
Packit 6c4009
    {"Sweetums", "monster"},
Packit 6c4009
    {"Dr. Strangepork", "pig"},
Packit 6c4009
    {"Link Hogthrob", "pig"},
Packit 6c4009
    {"Zoot", "human"},
Packit 6c4009
    {"Dr. Bunsen Honeydew", "human"},
Packit 6c4009
    {"Beaker", "human"},
Packit 6c4009
    {"Swedish Chef", "human"}
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
int count = sizeof (muppets) / sizeof (struct critter);
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* This is the comparison function used for sorting and searching. */
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
critter_cmp (const void *v1, const void *v2)
Packit 6c4009
{
Packit 6c4009
  const struct critter *c1 = v1;
Packit 6c4009
  const struct critter *c2 = v2;
Packit 6c4009
Packit 6c4009
  return strcmp (c1->name, c2->name);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Print information about a critter. */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
print_critter (const struct critter *c)
Packit 6c4009
{
Packit 6c4009
  printf ("%s, the %s\n", c->name, c->species);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/*@group*/
Packit 6c4009
/* Do the lookup into the sorted array. */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
find_critter (const char *name)
Packit 6c4009
{
Packit 6c4009
  struct critter target, *result;
Packit 6c4009
  target.name = name;
Packit 6c4009
  result = bsearch (&target, muppets, count, sizeof (struct critter),
Packit 6c4009
		    critter_cmp);
Packit 6c4009
  if (result)
Packit 6c4009
    print_critter (result);
Packit 6c4009
  else
Packit 6c4009
    printf ("Couldn't find %s.\n", name);
Packit 6c4009
}
Packit 6c4009
/*@end group*/
Packit 6c4009
Packit 6c4009
/* Main program. */
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  for (i = 0; i < count; i++)
Packit 6c4009
    print_critter (&muppets[i]);
Packit 6c4009
  printf ("\n");
Packit 6c4009
Packit 6c4009
  qsort (muppets, count, sizeof (struct critter), critter_cmp);
Packit 6c4009
Packit 6c4009
  for (i = 0; i < count; i++)
Packit 6c4009
    print_critter (&muppets[i]);
Packit 6c4009
  printf ("\n");
Packit 6c4009
Packit 6c4009
  find_critter ("Kermit");
Packit 6c4009
  find_critter ("Gonzo");
Packit 6c4009
  find_critter ("Janice");
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}