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