面试必会函数源代码 strcpy/memcpy/atoi/kmp/quicksort

来源:互联网 发布:讯鸟软件怎么样 编辑:程序博客网 时间:2024/04/19 12:18

一、写在前面

        继上一次发表了一片关于参加秋招的学弟学妹们如何准备找工作的博客之后,反响很大,顾在此整理一下,以便大家复习。好多源自july的这篇博客,也有很多是我自己整理的。希望大家能够一遍一遍的写,直到能够秒杀为止。


二、stl模板函数


    1、strcpy

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. char * strcpy( char *strDest, const char *strSrc )          
  2. {         
  3.     if(strDest == strSrc) { return strDest; }      
  4.     assert( (strDest != NULL) && (strSrc != NULL) );         
  5.     char *address = strDest;          
  6.     while( (*strDest++ = * strSrc++) != '\0' );          
  7.     return address;         
  8. }    

    2、strncpy

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;">char *strncpy(char *strDes, const char *strSrc, unsigned int count)          
  2. {          
  3.     assert(strDes != NULL && strSrc != NULL);          
  4.     char *address = strDes;          
  5.     while (count-- && *strSrc != '\0')          
  6.         *strDes++ = *strSrc++;       
  7.     *strDes = '\0';      
  8.     return address;          
  9. } </span>  


    3、strcmp

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int strcmp(const char *s, const char *t)       
  2. {       
  3.     assert(s != NULL && t != NULL);       
  4.     while (*s && *t && *s == *t)       
  5.     {       
  6.         ++ s;       
  7.         ++ t;       
  8.     }       
  9.     return (*s - *t);       
  10. }   

    4、strcat

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. char *strcat(char *strDes, const char *strSrc)       
  2. {       
  3.     assert((strDes != NULL) && (strSrc != NULL));       
  4.     char *address = strDes;       
  5.     while (*strDes != '\0')       
  6.         ++ strDes;       
  7.     while ((*strDes ++ = *strSrc ++) != '\0')       
  8.         NULL;       
  9.     return address;       
  10. }   

    5、strlen

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int strlen(const char *str)       
  2. {       
  3.     assert(str != NULL);       
  4.     int len = 0;       
  5.     while (*str ++ != '\0')       
  6.         ++ len;       
  7.     return len;       
  8. }    

    6、strstr

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. char *strstr(const char *strSrc, const char *str)       
  2. {       
  3.     assert(strSrc != NULL && str != NULL);       
  4.     const char *s = strSrc;       
  5.     const char *t = str;       
  6.     for (; *strSrc != '\0'; ++ strSrc)       
  7.     {       
  8.         for (s = strSrc, t = str; *t != '\0' && *s == *t; ++s, ++t)       
  9.             NULL;       
  10.         if (*t == '\0')       
  11.             return (char *) strSrc;       
  12.     }       
  13.     return NULL;       
  14. }     

    7、strncat

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. char *strncat(char *strDes, const char *strSrc, unsigned int count)       
  2. {       
  3.     assert((strDes != NULL) && (strSrc != NULL));       
  4.     char *address = strDes;       
  5.     while (*strDes != '\0')       
  6.         ++ strDes;       
  7.     while (count -- && *strSrc != '\0' )       
  8.         *strDes ++ = *strSrc ++;       
  9.     *strDes = '\0';       
  10.     return address;       
  11. }  

    8、strncmp

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int strncmp(const char *s, const char *t, unsigned int count)       
  2. {       
  3.     assert((s != NULL) && (t != NULL));       
  4.     while (*s && *t && *s == *t && count --)       
  5.     {       
  6.         ++ s;       
  7.         ++ t;       
  8.     }       
  9.     return (*s - *t);       
  10. }  

    9、memcpy

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. void *memcpy(void *dest, const void *src, unsigned int count)       
  2. {       
  3.     assert((dest != NULL) && (src != NULL));       
  4.     void *address = dest;       
  5.     while (count --)       
  6.     {       
  7.         *(char *) dest = *(char *) src;       
  8.         dest = (char *) dest + 1;       
  9.         src = (char *) src + 1;       
  10.     }       
  11.     return address;       
  12. }   

    10、memccpy

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. void *memccpy(void *dest, const void *src, int c, unsigned int count)       
  2. {       
  3.     assert((dest != NULL) && (src != NULL));       
  4.     while (count --)       
  5.     {       
  6.         *(char *) dest = *(char *) src;       
  7.         if (* (char *) src == (char) c)       
  8.             return ((char *)dest + 1);       
  9.         dest = (char *) dest + 1;       
  10.         src = (char *) src + 1;       
  11.     }       
  12.     return NULL;       
  13. }   

    11、memcmp

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int memcmp(const void *s, const void *t, unsigned int count)       
  2. {       
  3.     assert((s != NULL) && (t != NULL));       
  4.     while (*(char *) s && *(char *) t && *(char *) s == *(char *) t && count --)       
  5.     {       
  6.         s = (char *) s + 1;       
  7.         t = (char *) t + 1;       
  8.     }       
  9.     return (*(char *) s - *(char *) t);       
  10. }  

    12、memmove

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //@big:      
  2. //要处理src和dest有重叠的情况,不是从尾巴开始移动就没问题了。      
  3. //一种情况是dest小于src有重叠,这个时候要从头开始移动,      
  4. //另一种是dest大于src有重叠,这个时候要从尾开始移动。      
  5. void *memmove(void *dest, const void *src, unsigned int count)       
  6. {      
  7.     assert(dest != NULL && src != NULL);      
  8.     char* pdest = (char*) dest;      
  9.     char* psrc = (char*) src;      
  10.     
  11.     //pdest在psrc后面,且两者距离小于count时,从尾部开始移动. 其他情况从头部开始移动      
  12.     if (pdest > psrc && pdest - psrc < count)       
  13.     {      
  14.         while (count--)       
  15.         {      
  16.             *(pdest + count) = *(psrc + count);      
  17.         }      
  18.     } else       
  19.     {      
  20.         while (count--)       
  21.         {      
  22.             *pdest++ = *psrc++;      
  23.         }      
  24.     }      
  25.     return dest;      
  26. }    

    13、memset

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. void *memset(void *str, int c, unsigned int count)       
  2. {       
  3.     assert(str != NULL);       
  4.     void *s = str;       
  5.     while (count --)       
  6.     {       
  7.         *(char *) s = (char) c;       
  8.         s = (char *) s + 1;       
  9.     }       
  10.     return str;       
  11. }  


