C库实现-stdlib库

手动实现C库stdlib库中部分常用函数,如下代码所示。另外C++ QuickSort实现汇总如下url:

http://blog.csdn.net/hawthorn10/article/details/5376670

#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

int m_atoi ( const char * str );
double m_atof ( const char * str );
long int m_strtol ( const char * str, char ** endptr, int base );

void * m_bsearch ( const void * key, const void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );
void m_qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

int compareints (const void * a, const void * b);
size_t m_partition ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

int main(void)
{
	//printf("%d %lf\n", atoi(" +123abc12"), atof("  -.23e-10"));
	//printf("%d\n", m_atoi(NULL));
	//printf("%d\n", m_atoi(" -123abc12"));
	//printf("%lf\n", m_atof(" -123.12e-2"));
	
	/*
	int values[] = { 10, 20, 25, 40, 90, 100 };
	int key = 10;
	int *pItem = (int*) bsearch (&key, values, 6, sizeof (int), compareints);
	if (pItem != NULL)
		printf ("%d is in the array.\n",*pItem);
	else
		printf ("%d is not in the array.\n",key);
	*/

	/*
	int values[] = { 40, 10, 40, 100, 90, 20, 25 };
	m_qsort (values, 7, sizeof(int), compareints);
	int n;
	for (n=0; n<6; n++)
		printf ("%d ",values[n]);
	putchar('\n');
	*/

#define VNUM 	8
	char strvalues[VNUM][20] = {"some","example","strings","abc","we are the world", "you", "", "hello world"};


	/* sort elements in array: */
	m_qsort (strvalues, VNUM, 20, (int(*)(const void*,const void*)) strcmp);

	int i=0;
	for(; i<VNUM; ++i)
	{
		puts(strvalues[i]);
	}

	/* search for the key: */
	//char key[20] = "example";
	char key[20] = "here";
	char *pItem = (char*) bsearch (key, strvalues, VNUM, 20, (int(*)(const void*,const void*)) strcmp);

	if (pItem!=NULL)
		printf ("%s is in the array.\n",pItem);
	else
		printf ("%s is not in the array.\n",key);
	
	return 0;
}

/*
 * atoi implementation
 */
int m_atoi ( const char * str )
{
	assert(str);
	
	while(isspace(*str)) ++str;
	
	int neg_flag = 0;
	if('+' == *str)
	{
		neg_flag = 0;
		++ str;
	}
	else if('-' == *str)
	{
		neg_flag = 1;
		++ str;
	}
	
	int retv = 0;
	while(isdigit(*str))
	{
		retv = retv * 10 + *str++ - '0';
	}
	
	return neg_flag ? 0-retv : retv;
}

/*
 * atof implementation
 * unlike standard atof(), value overflow is not handled
 */
double m_atof ( const char * str )
{
	assert(str);
	
	while(isspace(*str)) ++str;
	
	int neg_flag = 0;
	if('+' == *str)
	{
		neg_flag = 0;
		++ str;
	}
	else if('-' == *str)
	{
		neg_flag = 1;
		++ str;
	}
	
	double retv = 0;
	while(isdigit(*str))
	{
		retv = retv * 10 + *str++ - '0';
	}
	
	if('.' == *str)	goto dot_sec;
	else if('e' == tolower(*str)) goto exp_sec;
	else goto end;
	
dot_sec:;
	++str;
	double dec = 0;
	double poi = 10;
	while(isdigit(*str))
	{
		dec = dec + (*str++ - '0')/poi;
		poi *= 10;
	}
	
	retv += dec;
	if('e' != tolower(*str)) goto end;
	
exp_sec:;
	++str;
	double exp = 0;
	
	int exp_flag = 0;
	if('+' == *str)
	{
		exp_flag = 0;
		++ str;
	}
	else if('-' == *str)
	{
		exp_flag = 1;
		++ str;
	}
	
	while(isdigit(*str))
	{
		exp = exp * 10 + *str++ - '0';
	}
	
	retv = retv * pow(10, exp_flag ? 0-exp : exp);
	
end:;
	return neg_flag ? 0-retv : retv;
}

/*
 * comparator to compare two integer values
 */
int compareints (const void * a, const void * b)
{
	return ( *(int*)a - *(int*)b );
}

/*
 * bsearch implementation
 */
void * m_bsearch ( const void * key, const void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) )
{
	assert(key && base && comparator);
	
	size_t p = 0, q = num-1;
	while(p <= q)
	{
		void *pos = (char *)base + size * (p + q)/2;
		int comp_res = comparator(key, pos);
		if(0 == comp_res)
		{
			return pos;
		}
		else if(0 > comp_res)
		{
			q = (p + q)/2 - 1;
		}
		else
		{
			p = (p + q)/2 + 1;
		}
	}
	return NULL;
}

/*
 * qsort implementation
 */
void m_qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) )
{
	assert(base && comparator);
	if(1 >= num) return;
	
	size_t part = m_partition(base, num, size, comparator);
	m_qsort(base, part, size, comparator);
	m_qsort((char *)base+size*(part+1), num-part-1, size, comparator);
	return;
}

/*
 * qsort partition implementation
 * we use a simple method to do the partition
 */
size_t m_partition ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) )
{
	assert(base && comparator);
	
	void *pivot = malloc(size);
	memcpy(pivot, base, size);
	
	size_t low=0, high=num-1;
	while(low < high)
	{
		while(high>low && 0<comparator((char *)base+high*size, pivot)) -- high;
		if(low < high)
		{
			memcpy((char *)base+low*size, (char *)base+high*size, size);
			++ low;
		}
		while(low<high && 0>=comparator((char *)base+low*size, pivot)) ++ low;
		if(low < high)
		{
			memcpy((char *)base+high*size, (char *)base+low*size, size);
			--high;
		}
	}
	memcpy((char *)base+low*size, pivot, size);
	free(pivot);
	
	return low;
}


你可能感兴趣的:(C库实现-stdlib库)