Efficient sorted array searching.
Compatibility:This function is compatible with the following targets:
|
|
#include <stdlib.h>
void *bsearch(const void *key, const void *base,  
size_t num, size_t size,
int (*compare) (const void *, const void *))
Parameters for this facility are:
The key argument points to the item you want to search for.
The num argument specifies the number of array elements to search.
The size argument specifies the size of an array element.
// A simple telephone directory manager
// This program accepts a list of names and
// telephone numbers, sorts the list, then
// searches for specified names.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// Maximum number of records in the directory.
#define MAXDIR 40
typedef struct
{
char lname[15]; // keyfield--see comp() function
char fname[15];
char phone[15];
} DIRENTRY; // telephone directory record
int comp(const DIRENTRY *, const DIRENTRY *);
DIRENTRY *look(char *);
DIRENTRY directory[MAXDIR]; // the directory itself
int reccount; // the number of records entered
int main(void)
{
DIRENTRY *ptr;
int lastlen;
char lookstr[15];
printf("Telephone directory program.\n");
printf("Enter blank last name when done.\n");
reccount = 0;
ptr = directory;
do {
printf("\nLast name: ");
gets(ptr->lname);
printf("First name: ");
gets(ptr->fname);
printf("Phone number: ");
gets(ptr->phone);
if ( (lastlen = strlen(ptr->lname)) > 0) {
reccount++;
ptr++;
}
} while ( (lastlen > 0) && (reccount < MAXDIR) );
printf("Thank you. Now sorting. . .\n");
// sort the array using qsort()
qsort(directory, reccount,
sizeof(directory[0]),(void *)comp);
printf("Enter last name to search for,\n");
printf("blank to quit.\n");
printf("\nLast name: ");
gets(lookstr);
while ( (lastlen = strlen(lookstr)) > 0) {
ptr = look(lookstr);
if (ptr != NULL)
printf("%s, %s: %s\n",
ptr->lname,
ptr->fname,
ptr->phone);
else printf("Can't find %s.\n", lookstr);
printf("\nLast name: ");
gets(lookstr);
}
printf("Done.\n");
return 0;
}
int comp(const DIRENTRY *rec1, const DIRENTRY *rec2)
{
return (strcmp((char *)rec1->lname,
(char *)rec2->lname));
}
// search through the array using bsearch()
DIRENTRY *look(char k[])
{
return (DIRENTRY *) bsearch(k, directory, reccount, sizeof(directory[0]), (void *)comp);
}
Output
Telephone directory program.
Enter blank last name when done.
Last name: Mation
First name: Infor
Phone number: 555-1212
Last name: Bell
First name: Alexander
Phone number: 555-1111
Last name: Johnson
First name: Betty
Phone number: 555-1010
Last name:
First name:
Phone number:
Thank you. Now sorting. . .
Enter last name to search for,
blank to quit.
Last name: Mation
Infor, Mation: 555-1212
Last name: Johnson
Johnson, Betty: 555-1010
Last name:
Done.
[ First ] [ Previous ] [ Next ] [ Last ] [ Manuals ]
Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.
Last updated: August 16, 2000