全部博文(158)
分类: WINDOWS
2010-01-05 11:18:40
//定义一个宏,比较两个数a、b的大小,不能使用大于、小于、if语句
#define max(a,b) (abs(a-b)-(a-b))?b:a
int strcmp2(const char *s1, const char*s2)
{
int ret;
for( ; *s1++==*s2++; ret=*s1-*s2 )
{
if(*s1=='\0')
break;
}
if(ret<0)
ret = -1;
else if(ret>0)
ret = 1;
return ret;
}
int strcmp3( char *src, char *sub )
{
int ret;
while( !(ret=*src -*sub) && *sub )
{
++src;
++sub;
}
if(ret<0)
ret = -1;
else if(ret>0)
ret = 1;
return ret;
}
char *strncpy2( char *dest, const char *src, size_t count )
{
char *str=dest;
for( ; (count>0)&&(*dest=*src); count--,dest++,src++ )
;
return str;
}
char * strncpy1( char *dest, const char *src, size_t count )
{
char *address = dest;
assert( (dest!=NULL && src!=NULL && count>1) );
while( count-->0 && (*dest++ = *src++)!='\0' );
return address;
}
char * strcpy1( char *strDest, const char *strSrc )
{
char *address = strDest;
assert( (strDest != NULL) && (strSrc != NULL) );
printf("1=%s 2=%s\n", strDest, strSrc);
while( (*strDest++ = * strSrc++)!='\0' )
{
printf("1=%s 2=%s\n", strDest, strSrc);
}
printf("function str=%s\n", address);
return address;
}
int strlen1( const char *str ) //输入参数const
{
int len=0;
assert( str != NULL ); //断言字符串地址非0
while( (*str++) != '\0' )
{
printf("len=%d str=%s\n", len, str);
len++;
}
printf("\n\nlen=%d str=%s\n", len, str);
return len;
}
int strlen2( const char *src )
{
int len=0;
assert( src!=NULL );
while( *src++!='\0' )
{
len++;
}
return len;
}
int mystrlen( const char *p)
{
int t;
printf("p=[%s]\n", p);
if(*p)
{
t = mystrlen(p+1)+1;
printf("t=%d\n",t);
return t;
}
printf("last!!!!!!!!\n");
return 0;
}
int mystrlen1( const char *p)
{
printf("\nfirst=0x%x last=0x%x\n", p, strchr(p,'\0') );
return strchr(p,'\0')-p;
}
int mystrlen2( const char *p)
{
return (printf("%s",p));
}
int mystrlen3( const char *p)
{
if( 0==p[0] ) return 0;
if( 0==p[1] ) return 1;
if( 0==p[2] ) return 2;
if( 0==p[3] ) return 3;
for(*(int*)p=4;'\0'!=p[*(int*)p]; ++(*(int*)p))
{
}
return *(int*)p;
}
int x(int n)
{
if(n<=3)
{
printf("laset n=%d\n", n);
return 1;
}
else
{
printf("n=%d \n", n);
return x(n-2)+x(n-4)+1;
}
}
int calcone(unsigned char s)
{
int ret=0;
while (s)
{
ret++;
s&=s-1;
}
return ret;
}
size_t func( unsigned char x )
{
printf("0x=0x%x >>1=0x%x\n",x, x>>1);
x = (x & 0x55) + ((x>>1) & 0x55);
printf("0x=0x%x >>2=0x%x\n",x, x>>2);
x = (x & 0x33) + ((x>>2) & 0x33);
printf("0x33=0x%x\n",x);
x = (x & 0x0f) + ((x>>4) & 0x0f);
printf("0x0f=0x%x\n",x);
return x;
}
int MaxSub(const int A[], int N)
{
int ThisSum=0;
int MaxSum=0;
int i;
for(i=0;i<N;i++)
{
ThisSum += A[i];
if(ThisSum > MaxSum)
MaxSum = ThisSum;
else if(ThisSum < 0)
ThisSum = 0;
}
return MaxSum;
}
void shuzu( int a[], int b[][2] )
{
printf("a=%d b=%d!\n", a[0], b[0][0]);
return;
}
int count(int n)
{
if(1==n)
{
printf("1\n");
return 1;
}
else if(2==n)
{
printf("2\n");
return 2;
}
else
{
printf("%d%d%d\n", n, n, n);
return count(n-1)+count(n-2);
}
}
char * ReverseStringSlow(char * aStr)
{
int i,j;
int len = strlen(aStr);
char * ptemp = (char*) malloc (len);
for ( i = len - 1, j = 0; i >= 0; --i, ++j)
{
ptemp[j] = aStr[i];
}
strncpy(aStr, ptemp, len);
free(ptemp);
return aStr;
}
char * ReverseStringFast(char * aStr)
{
int i;
int len = strlen(aStr);
char temp = '\0';
for( i = 0; i < len/2; ++i)
{
printf("i=%d len-i-1=%d\n", i, len-i-1);
temp = aStr[i];
aStr[i] = aStr[len-i-1];
aStr[len-i-1] = temp;
}
return aStr;
}