资源简介

《剑指offer》第二版 高清PDF+代码附赠 64M PDF文档

资源截图

代码片段和文件信息

/*******************************************************************
Copyright(c) 2016 Harry He
All rights reserved.

Distributed under the BSD license.
(See accompanying file LICENSE.txt at
https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
*******************************************************************/

//==================================================================
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================

// 面试题1:赋值运算符函数
// 题目:如下为类型CMyString的声明,请为该类型添加赋值运算符函数。

#include
#include

class CMyString
{
public:
    CMyString(char* pData = nullptr);
    CMyString(const CMyString& str);
    ~CMyString(void);

    CMyString& operator = (const CMyString& str);

    void Print();
      
private:
    char* m_pData;
};

CMyString::CMyString(char *pData)
{
    if(pData == nullptr)
    {
        m_pData = new char[1];
        m_pData[0] = ‘\0‘;
    }
    else
    {
        int length = strlen(pData);
        m_pData = new char[length + 1];
        strcpy(m_pData pData);
    }
}

CMyString::CMyString(const CMyString &str)
{
    int length = strlen(str.m_pData);
    m_pData = new char[length + 1];
    strcpy(m_pData str.m_pData);
}

CMyString::~CMyString()
{
    delete[] m_pData;
}

CMyString& CMyString::operator = (const CMyString& str)
{
    if(this == &str)
        return *this;

    delete []m_pData;
    m_pData = nullptr;

    m_pData = new char[strlen(str.m_pData) + 1];
    strcpy(m_pData str.m_pData);

    return *this;
}

// ====================测试代码====================
void CMyString::Print()
{
    printf(“%s“ m_pData);
}

void Test1()
{
    printf(“Test1 begins:\n“);

    char* text = “Hello world“;

    CMyString str1(text);
    CMyString str2;
    str2 = str1;

    printf(“The expected result is: %s.\n“ text);

    printf(“The actual result is: “);
    str2.Print();
    printf(“.\n“);
}

// 赋值给自己
void Test2()
{
    printf(“Test2 begins:\n“);

    char* text = “Hello world“;

    CMyString str1(text);
    str1 = str1;

    printf(“The expected result is: %s.\n“ text);

    printf(“The actual result is: “);
    str1.Print();
    printf(“.\n“);
}

// 连续赋值
void Test3()
{
    printf(“Test3 begins:\n“);

    char* text = “Hello world“;

    CMyString str1(text);
    CMyString str2 str3;
    str3 = str2 = str1;

    printf(“The expected result is: %s.\n“ text);

    printf(“The actual result is: “);
    str2.Print();
    printf(“.\n“);

    printf(“The expected result is: %s.\n“ text);

    printf(“The actual result is: “);
    str3.Print();
    printf(“.\n“);
}

int main(int argc char* argv[])
{
    Test1();
    Test2();
    Test3();

    return 0;
}


 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       2518  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\.gitattributes

     文件       3833  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\.gitignore

     文件       7160  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\01_AssignmentOperator\01_AssignmentOperator.vcxproj

     文件        949  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\01_AssignmentOperator\01_AssignmentOperator.vcxproj.filters

     文件       2798  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\01_AssignmentOperator\AssignmentOperator.cpp

     文件       2626  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\02_Singleton\02_Singleton.csproj

     文件        184  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\02_Singleton\App.config

     文件       3573  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\02_Singleton\Program.cs

     文件       1400  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\02_Singleton\Properties\AssemblyInfo.cs

     文件       7126  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\03_01_DuplicationInArray\03_01_DuplicationInArray.vcxproj

     文件        946  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\03_01_DuplicationInArray\03_01_DuplicationInArray.vcxproj.filters

     文件       4182  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\03_01_DuplicationInArray\FindDuplication.cpp

     文件       7138  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\03_02_DuplicationInArrayNoEdit\03_02_DuplicationInArrayNoEdit.vcxproj

     文件        952  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\03_02_DuplicationInArrayNoEdit\03_02_DuplicationInArrayNoEdit.vcxproj.filters

     文件       4818  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\03_02_DuplicationInArrayNoEdit\FindDuplicationNoEdit.cpp

     文件       7144  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\04_FindInPartiallySortedMatrix\04_FindInPartiallySortedMatrix.vcxproj

     文件        958  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\04_FindInPartiallySortedMatrix\04_FindInPartiallySortedMatrix.vcxproj.filters

     文件       3471  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\04_FindInPartiallySortedMatrix\FindInPartiallySortedMatrix.cpp

     文件       7116  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\05_ReplaceSpaces\05_ReplaceSpaces.vcxproj

     文件        944  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\05_ReplaceSpaces\05_ReplaceSpaces.vcxproj.filters

     文件       3650  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\05_ReplaceSpaces\ReplaceSpaces.cpp

     文件       7319  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\06_PrintListInReversedOrder\06_PrintListInReversedOrder.vcxproj

     文件        955  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\06_PrintListInReversedOrder\06_PrintListInReversedOrder.vcxproj.filters

     文件       2329  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\06_PrintListInReversedOrder\PrintListInReversedOrder.cpp

     文件       7309  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\07_ConstructBinaryTree\07_ConstructBinaryTree.vcxproj

     文件        950  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\07_ConstructBinaryTree\07_ConstructBinaryTree.vcxproj.filters

     文件       5285  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\07_ConstructBinaryTree\ConstructBinaryTree.cpp

     文件       7313  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\08_NextNodeInBinaryTrees\08_NextNodeInBinaryTrees.vcxproj

     文件        952  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\08_NextNodeInBinaryTrees\08_NextNodeInBinaryTrees.vcxproj.filters

     文件       6085  2018-03-05 17:23  剑指offer第二版\CodingInterviewChinese2\08_NextNodeInBinaryTrees\NextNodeInBinaryTrees.cpp

............此处省略320个文件信息

评论

共有 条评论