三、atoi && itoa


    1、atoi

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //代码自己所写,不对地方请及时指正,谢谢!  
  2. //inf用来标记作为判断是否越界  
  3. //atoiFlag作为是否是正确结果的返回值  
  4. const __int64 inf = (0x7fffffff);  
  5. int atoiFlag;  
  6. int atoi(const char* ch)  
  7. {  
  8.     ASSERT(ch != NULL);  
  9.     atoiFlag = false;  
  10.     while (*ch == ' ' || *ch == '\t')  
  11.         ch ++;  
  12.     int isMinus = 1;  
  13.     if (*ch == '-')  
  14.     {  
  15.         isMinus = -1;  
  16.         ch ++;  
  17.     }  
  18.     else if (*ch == '+')  
  19.     {  
  20.         ch ++;  
  21.     }  
  22.     //判断非法  
  23.     if (!(*ch <= '9' && *ch >= '0'))  
  24.         return 0;  
  25.     __int64 ans = 0;  
  26.     while (*ch && *ch <= '9' && *ch >= '0')  
  27.     {  
  28.         ans *= 10;  
  29.         ans += *ch - '0';  
  30.         //判断越界  
  31.         if (ans > inf)  
  32.         {  
  33.             return inf;  
  34.         }  
  35.         ch ++;  
  36.     }  
  37.     ans *= isMinus;  
  38.     atoiFlag = true;  
  39.     return (int)ans;  
  40. }  
  41.   
  42. //如何使用  
  43. int main()  
  44. {  
  45.     char a[100];  
  46.     while (scanf("%s", a) != EOF)  
  47.     {  
  48.         int ans = atoi(a);  
  49.         if (atoiFlag)  
  50.             printf("%d\n", ans);  
  51.         else if (ans == inf)  
  52.             puts("The data of crossing the line");  
  53.         else  
  54.             puts("Not is a integer");  
  55.     }  
  56.     return 0;  
  57. }  



    2、itoa

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <stdlib.h>   
  2. #include <stdio.h>   
  3. char *myitoa(int num,char *str,int radix);  
  4. int main()   
  5. {   
  6.     int number = -123456;   
  7.     char string[25];   
  8.     myitoa(number, string, 16);   
  9.     printf("integer = %d string = %s\n", number, string);   
  10.     return 0;   
  11. }   
  12. /* 实现itoa函数的源代码 */   
  13. char *myitoa(int num,char *str,int radix)   
  14. {    
  15.     /* 索引表 */   
  16.     char index[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";   
  17.     unsigned unum; /* 中间变量 */   
  18.     int i=0,j,k;   
  19.     /* 确定unum的值 */   
  20.     if(radix==10&&num<0) /* 十进制负数 */   
  21.     {   
  22.         unum=(unsigned)-num;   
  23.         str[i++]='-';   
  24.     }   
  25.     else unum=(unsigned)num; /* 其他情况 */   
  26.     /* 逆序 */   
  27.     do    
  28.     {   
  29.         str[i++]=index[unum%(unsigned)radix];   
  30.         unum/=radix;   
  31.     }while(unum);   
  32.     str[i]='\0';   
  33.     /* 转换 */   
  34.     if(str[0]=='-') k=1; /* 十进制负数 */   
  35.     else k=0;   
  36.     /* 将原来的“/2”改为“/2.0”,保证当num在16~255之间,radix等于16时,也能得到正确结果 */   
  37.     char temp;   
  38.     for(j=k;j<=(i-k-1)/2.0;j++)   
  39.     {   
  40.         temp=str[j];   
  41.         str[j]=str[i-j-1];   
  42.         str[i-j-1]=temp;   
  43.     }   
  44.     return str;   
  45. }   


四、kmp  &&  quicksort


    1、kmp

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. void getNext(const char* p, int next[])  
  2. {  
  3.     next[0] = -1;  
  4.     int index = 0;  
  5.     int p_l = strlen(p);  
  6.     for (int i=1; i<p_l; i++)  
  7.     {  
  8.         index = next[i-1];  
  9.         while (index >= 0 && p[index+1] != p[i])  
  10.         {  
  11.             index = next[index];  
  12.         }  
  13.         if (p[index+1] == p[i])  
  14.         {  
  15.             next[i] = index + 1;  
  16.         }  
  17.         else  
  18.         {  
  19.             next[i] = -1;  
  20.         }  
  21.     }  
  22. }  
  23.   
  24. int kmp(const char* t, const char* p)  
  25. {  
  26.     const int t_l = strlen(t);  
  27.     const int p_l = strlen(p);  
  28.     int Next = new int[p_l + 1];  
  29.     getNext(p, Next);  
  30.     int p_index = 0, t_index = 0;  
  31.       
  32.     int cnt = 0;  
  33.       
  34.     while (p_index < p_l && t_index < t_l)  
  35.     {  
  36.         if (t[t_index] == p[p_index])  
  37.         {  
  38.             t_index ++;  
  39.             p_index ++;  
  40.         }  
  41.         else if (p_index == 0)  
  42.         {  
  43.             t_index ++;  
  44.         }  
  45.         else  
  46.         {  
  47.             p_index = Next[p_index-1] + 1;  
  48.         }  
  49.         if (p_index == p_l)  
  50.         {  
  51.             cnt ++;  
  52.             p_index = Next[p_index-1] + 1;  
  53.         }  
  54.     }  
  55.     delete[] Next;  
  56.     return cnt;  
  57. }  



    2、quicksort


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <time.h>  
  4.   
  5. int partion(int a[], int p, int r)  
  6. {  
  7.     srand((unsigned)time(NULL));  
  8.     int e = rand()%(r-p+1)+p;  
  9.     int tmp = a[e];  
  10.     a[e] = a[r];  
  11.     a[r] = tmp;  
  12.     int x = a[r];  
  13.     int i, j;  
  14.     i = p;  
  15.     for (j=p; j<r; j++)  
  16.     {  
  17.         if (a[j] <= x)  
  18.         {  
  19.             tmp = a[i];  
  20.             a[i] = a[j];  
  21.             a[j] = tmp;  
  22.             i ++;  
  23.         }  
  24.     }  
  25.     tmp = a[r];  
  26.     a[r] = a[i];  
  27.     a[i] = tmp;  
  28.     return i;  
  29. }  
  30.   
  31. void QuickSort(int a[], int p, int r)  
  32. {  
  33.     if (p < r)  
  34.     {  
  35.         int q = partion(a, p, r);  
  36.         QuickSort(a, p, q-1);  
  37.         QuickSort(a, q+1, r);  
  38.     }  
  39. }  
  40.   
  41. int main()  
  42. {  
  43.     int a[] = {1,2,3,-1,5,-9,10,33,0};  
  44.     QuickSort(a, 0, 8);  
  45.     for (int i=0; i<9; i++)  
  46.     {  
  47.         printf("%d\t", a[i]);  
  48.     }  
  49.     return 0;  
  50. }  

0 0