一、社论

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

二、stl模板函数

1、strcpy

  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

  1. 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. }

3、strcmp

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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  &&  MergeSort

1、kmp

  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

  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. }

3、MergeSort
  1. #include<iostream.h>
  2. void Merge(int r[], int r1[], int s, int m, int t)
  3. {
  4. int i = s;
  5. int j = m+1;
  6. int k = s;
  7. while (i <= m && j <= t)
  8. {
  9. if (r[i] <= r[j])
  10. r1[k++] = r[i++];
  11. else
  12. r1[k++] = r[j++];
  13. }
  14. if (i <= m)
  15. while (i <= m)
  16. r1[k++] = r[i++];
  17. else
  18. while (j <= t)
  19. r1[k++] = r[j++];
  20. for(int n = s; n <= t; n++)
  21. r[n] = r1[n];
  22. }
  23.  
  24. void MergeSort(int r[], int r1[], int s, int t)
  25. {
  26. if (s < t)
  27. {
  28. int m = (s+t)/2;
  29. MergeSort(r, r1, s, m);
  30. MergeSort(r, r1, m+1, t);
  31. Merge(r, r1, s, m, t);
  32. }
  33. }
  34. int main()
  35. {
  36. int r[8] = {10, 3, 5, 1, 9, 34, 54, 565}, r1[8];
  37. MergeSort(r, r1, 0, 7);
  38. for(int q = 0; q<8; q++)
  39. cout<<" "<<r[q];
  40. return 0;
  41. }

2、quicksort

访谈将源代码的函数 strcpy/memcpy/atoi/kmp/quicksort的更多相关文章

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

    http://blog.csdn.net/liuqiyao_01/article/details/26967813 二.stl模板函数 1.strcpy char * strcpy( char *st ...

  2. C/C++自实现的函数(memset, memcpy, atoi)

    函数原型: void * memset ( void * buffer, int c, size_t num ); 关于void * 因为任何类型的指针都可以传入memset函数,这也真是体现了内存操 ...

  3. 转:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文

    转自:C语言字符串操作函数 - strcpy.strcmp.strcat.反转.回文 C++常用库函数atoi,itoa,strcpy,strcmp的实现 作者:jcsu C语言字符串操作函数 1. ...

  4. c++中内存拷贝函数(C++ memcpy)详解

    原型:void*memcpy(void*dest, const void*src,unsigned int count); 功能:由src所指内存区域复制count个字节到dest所指内存区域. 说明 ...

  5. [转载] 已知strcpy的函数原型:char *strcpy(char *strDest, const char *strSrc),编写函数 strcpy(C++版)

    已知strcpy的函数原型:char *strcpy(char *strDest, const char *strSrc)其中strDest 是目的字符串,strSrc 是源字符串.不调用C++/C ...

  6. C语言——常用标准输入输出函数 scanf(), printf(), gets(), puts(), getchar(), putchar(); 字符串拷贝函数 strcpy(), strncpy(), strchr(), strstr()函数用法特点

    1 首先介绍几个常用到的转义符 (1)     换行符“\n”, ASCII值为10: (2)     回车符“\r”, ASCII值为13: (3)     水平制表符“\t”, ASCII值为 9 ...

  7. 编写实现字符串拷贝函数strcpy()完整版

    有个题目编程实现字符串拷贝函数strcpy(),很多人往往很快就写出下面这个代码. void strcpy( char *strDest,char *strSrc ) { while(( *strDe ...

  8. 【C语言】编写函数实现库函数atoi,把字符串转换成整形

    //编写函数实现库函数atoi.把字符串转换成整形 #include <stdio.h> #include <string.h> int my_atoi(const char ...

  9. C++使用函数strcpy出现bug: 错误 C4996 'strcpy': This function or variable

    C++中使用函数strcpy时出现问题: 解决方案: 在文件开头添加语句: #pragma warning(disable:4996) done! 剑指offer: 第一题:赋值运算符函数 #incl ...

随机推荐

  1. jquery 图片放大

    上一篇是关于手风琴效果的,但是有时候我们需要放大的图片大小不规律,想要在屏幕中间显示大图. 图片放大可以做为单独的效果,也可以和其他的效果结合起来.比如Demo 里的Demo5.html是以图片无缝切 ...

  2. HAOI2007 理想的正方形

    1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1402  Solved: 738[Submit][Sta ...

  3. LeetCode Summary Ranges (统计有序数列范围)

    题意:给出个有序不重复数列(可能负数),用缩写法记录这个数列. 思路:找每个范围的起始和结束即可. class Solution { public: vector<string> summ ...

  4. JPA--多对多关系

    JPA中,多对多关系用@ManyToMany标示. 关系维护端: package com.yl.demo1.bean.manyTomany; import java.util.HashSet; imp ...

  5. Entity Framework中查看生成的SQL语句

    Entity Framework 4.0 中是这样的,高版本的跟这个有些差异,不太一样,貌似已经到7了 using (Entities entities = new Entities()) { var ...

  6. Explain 结果解读与实践

    Explain 结果解读与实践 基于 MySQL 5.0.67 ,存储引擎 MyISAM . 注:单独一行的"%%"及"`"表示分隔内容,就象分开“第一章”“第 ...

  7. 【Python】Python-skier游戏[摘自.与孩子一起学编程]

    这是一个滑雪者的游戏. skier从上向下滑,途中会遇到树和旗子,捡起一个旗子得10分,碰到一颗树扣100分,可以用左右箭头控制skier方向. 准备素材 一 准备python环境:我下载的pytho ...

  8. 安装GNS3-0.8.6-all-in-one时language里没有选项

    初次安装使用GNS3,安装的版本是GNS3-0.8.6-all-in-one,本人也是菜鸟,安装时都是一路 Next,结果安装后运行出现了这样的问题,如图 language里是没有选项的,解决方法 把 ...

  9. 430单片机之定时器A功能的大致介绍

    总的来说,430单片机一共有三个定时器,定时器A,定时器B,还有就是看门狗定时器,这里我们主要是讨论430单片机的定时器A的功能,定时器A的功能是我目前见过最厉害的定时器,视频上说用好定时器A的话,对 ...

  10. Leetcode OJ : Merge k Sorted Lists 归并排序+最小堆 mergesort heap C++ solution

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...