Code:
#include"header.h"
int main(void) {
record_store records[SIZE], /* array of records to fill, sort, search */
target; /* record to search for in sorted array */
not_found_t *first; /* ptr to alphabetic first node in */
/* linked not-found list */
int count, /* number of records actually read */
position; /* index of found target in records */
/*--- Fill the array of records, sort it, display sorted list. */
fill_record(records, &count);
sort_input(records, count);
puts(" "); puts("Sorted List:");
print_to_screen(records, count); puts(" ");
/*--- Create empty list for not-found target records. */
first = NULL;
/*--- Read targets and search the list; link not-found targets. */
scanf("%d %lf\n", &target.code, &target.percent);
gets(target.string);
while(strcmp(target.string, SEARCH_SENTINEL)) {
position = binary_search(records, count, target);
if(position >= 0)
printf("Found at position %2d: %s\n", position, target.string);
else
first = link_it(first, target);
scanf("%d %lf\n", &target.code, &target.percent);
gets(target.string);
}
/*--- Display sorted list of not-found targets. */
puts(" "); puts("Sorted Linked List:");
list_it(first);
}
/*********************************** FILL RECORDS ****/
void fill_record(record_store records[], int *count) {
int i,code,z;
char temp[LENGTH];
double percent;
*count=0;
scanf("%d %lf\n", &code,&percent);
gets(temp);
for (i=0; ((i<SIZE)&&(strcmp(temp,FILL_SENTINEL)!=0)); i++) {
if (strcmp(temp,FILL_SENTINEL)!=0) {
strcpy(records[i].string,temp);
records[i].code=code;
records[i].percent=percent;
(*count)++;
if(i+1 < SIZE){
scanf("%d %lf\n",&code,&percent);
gets(temp);
}
}
}
}
/****************************** SORT INPUT****/
void sort_input(record_store records[], int count) {
char temp[LENGTH];
char min[LENGTH];
int i,z,minimum;
for (i=0; i<count; i++) {
minimum = i;
strcpy(temp, records[i].string);
strcpy(min, records[i].string);
for (z=i; z<count; z++) {
if (strcmp(min, records[z].string)>0) {
strcpy(min, records[z].string);
minimum=z;
}
}
strcpy(temp, records[minimum].string);
strcpy(records[minimum].string, records[i].string);
strcpy(records[i].string, temp);
}
}
/******************************* PRINT TO SCREEN****/
void print_to_screen(record_store records[], int count) {
int i;
for (i=0; i<count; i++) {
printf("%d %lf ",records[i].code, records[i].percent);
puts(records[i].string);
}
}
/******************************** BINARY SEARCH ****/
int binary_search(record_store records[], int count, record_store target) {
int middle;
int bottom=0, top=count-1;
while (bottom<=top) {
middle=(bottom+top)/2;
if (strcmp(records[middle].string,target.string)==0) {
return(middle);
}
else if ((strcmp(records[middle].string,target.string)>0))
top=(middle-1);
else
bottom=(middle+1);
}
return (-1);
}
/************************************ LINK IT ****/
not_found_t * link_it(not_found_t *first, record_store target) {
not_found_t *ptr;
ptr=malloc(sizeof(not_found_t));
/*if (first==NULL) {
ptr->code=target.code;
ptr->percent=target.percent;
strcpy(ptr->string,target.string);
ptr->next=NULL;
return(ptr);
}
else if (strcmp((ptr->string),(target.string)<0)) {
ptr->next=target;
ptr->code=target.code;
ptr->percent=target.percent;
strcpy(ptr->string,target.string);
ptr->next=first;
target.next=NULL;
}
else {
first->next=target;
ptr->code=target.code;
ptr->percent=target.percent;
strcpy(ptr->string,target.string);
target.next=ptr;
}
*/
}
/************************************ LIST IT ****/
void list_it(not_found_t *first) {
}
Here is the Header File
Code:
/************************************ HEADER.H ****/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 10
#define LENGTH 21
#define FILL_SENTINEL "HALT_FILL"
#define SEARCH_SENTINEL "HALT_SEARCH"
typedef struct {
char string[LENGTH];
int code;
double percent;
} record_store;
typedef struct not_found_s {
char string[LENGTH];
int code;
double percent;
struct not_found_s *next;
} not_found_t;
void fill_record(record_store[], int *);
void sort_input(record_store[], int);
void print_to_screen(record_store[], int);
int binary_search(record_store[], int, record_store);
not_found_t * link_it(not_found_t *, record_store);
void list_it(not_found_t *);