codice:
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct item_s
{
int key;
double value;
};
typedef struct item_s item_t;
static void *binary_search(const void *key, const void *base, size_t num_elem, size_t elem_size, int (*compar)(const void *, const void *));
static int int_cmp(const void* key, const void* elem);
static int str_cmp(const void* key, const void* elem);
void *binary_search(const void *key, const void *base, size_t num_elem, size_t elem_size, int (*compar)(const void *, const void *))
{
assert( key != NULL );
assert( base != NULL );
assert( compar != NULL );
/* TO STUDENTS:
* Remove the following two lines and put here your implementation. */
fprintf(stderr, "To be implemented!\n");
abort();
}
int int_cmp(const void* key, const void* elem)
{
const int* ikey = key;
const int* ielem = elem;
return (*ikey > *ielem) ? 1 : ((*ikey < *ielem) ? -1 : 0);
}
int str_cmp(const void* key, const void* elem)
{
const char* ckey = key;
const char* const* celem = elem;
/*
fprintf(stderr, "key: '%s', candidate: '%s' -> cmp: %d\n", ckey, *celem, strcmp(ckey, *celem));
*/
return strcmp(ckey, *celem);
}
int main()
{
int iary[] = {1, 20, 25, 32, 76, 123};
int ikey = 76;
int* ires = NULL;
int* ires_check = NULL;
const char* sary[] = {"k01","k02","k03","k04","k05","k06"};
const char skey[] = "k01";
char** sres = NULL;
char** sres_check = NULL;
ires = binary_search(&ikey, iary, sizeof iary/sizeof iary[0], sizeof iary[0], int_cmp);
ires_check = bsearch(&ikey, iary, sizeof iary/sizeof iary[0], sizeof iary[0], int_cmp);
assert( ires == ires_check );
printf("Key %d -> %s (element: %d)\n", ikey, (ires != NULL) ? "found" : "not found", *ires);
sres = binary_search(&skey, sary, sizeof sary/sizeof sary[0], sizeof sary[0], str_cmp);
sres_check = bsearch(&skey, sary, sizeof sary/sizeof sary[0], sizeof sary[0], str_cmp);
assert( sres == sres_check );
printf("Key '%s' -> %s (element: '%s')\n", skey, (sres != NULL) ? "found" : "not found", *sres);
return 0;
}
Il codice deve essere implementato nella funzione binary_search dopo il commento TO STUDENTS. Il codice che ho implementato è il seguente:
codice:
void *binary_search(const void *key, const void *base, size_t num_elem, size_t elem_size, int (*compar)(const void *, const void *))
{
size_t i = 0;
size_t j = num_elem-1;
size_t x;
const void *ptr1 = base;
const void *ptr2 = key;
assert( key != NULL );
assert( base != NULL );
assert( compar != NULL );
/* TO STUDENTS:
* Remove the following two lines and put here your implementation.
* fprintf(stderr, "To be implemented!\n");
* abort(); */
while(i <= j)
{
x = (i+j)/2;
if(compar(ptr2, ptr1) < 0)
j = j-1;
else if(compar(ptr2, ptr1) > 0)
i = i+1;
else
return ptr1[x];
}
return NULL;
}
Essendo una funzione void generica che può ricevere come argomenti sia stringhe che interi, compilando il programma mi dà errore a causa del valore di ritorno che non dovrebbe esserci essendo la funzione di tipo void. E' possibile trovare una soluzione per mantenere la funzione generica e restituire comunque il valore dell'array oppure è necessario memorizzare il valore dell'array in una variabile passata per riferimento all funzione?