zoukankan      html  css  js  c++  java
  • 笔试算法题(04):实现 string & memcpy & strcpy & strlen

    出题:请实现给定String的类定义;

    分析:注意检查标准类构造注意事项;

    解题:

     1 #include <stdio.h>
     2 #include <string.h>
     3 /**
     4  * 检查是否需要构造函数
     5  * 检查是否需要无参构造函数
     6  * 检查是否需要成员变量(函数)私有
     7  * 检查是否需要在构造函数预初始化成员变量
     8  * 检查是否需要析构函数
     9  * 检查是否需要虚拟析构函数
    10  * 检查是否需要复制构造函数(参数为const)
    11  * 检查是否需要赋值重载函数(参数为const)
    12  *
    13  * */
    14 class MyString {
    15 public:
    16         MyString(const char *str=NULL);//普通和无参构造函数
    17         MyString(const MyString&);//拷贝构造函数
    18         ~MyString();//析构函数
    19         MyString& operator=(const MyString&);//重载操作符函数
    20 private:
    21         char *m_data;
    22 };
    23 MyString::MyString(const char *str) {
    24         if(str==NULL) {
    25                 m_data=new char[1];
    26                 *m_data=" ";
    27         } else {
    28                 int length=strlen(str);
    29                 m_data=new char[length+1];//需要为预留空间
    30                 strcpy(m_data, str);
    31         }
    32 }
    33 MyString::MyString(const MyString& other) {
    34         int length=strlen(other.m_data);
    35         strcpy(m_data, other.m_data);
    36 }
    37 MyString::~MyString() {
    38         delete [] m_data;
    39 }
    40 MyString& MyString::operator=(const MyString& other) {
    41         if(this==&other) {//需要判断是否为对象本身,否则可能double deletion
    42                 return *this;
    43         }
    44         delete [] m_data;
    45         int length=strlen(other.m_data);
    46         m_data=new char[length+1];
    47         strcpy(m_data, other.m_data);
    48         return *this;
    49 }

    出题:写一个函数,完成内存之间的复制(注意完整性);

    分析:当dest的起始位置在src到src+count之间时,dest会覆盖掉src后面的内存,所以应该使用倒序复制;

    解题:

     1 /**
     2  * 内存中字符串有五种相对位置,两种重叠的情况需要考虑内存破坏问题
     3  * 1
     4  * ****
     5  *         ****
     6  * 2
     7  * ****
     8  *   ****
     9  * 3
    10  * ****
    11  * ****
    12  * 4
    13  *    ****
    14  * ****
    15  * 5
    16  *          ****
    17  * ****
    18  * */
    19 void* MyMemoryCopy(void *dest, const void *src, size_t count) {
    20         if(dest==NULL && src==NULL) return NULL;
    21 
    22         char *pdest=static_cast(src);
    23         const char *psrc=static_cast(src);
    24         //字符串在内存中的存储方向是由低地址往高地址方向
    25         if(pdest>psrc && pdest<psrc+count) {
    26                 for(size_t i=count;i!=-1;i--) {
    27                         pdest[i]=psrc[i];//逆序拷贝
    28                 }
    29         } else {
    30                 for(size_t i=0;i<count;i++) {
    31                         pdest[i]=psrc[i];//顺序拷贝
    32                 }
    33         }
    34         return dest;
    35 }

    出题:正确的strcpy的实现;

    分析:返回值为char *,为了形成链式表达式;

    解题:

     1 char *strcpy(char *strDest, const char *strSrc)
     2 {
     3         assert((strDest!=NULL)&&(strSrc!=NULL));
     4         strSrc=const_cast<char *>(strScr);
     5         char *address=strDest;
     6         while((*strDest++=*strSrc++)!='')
     7         return address;
     8 }
     9 10. 正确的strcmp的实现
    10 int strcmp(const char *str1,const char *str2)
    11 {
    12         assert(str1!=NULL && str2!=NULL);
    13         int ret=0;
    14         while(!(ret=*(unsigned char *)str1 - *(unsigned char *)str2) &&
    15                 *str2)
    16         {
    17                 str1++;
    18                 str2++;
    19         }
    20         return ret;
    21 }

    出题:正确的strlen的实现;

    分析:Strlen可以计算以结尾的字符串的长度,长度包括除以外的所有字符,sizeof也能计算字符串的程度,但是它的结果包含了终止字符;

    解题:

     1 int strlen(const char *str)
     2 {
     3         assert(str!=NULL);
     4         int len;
     5         while((*str++)!='')
     6         {
     7                 len++;
     8         }
     9         return len;
    10 }
  • 相关阅读:
    ubuntu下文件安装与卸载
    webkit中的JavaScriptCore部分
    ubuntu 显示文件夹中的隐藏文件
    C语言中的fscanf函数
    test
    Use SandCastle to generate help document automatically.
    XElement Getting OuterXML and InnerXML
    XUACompatible meta 用法
    Adobe Dreamweaver CS5.5 中文版 下载 注册码
    The Difference Between jQuery’s .bind(), .live(), and .delegate()
  • 原文地址:https://www.cnblogs.com/leo-chen-2014/p/3731742.html
Copyright © 2011-2022 走看看