C\C++常用库函数实现
来源:互联网 发布:软件著作权申请流程 编辑:程序博客网 时间:2024/05/17 01:47
1.//整数转换成字符串itoa函数的实现
#include "stdafx.h"
#include <iostream>
using namespace std;
void itoaTest(int num,char str[] )
{
int sign = num,i = 0,j = 0;
char temp[11];
if(sign<0)//判断是否是一个负数
{
num = -num;
};
do
{
temp[i] = num%10+'0';
num/=10;
i++;
}while(num>0);
if(sign<0)
{
temp[i++] = '-';
}
temp[i] = '\0';
i--;
while(i>=0)
{
str[j] = temp[i];
j++;
i--;
}
str[j] = '\0';
}
;***************************************************************************************************************
; strlen returns the length of a null-terminated string in bytes, not including the null byte itself.
; Algorithm:
; int strlen (const char * str)
; {
; int length = 0;
;
; while( *str++ )
; ++length;
;
; return( length );
; }
;***************************************************************************************************************
; memcpy() copies a source memory buffer to a destination buffer.
; Overlapping buffers are not treated specially, so propogation may occur.
; Algorithm:
; void * memcpy(void * dst, void * src, size_t count)
; {
; void * ret = dst;
; /*
; * copy from lower addresses to higher addresses
; */
; while (count--)
; *dst++ = *src++;
;
; return(ret);
; }
2. //字符串转换成整数atoi函数的实现
int atoiTest(char s[])
{
int i = 0,sum = 0,sign; //输入的数前面可能还有空格或制表符应加判断
while(' '==s[i]||'\t'==s[i])
{
i++;
}
sign = ('-'==s[i])?-1:1;
if('-'==s[i]||'+'==s[i])
{
i++;
}
while(s[i]!='\0')
{
sum = s[i]-'0'+sum*10;
i++;
}
return sign*sum;
}
4.//判断输入的是否是一个回文字符串
#include "stdafx.h"
#include <string.h>
#include <iostream>
using namespace std;
//方法一:借助数组
bool isPalindrome(char *input)
{
char s[100];
strcpy(s,input);
int length = strlen(input);
int begin = 0,end = length-1;
while(begin<end)
{
if(s[begin]==s[end])
{
begin++;
end--;
}
else
{
break;
}
}
if(begin<end)
{
return false;
}
else
{
return true;
}
}
//方法二:使用指针
bool isPalindrome2(char *input)
{
if(input==NULL)
return false;
char *begin = input;
char *end = begin+strlen(input)-1;
while(begin<end)
{
if(*begin++!=*end--)
return false;
}
return true;
}
int main(int argc, char* argv[])
{
char *s ="1234554321";
if(isPalindrome(s))
{
cout<<"True"<<endl;
}
else
{
cout<<"Fasle"<<endl;
}
if(isPalindrome2(s))
{
cout<<"True"<<endl;
}
else
{
cout<<"Fasle"<<endl;
}
cin.get();
return 0;
}
;***************************************************************************************************************
; memmove() copies a source memory buffer to a destination memory buffer.
; This routine recognize overlapping buffers to avoid propogation.
; For cases where propogation is not a problem, memcpy() can be used.
; Algorithm:
; void * memmove(void * dst, void * src, size_t count)
; {
; void * ret = dst;
; if (dst <= src || dst >= (src + count)) {
; /*
; * Non-Overlapping Buffers
; * copy from lower addresses to higher addresses
; */
; while (count--)
; *dst++ = *src++;
; }
; else {
; /*
; * Overlapping Buffers
; * copy from higher addresses to lower addresses
; */
; dst += count - 1;
; src += count - 1;
;
; while (count--)
; *dst-- = *src--;
; }
;
; return(ret);
; }
;***************************************************************************************************************
int strcmp(const char *str1,const char *str2)
{
while((*str1==*str2)&&(*str1))
{
str1++;
str2++;
}
if((*str1==*str2)&&(!*str1)) //Same strings
return 0;
else if((*str1)&&(!*str2)) //Same but str1 longer
return -1;
else if((*str2)&&(!*str1)) //Same but str2 longer
return 1;
else
return((*str1>*str2)?-1:1);
}
;***************************************************************************************************************
char *strstr1(const char *str1, const char *str2)
{
char *cp = (char *)str1; //type transfer
char *s1, *s2;
if(!str2)
return (char *)str1;
while (cp)
{
s1 = cp;
s2 = (char *)str2;
while( !s1 && !s2 && !(*s1-*s2))
{
s1++, s2++;
}
if(!s2)
return cp;
else
cp++;
}
return NULL;
}
;***************************************************************************************************************
*char *_itoa, *_ltoa, *_ultoa(val, buf, radix) - convert binary int to ASCII string
static void __cdecl xtoa (unsigned long val, char *buf, unsigned radix, int is_neg )
{
char *p; /* pointer to traverse string */
char *firstdig; /* pointer to first digit */
char temp; /* temp char */
unsigned digval; /* value of digit */
p = buf;
if (is_neg) {
/* negative, so output '-' and negate */
*p++ = '-';
val = (unsigned long)(-(long)val);
}
firstdig = p; /* save pointer to first digit */
do {
digval = (unsigned) (val % radix);
val /= radix; /* get next digit */
/* convert to ascii and store */
if (digval > 9)
*p++ = (char) (digval - 10 + 'a'); /* a letter */
else
*p++ = (char) (digval + '0'); /* a digit */
} while (val > 0);
/* We now have the digit of the number in the buffer, but in reverse
order. Thus we reverse them now. */
*p-- = '\0'; /* terminate string; p points to last digit */
do {
temp = *p;
*p = *firstdig;
*firstdig = temp; /* swap *p and *firstdig */
--p;
++firstdig; /* advance to next two digits */
} while (firstdig < p); /* repeat until halfway */
}
char * __cdecl _itoa ( int val, char *buf, int radix )
{
if (radix == 10 && val < 0)
xtoa((unsigned long)val, buf, radix, 1);
else
xtoa((unsigned long)(unsigned int)val, buf, radix, 0);
return buf;
}
char * __cdecl _ltoa ( long val, char *buf, int radix )
{
xtoa((unsigned long)val, buf, radix, (radix == 10 && val < 0));
return buf;
}
;***************************************************************************************************************
*long atol(char *nptr) - Convert string to long. Overflow is not detected.
long __cdecl _tstol(const _TCHAR *nptr )
{
int c; /* current char */
long total; /* current total */
int sign; /* if '-', then negative, otherwise positive */
while ( _istspace((int)(_TUCHAR)*nptr) )
++nptr; /* skip whitespace */
c = (int)(_TUCHAR)*nptr++;
sign = c; /* save sign indication */
if (c == _T('-') || c == _T('+'))
c = (int)(_TUCHAR)*nptr++; /* skip sign */
total = 0;
while ( (c = _tchartodigit(c)) != -1 ) {
total = 10 * total + c; /* accumulate digit */
c = (_TUCHAR)*nptr++; /* get next char */
}
if (sign == '-')
return -total;
else
return total; /* return result, negated if necessary */
}
int __cdecl _tstoi( const _TCHAR *nptr )
{
return (int)_tstol(nptr);
}
;***************************************************************************************************************
;performs a byteswap on an unsigned integer.
unsigned short __cdecl _byteswap_ushort(unsigned short i)
{
unsigned short j;
j = (i << 8) ;
j += (i >> 8) ;
return j;
}
unsigned long __cdecl _byteswap_ulong(unsigned long i)
{
unsigned int j;
j = (i << 24);
j += (i << 8) & 0x00FF0000;
j += (i >> 8) & 0x0000FF00;
j += (i >> 24);
return j;
}
;***************************************************************************************************************
*char *bsearch() - do a binary search on an array
*Entry:
* const char *key - key to search for const char *base - base of sorted array to search unsigned int num - number of *elements in array unsigned int width - number of bytes per element int (*compare)() - pointer to function that compares two
*array elements, returning neg when #1 < #2, pos when #1 > #2, and 0 when they are equal. Function is passed pointers to two
*array elements.
*Exit:
* if key is found: returns pointer to occurrence of key in array
* if key is not found:returns NULL
void * __cdecl bsearch ( REG4 const void *key, const void *base, size_t num, size_t width, int (__cdecl *compare)(const void *, const void *) )
{
REG1 char *lo = (char *)base;
REG2 char *hi = (char *)base + (num - 1) * width;
REG3 char *mid;
size_t half;
int result;
while (lo <= hi)
if (half = num / 2)
{
mid = lo + (num & 1 ? half : (half - 1)) * width;
if (!(result = (*compare)(key,mid)))
return(mid);
else if (result < 0)
{
hi = mid - width;
num = num & 1 ? half : half-1;
}
else {
lo = mid + width;
num = half;
}
}
else if (num)
return((*compare)(key,lo) ? NULL : lo);
else
break;
return(NULL);
}
;***************************************************************************************************************
void __cdecl _tmakepath (register _TSCHAR *path, const _TSCHAR *drive, const _TSCHAR *dir,
const _TSCHAR *fname, const _TSCHAR *ext )
{
register const _TSCHAR *p;
/* copy drive */
if (drive && *drive) {
*path++ = *drive;
*path++ = _T(':');
}
/* copy dir */
if ((p = dir) && *p) {
do {
*path++ = *p++;
}while (*p);
if (*(p-1) != _T('/') && *(p-1) != _T('\\')) {
*path++ = _T('\\');
}
}
/* copy fname */
if (p = fname) {
while (*p) {
*path++ = *p++;
}
}
/* copy ext, including 0-terminator - check to see if a '.' needs to be inserted. */
if (p = ext) {
if (*p && *p != _T('.')) {
*path++ = _T('.');
}
while (*path++ = *p++)
;
}
else {
/* better add the 0-terminator */
*path = _T('\0');
}
}
- C常用库函数实现
- 常用库函数的C实现
- C\C++常用库函数实现
- 常用C库函数的C实现
- 一些常用C语言库函数实现
- C中常用库函数的简单实现
- C语言常用字符串库函数实现
- C语言常用库函数及其实现
- 常用C库函数
- 常用C库函数
- C常用库函数
- C 常用库函数积累
- c语言常用库函数
- C语言常用库函数
- ACM常用c库函数
- C语言常用库函数
- C语言常用库函数
- 常用的C库函数
- Spark核心RDD的研究
- pixhawk自学笔记之uorb学习总结
- startService和bindService的区别
- Java Version Mismatch 解决方法
- 苦行使人心智敏锐
- C\C++常用库函数实现
- 用scrollview制作3D效果的导航页
- 猎球者app v2.6.3
- ORACLE:一列的多行数据拼成字符串
- 时间戳
- Convergent Encryption - 云存储的去重与加密
- HTTP协议个人理解
- android使用adb命令小结
- Android Graphics (一) :Paint