Good Good code,Day Day up

PearLi's Blog

• 随笔 - 50
• 文章 - 42
• 评论 - 147
• 引用 - 0

•

• 积分 - 157672
• 排名 - 161

评论排行榜

-------------------------------------------------------atoi----------------------------------------------------------

int atoi(const char* str)
{
int sign = 0,num = 0;
assert(NULL != str);
while (*str == ' ')
{
str++;
}
if ('-' == *str)
{
sign = 1; str++;
}
while ((*str >= '0') && (*str <= '9'))
{
num = num*10 + (*str - '0'); //就是这一行，将对应字符转化为数字

str++;
}
if(sign == 1)
return -num;
else
return num;
}

-------------------------------------------------atof------------------------------------------------------------

double atof(const char* str)
{
double val = 0.0,power = 0.0;
int sign = 0;
assert(NULL != str);
while (*str == ' ')
{
str++;
}
sign = (*str == '-')? -1 : 1;
if ('-' == *str || '+' == *str)
{
str++;
}
while ((*str >= '0')&&(*str <= '9'))
{
val = val* 10.0 + (*str - '0'); str++;
}
if ('.' == *str)
{
str++;
}
power = 1.0;
while ((*str >= '0')&&(*str <= '9'))
{
val = val* 10.0 + (*str - '0');
power *= 10; str++;
}
return sign*val/power;
}

---------------------------------------------itoa------------------------------------------------------------------------

char* itoa(int val,char* buf,unsigned int radix)
{
char *bufptr;
char *firstdig;
char temp;
unsigned int digval;
assert(buf != NULL);
bufptr = buf;
if (val < 0)
{
*bufptr++ = '-'; val = (unsigned int)(-(int)val);
}
firstdig = bufptr;
do
{
if (digval > 9)
{
*bufptr++ = (char)(digval - 10 + 'a');
}
else
{
*bufptr++ = (char)(digval + '0');
}
} while(val > 0);
*bufptr-- = '\0';//设置字符串末尾，并将指针指向最后一个字符
do //反转字符
{
temp = *bufptr;     *bufptr = *firstdig;  *firstdig = temp;
--bufptr; ++firstdig;
} while(firstdig < bufptr);
return buf;
}

----------------------------------------------itoa.end-------------------------------------------------------------------

-------------------------------------------------------strcmp------------------------------------------------------------

int strcmp(const char* src,const char* dst)
{
int ret = 0;
if (src == dst)
{
return 0;
}
assert(NULL != src);//期待源字符串不为空
if (dst == NULL)
{
return -1;
}
while (!(ret = *(unsigned char*)src - *(unsigned char*)dst)&& *dst)
{
++src,++dst;
}
if (ret < 0)
{
ret = -1;
}
else if (ret > 0)
{
ret = 1;
}
return ret;
}

-------------------------------------------strcpy------------------------------------------------------------------------

char* strcpy(char* dst,const char* src)
{
char* strDst = dst;
assert(src != NULL && dst != NULL);//拷贝空串被认为是没有意义的，使用assert检查
while ((*dst++ = *src++) != '\0')
{
NULL;
}
return strDst;
}

--------------------------------------memcpy-------------------------------------------------------------------------

void* memcpy(void* dst,const void* src,size_t count)
{
char* pbTo = (char*)dst;
char* pbFrom = (char*)src;
assert(dst!= NULL && src != NULL);
assert(pbTo >= pbFrom+count || pbFrom >= pbTo + count);//防止内存重叠(overlap)
while (count-- > 0)
{
*pbTo++ = *pbFrom++;
}
return dst;
}

--------------------------------------memmove---------------------------------------------------------------------

void* memmove(void* dst,const void* src,size_t count)
{
char* pbTo = (char*)dst;
char* pbFrom = (char*)src;
assert(dst != NULL && src != NULL);
if (dst <= src || pbTo >= pbFrom + count)//没有overlap的情况，直接拷贝
{
while (count-- > 0)
{
*pbTo++ = *pbFrom++;
}
}
else
{
pbTo = pbTo + count -1;//overlap的情况，从高位地址向低位拷贝
pbFrom = pbFrom + count -1;
while (count-- > 0)
{
*pbTo-- = *pbFrom--;
}
}
return dst;
}

--------------------------------------memset-------------------------------------------------------------------------

void* memset(void* buf,int c,size_t count)
{
char* pvTo = (char*)buf;
assert(buf != NULL);
while (count-- >0)
{
*pvTo++ = (char)c;
}
return buf;
}

--------------------------------------memset.end---------------------------------------------------------------------

1.《编程精粹—微软编写优质无错C程序秘诀》，Writing Clean Code——Microsoft Techniques for Developing Bug-free C Programs；

2.《高质量C\C++编程指南》，一本书，也是林锐编的，网上广为流传的同名小册子算是精简版；

3.《程序员面试宝典》；

posted on 2009-08-24 15:17 pear_li 阅读(3075) 评论(0)  编辑 收藏 引用 所属分类: C++