在开源如此盛行的年代,那种无聊的笔试,我真是痛恶之绝!!!
可我们得无奈的面对.........
以下为经典之作,那个考官能有如此慎密思路??不防一试!!!
-------------------------
1、自己写atoi
int atoi(number)
char *number;
{
int n = 0, neg = 0;
while (*number <= ' ' && *number > 0)
++number;
if (*number == '-')
{
neg = 1;
++number;
}
else if (*number == '+')
++number;
while (*number>='0' && *number<='9')
n = (n * 10) + ((*number++) - '0');
return (neg ? -n : n);
}
------------------------------------
2.
#if 1
char *
strstr(s1, s2)
const char *s1;
const char *s2;
{
int l = strlen(s2);
char * p = (char *) s1;
if( l==0 ) return p;
while (p = strchr(p, *s2))
{
if( memcmp(p, s2, l) == 0 )
return p;
p++;
}
return (char *) 0;
}
#else
char *strstr(str1, str2)
char *str1, *str2;
{
char *Sptr, *Tptr;
int len = strlen(str1) -strlen(str2) + 1;
if (*str2)
for (; len > 0; len--, str1++){
if (*str1 != *str2)
continue;
for (Sptr = str1, Tptr = str2; *Tptr != '\0'; Sptr++, Tptr++)
if (*Sptr != *Tptr)
break;
if (*Tptr == '\0')
return (char*) str1;
}
return (char*)0;
}
#endif
-----------------------------
3.自己写fabs
/* Avoid using UNK if possible. */
#ifdef UNK
#if BIGENDIAN
#define MIEEE 1
#else
#define IBMPC 1
#endif
#endif
double fabs(x)
double x;
{
union
{
double d;
short i[4];
} u;
u.d = x;
#ifdef IBMPC
u.i[3] &= 0x7fff;
#endif
#ifdef MIEEE
u.i[0] &= 0x7fff;
#endif
#ifdef DEC
u.i[3] &= 0x7fff;
#endif
#ifdef UNK
if( u.d < 0 )
u.d = -u.d;
#endif
return( u.d );
}
------------------------
4.
static char buf[12];
extern char * ultoa();
char * ltoa(val)
long val;
{
char *p;
int flg = 0;
if( val < 0 ) { flg++; val= -val; }
p = ultoa(val);
if(flg) *--p = '-';
return p;
}
char * ultoa(val)
unsigned long val;
{
char *p;
p = buf+sizeof(buf);
*--p = '\0';
do
{
*--p = '0' + val%10;
val/=10;
}
while(val);
return p;
}
--------------------
5.
#define __MAX_INT_CHARS 7
char *
itoa(i)
int i;
{
static char a[__MAX_INT_CHARS];
char *b = a + sizeof(a) - 1;
int sign = (i < 0);
if (sign)
i = -i;
*b = 0;
do
{
*--b = '0' + (i % 10);
i /= 10;
}
while (i);
if (sign)
*--b = '-';
return b;
}
---------------------------
6:折半查
#include <stdio.h>
static int _bsearch; /* index of element found, or where to
* insert */
char *bsearch(key, base, num, size, cmp)
char *key; /* item to search for */
char *base; /* base address */
int num; /* number of elements */
int size; /* element size in bytes */
int (*cmp) (); /* comparison function */
{
int a, b, c, dir;
a = 0;
b = num - 1;
while (a <= b) {
c = (a + b) >> 1; /* == ((a + b) / 2) */
if ((dir = (*cmp) (key, (base + (c * size))))) {
if (dir < 0)
b = c - 1;
else /* (dir > 0) */
a = c + 1;
} else {
_bsearch = c;
return (base + (c * size));
}
}
_bsearch = b;
return (NULL);
}
太多了,一大把,哎........................