• c++课后作业


    字符串转换为整数

    1. int main()
    2. {
    3. char pn[21];
    4. cout << "请输入一个由数字组成的字符串: ";
    5. cin >> pn;
    6. int last = 0;
    7. int res[10];
    8. int j = strlen(pn);
    9. int idx = 2;
    10. cout << "请选择(2-二进制,10-十进制): ";
    11. cin >> idx;
    12. for (int i = 0,len = strlen(pn);i < len; i++)
    13. {
    14. if (pn[i] < '0' || pn[i] > '9') { cout << "非法字符!!!"; break; }
    15. last += (pn[i] - '0') * pow(idx,j-1);
    16. j = j - 1 ;
    17. }
    18. cout << last;
    19. }

    重写strcat和strncat

    1. char* mystrcat(char* dest, const char* src)
    2. {
    3. memcpy(dest + strlen(dest), src, strlen(src) + 1);
    4. return dest;
    5. }
    6. char* mystrncat(char* dest, const char* src, const size_t n)
    7. {
    8. size_t len = strlen(dest);
    9. memcpy(dest + len, src, n);
    10. *(dest + len + n) = 0;
    11. return dest;
    12. }

    重写strchr和strrchr

    注意⚠️:这里不能从后往前找,因为想从后往前找就只能使用strlen,而strlen本质就是遍历一遍字符串,这样就更慢了。

    1. const char* mystrchr(const char* s, int c)
    2. {
    3. char* p = (char*)s;
    4. while (*p)
    5. {
    6. if (*p == c) return p;
    7. p++;
    8. }
    9. return 0;
    10. }
    11. const char* mystrrchr(const char* s, int c) //查找字符
    12. {
    13. char* p = (char*)s;
    14. char* p1 = 0;
    15. while (*p)
    16. {
    17. if (*p == c)
    18. {
    19. p1 = p;
    20. }
    21. p++;
    22. }
    23. return p1;
    24. }

    重写strcmp和strncmp

    1. int mystrcmp(const char* str1, const char* str2)
    2. {
    3. char* p1 = (char*)str1;
    4. char* p2 = (char*)str2;
    5. while (*p1 && *p2)
    6. {
    7. if (*p1 != *p2)
    8. {
    9. if (*(p1) > *(p2)) return -1;
    10. else return 1;
    11. }
    12. p1++;
    13. p2++;
    14. }
    15. return 0;
    16. }
    17. int mystrncmp(const char* str1, const char* str2, size_t n)
    18. {
    19. char* p1 = (char*)str1;
    20. char* p2 = (char*)str2;
    21. for (int i = 0; i < n; i++)
    22. {
    23. if (*p1 != *p2)
    24. {
    25. if (*(p1) > *(p2)) return -1;
    26. else return 1;
    27. }
    28. p1++;
    29. p2++;
    30. }
    31. return 0;
    32. }

    重写strstr

    1. char* caijimystrstr(const char* haystack, const char* needle)
    2. {
    3. char* p1 = (char*)haystack;
    4. char* p2 = (char*)needle;
    5. char* tmp1;
    6. char* tmp2 = p2;
    7. while (*p1)
    8. {
    9. while (*p1 == *p2)
    10. {
    11. tmp1 = p1;
    12. for (int i = 0; i < strlen(needle); i++)
    13. {
    14. if (*p1 != *p2)
    15. {
    16. continue;
    17. }
    18. p1++;
    19. p2++;
    20. if (*p2 == 0)
    21. {
    22. return p1-strlen(needle);
    23. }
    24. }
    25. continue;
    26. }
    27. p2 = tmp2;
    28. p1++;
    29. }
    30. return nullptr;
    31. }
    32. const char* dalaomystrstr(const char* haystack, const char* needle)
    33. {
    34. size_t ii = 0, jj = 0;
    35. size_t len = strlen(haystack), slen = strlen(needle);
    36. while ((ii < len) && (jj < slen))
    37. {
    38. if (haystack[ii] == needle[jj])
    39. {
    40. ii++, jj++;
    41. }
    42. else
    43. {
    44. ii = ii - jj;
    45. jj = 0;
    46. ii++;
    47. }
    48. }
    49. if (jj == slen) return (haystack+(ii-jj));
    50. return 0;
    51. }

    解析XML

    1. bool xmlbuffer(const char* xmlbuf, const char* fieldname, char* value = 0, const size_t ilen = 0)
    2. {
    3. if (xmlbuf == 0 || fieldname == 0) return false;
    4. //创建字段
    5. int lenfieldname = strlen(fieldname);
    6. //开辟空间
    7. char* sfieldname = new char[lenfieldname + 3];
    8. char* efieldname = new char[lenfieldname + 4];
    9. //初始化
    10. memset(sfieldname, 0, lenfieldname +3); // 错误代码 memset(sfieldname, 0, sfieldname); 原因:new出来的空间不能用memset
    11. memset(efieldname, 0, lenfieldname +4);
    12. //创建字段
    13. strcpy(sfieldname, "<"); strcat(sfieldname, fieldname); strcat(sfieldname,">");
    14. strcpy(efieldname, "); strcat(efieldname, fieldname); strcat(efieldname, ">");
    15. //找字段;
    16. char* start;
    17. char* end;
    18. start = (char*)strstr(xmlbuf, sfieldname);
    19. end = (char*)strstr(xmlbuf, efieldname);
    20. //没找到
    21. if (start == 0 || end == 0)
    22. {
    23. delete[] sfieldname;
    24. delete[] efieldname;
    25. return 0;
    26. }
    27. size_t vallen = (end - start - lenfieldname - 2); //指针减去指针等于数值,表示的是从 start 到 end 的距离减去了 lenfieldname( 的长度)和 2(< 和 > 的长度)。
    28. if (ilen == 0)
    29. {
    30. strncpy(value, start + lenfieldname + 2, vallen);
    31. value[vallen] = 0;
    32. }
    33. else if (vallen <= ilen)
    34. {
    35. strncpy(value, start + lenfieldname + 2, vallen);
    36. value[vallen] = 0;
    37. }
    38. else
    39. {
    40. strncpy(value, start + lenfieldname + 2, ilen);
    41. value[ilen] = 0;
    42. }
    43. delete[] sfieldname;
    44. delete[] efieldname;
    45. return 1;
    46. }

    不重复的随机数

    1. void rrand(int arr[], const size_t len, const int minvalue = 0)
    2. {
    3. srand(time(0));
    4. for (int i = 0; i < len; i++)
    5. {
    6. while (1)
    7. {
    8. int tmp = rand() % len + minvalue;
    9. int j = 0;
    10. for (j = 0; j < i; j++)
    11. {
    12. if (tmp == arr[j]) break;
    13. }
    14. if (j == i)
    15. {
    16. arr[i] = tmp;
    17. break;
    18. }
    19. }
    20. }
    21. }

    重写strcpy和strncpy

    1. char* my2strcpy(char* dest, const char* src)
    2. {
    3. int i = 0;
    4. while (src[i])
    5. {
    6. dest[i] = src[i];
    7. i++;
    8. }
    9. dest[i] = 0;
    10. return dest;
    11. }
    12. char* my2strncpy(char* dest, const char* src , const size_t n )
    13. {
    14. if (n < 0) return 0;
    15. size_t lens = strlen(src);
    16. if (n > lens)
    17. {
    18. my2strcpy(dest, src);
    19. }
    20. int i = 0;
    21. for (i = 0; i < n; i++)
    22. {
    23. dest[i] = src[i];
    24. }
    25. dest[i++] = 0;
    26. return dest;
    27. }

  • 相关阅读:
    JS中的链
    基于arduino uno + L298 的直流电机驱动proteus仿真设计
    MD5 绕过第三式:ffifdyop
    verilog 并行块实现
    Android修行手册 - 一文全了解Kotlin几种静态变量、函数实现的那些事
    哨兵模式(sentinel)
    全国市政公用事业和邮政、电信业发展数据,shp/excel格式
    A Guide to PriorityQueue
    【2022 谷歌开发者大会】名额有限,快来,带你体验谷歌的工程师文化
    电影管理系统
  • 原文地址:https://blog.csdn.net/m0_54146002/article/details/140286685