資源簡介
《劍指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:賦值運(yùn)算符函數(shù)
//?題目:如下為類型CMyString的聲明,請為該類型添加賦值運(yùn)算符函數(shù)。
#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“);
}
//?連續(xù)賦值
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個文件信息
評論
共有 條評論