Переглянути джерело

分支七完成了题目 ,并做了单元测试

master
linyongji 4 роки тому
джерело
коміт
2d04f611c6
10 змінених файлів з 154 додано та 2 видалено
  1. +4
    -0
      .gitignore
  2. BIN
      Alogrithm/.vs/Alogrithm/v16/.suo
  3. +3
    -0
      Alogrithm/Alogrithm/Alogrithm.vcxproj
  4. +9
    -0
      Alogrithm/Alogrithm/Alogrithm.vcxproj.filters
  5. +15
    -0
      Alogrithm/Alogrithm/config/7_MaxDepth.ini
  6. +18
    -0
      Alogrithm/Alogrithm/include/7_MaxDepth.h
  7. +83
    -0
      Alogrithm/Alogrithm/src/7_MaxDepth.cpp
  8. +20
    -0
      Alogrithm/UnitTest/UnitTest.cpp
  9. +1
    -1
      Alogrithm/UnitTest/UnitTest.vcxproj
  10. +1
    -1
      Alogrithm/UnitTest/pch.h

+ 4
- 0
.gitignore Переглянути файл

@@ -104,3 +104,7 @@ Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/ab4a44a8d749bee1/6_CONTAINSDUPLICATE.ip
>>>>>>> 71fa9786fe524fa393358479cab8105f9239c68c
Alogrithm/Alogrithm/Debug/6_ContainsDuplicate.obj
Alogrithm/Alogrithm/config/6_ContainsDuplicate.ini.rej
Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/8d72cbe9b24557ef/7_MAXDEPTH.ipch
Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/d67b185b0905d0e0/7_MAXDEPTH.ipch
Alogrithm/.vs/Alogrithm/v16/TestStore/3/001.testlog
Alogrithm/Alogrithm/Debug/7_MaxDepth.obj

BIN
Alogrithm/.vs/Alogrithm/v16/.suo Переглянути файл


+ 3
- 0
Alogrithm/Alogrithm/Alogrithm.vcxproj Переглянути файл

@@ -146,6 +146,7 @@
<ClCompile Include="src\4_IsPalindrome.cpp" />
<ClCompile Include="src\5_MinDepth.cpp" />
<ClCompile Include="src\6_ContainsDuplicate.cpp" />
<ClCompile Include="src\7_MaxDepth.cpp" />
<ClCompile Include="src\main.cpp" />
</ItemGroup>
<ItemGroup>
@@ -155,6 +156,7 @@
<ClInclude Include="include\4_IsPalindrome.h" />
<ClInclude Include="include\5_MinDepth.h" />
<ClInclude Include="include\6_ContainsDuplicate.h" />
<ClInclude Include="include\7_MaxDepth.h" />
</ItemGroup>
<ItemGroup>
<None Include="config\1_ContainsNearbyDuplicate.ini" />
@@ -163,6 +165,7 @@
<None Include="config\4_IsPalindrome.ini" />
<None Include="config\5_MinDepth.ini" />
<None Include="config\6_ContainsDuplicate.ini" />
<None Include="config\7_MaxDepth.ini" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">


+ 9
- 0
Alogrithm/Alogrithm/Alogrithm.vcxproj.filters Переглянути файл

@@ -45,6 +45,9 @@
<ClCompile Include="src\6_ContainsDuplicate.cpp">
<Filter>源文件\src</Filter>
</ClCompile>
<ClCompile Include="src\7_MaxDepth.cpp">
<Filter>源文件\src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\2_ExcelSheetColumnTitle.h">
@@ -65,6 +68,9 @@
<ClInclude Include="include\6_ContainsDuplicate.h">
<Filter>头文件\include</Filter>
</ClInclude>
<ClInclude Include="include\7_MaxDepth.h">
<Filter>头文件\include</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="config\3_bool IsUgly.ini">
@@ -85,5 +91,8 @@
<None Include="config\6_ContainsDuplicate.ini">
<Filter>资源文件\config</Filter>
</None>
<None Include="config\7_MaxDepth.ini">
<Filter>资源文件\config</Filter>
</None>
</ItemGroup>
</Project>

+ 15
- 0
Alogrithm/Alogrithm/config/7_MaxDepth.ini Переглянути файл

@@ -0,0 +1,15 @@
[Test1]
input=3,9,20,NULL,NULL,15,7
output=3
[Test2]
input=4,3,1,2,NULL,6,8
output=3
[Test3]
input=4,3,1,NULL,2,6,8
output=3
[Test5]
input=NULL
output=1
[Test6]
input=
output=0

+ 18
- 0
Alogrithm/Alogrithm/include/7_MaxDepth.h Переглянути файл

@@ -0,0 +1,18 @@
#pragma once
#include <stdio.h>
#include <math.h>
#include <atlstr.h>


struct TreeNode2
{
int val;
struct TreeNode2* left;
struct TreeNode2* right;
};


int MaxDepth(struct TreeNode2* root);
TreeNode2* CreatBitTree2(char str[][50], int return_count);
void CreatBitTreeNode2(char str[][50], int return_count, TreeNode2* cur, int curIndex);
void free_tree2(TreeNode2* T);

+ 83
- 0
Alogrithm/Alogrithm/src/7_MaxDepth.cpp Переглянути файл

@@ -0,0 +1,83 @@
#include "../include/7_MaxDepth.h"

TreeNode2* CreatBitTree2(char str[][50], int return_count)
{
if (str == NULL || return_count <= 0) {
return NULL;
}
TreeNode2* head = (TreeNode2*)malloc(sizeof(TreeNode2));
if (NULL == head) {
return NULL;
}
head->val = _ttoi(str[0]);
CreatBitTreeNode2(str, return_count, head, 0);
return head;
}
void CreatBitTreeNode2(char str[][50], int return_count, TreeNode2* cur, int curIndex)
{
if (str == NULL || return_count <= 0 || cur == NULL || curIndex >= return_count || curIndex < 0) {
return;
}
int last = return_count - 1;
if ((2 * curIndex + 1 <= last) && strcmp(str[2 * curIndex + 1], "NULL")) {
cur->left = (TreeNode2*)malloc(sizeof(TreeNode2));
if (NULL == cur->left) {
return;
}
cur->left->val = _ttoi(str[2 * curIndex + 1]);
}
else {
cur->left = NULL;
}
if ((2 * curIndex + 2 <= last) && strcmp(str[2 * curIndex + 2], "NULL")) {
cur->right = (TreeNode2*)malloc(sizeof(TreeNode2));
if (NULL == cur->right) {
return;
}
cur->right->val = _ttoi(str[2 * curIndex + 2]);
}
else {
cur->right = NULL;
}
CreatBitTreeNode2(str, return_count, cur->left, 2 * curIndex + 1);
CreatBitTreeNode2(str, return_count, cur->right, 2 * curIndex + 2);
}
int MaxDepth(struct TreeNode2* root)
{
int Depth = 0;
int L1, L2;
if (NULL == root) {
return 0;
}
else if ((NULL == root->left) && (NULL == root->right)) {
return 1;
}
else if ((NULL != root->left) && (NULL == root->right)) {
Depth = MaxDepth(root->left);
}
else if ((NULL == root->left) && (NULL != root->right)) {
Depth = MaxDepth(root->right);
}
else {
L1 = MaxDepth(root->left);
L2 = MaxDepth(root->right);
Depth = fmax(L1, L2);
}
return Depth + 1;
}
void free_tree2(TreeNode2* T)//ºóÐòÊÍ·Å
{
if (!T) {
return;
}
if (T->left) {
free_tree2(T->left);
}
if (T->right) {
free_tree2(T->right);
}
if (T) {
free(T);
T = NULL;
}
}

+ 20
- 0
Alogrithm/UnitTest/UnitTest.cpp Переглянути файл

@@ -8,6 +8,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
#define FileName_4 "../Alogrithm/config/4_IsPalindrome.ini"
#define FileName_5 "../Alogrithm/config/5_MinDepth.ini"
#define FileName_6 "../Alogrithm/config/6_ContainsDuplicate.ini"
#define FileName_7 "../Alogrithm/config/7_MaxDepth.ini"

namespace UnitTest
{
@@ -112,4 +113,23 @@ namespace UnitTest
}
}
};
TEST_CLASS(UnitTest_7)
{
TEST_METHOD(TestMethode1)
{
char Section_Name[100][10] = { 0 };
int Section_Count = CalcCount(100, Section_Name, FileName_7);
CString Na, nExpect;
for (int i = 0; i < Section_Count; i++) {
GetPrivateProfileString(Section_Name[i], "input", " ", Na.GetBuffer(200), 200, FileName_7);
GetPrivateProfileString(Section_Name[i], "output", " ", nExpect.GetBuffer(20), 20, FileName_7);
char return_str[100][50] = { 0 };
int return_count = str_device2(Na, return_str);
TreeNode2* root = CreatBitTree2(return_str, return_count);
int nReal = MaxDepth(root);
Assert::AreEqual(nReal, _ttoi(nExpect));
free_tree2(root);
}
}
};
}

+ 1
- 1
Alogrithm/UnitTest/UnitTest.vcxproj Переглянути файл

@@ -103,7 +103,7 @@
<SubSystem>Windows</SubSystem>
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<GenerateDebugInformation>DebugFull</GenerateDebugInformation>
<AdditionalDependencies>../Alogrithm/Debug/1_ContainsNearbyDuplicate.obj;../Alogrithm/Debug/2_ExcelSheetColumnTitle.obj;../Alogrithm/Debug/3_bool IsUgly.obj;../Alogrithm/Debug/4_IsPalindrome.obj;../Alogrithm/Debug/5_MinDepth.obj;../Alogrithm/Debug/6_ContainsDuplicate.obj;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>../Alogrithm/Debug/1_ContainsNearbyDuplicate.obj;../Alogrithm/Debug/2_ExcelSheetColumnTitle.obj;../Alogrithm/Debug/3_bool IsUgly.obj;../Alogrithm/Debug/4_IsPalindrome.obj;../Alogrithm/Debug/5_MinDepth.obj;../Alogrithm/Debug/6_ContainsDuplicate.obj;../Alogrithm/Debug/7_MaxDepth.obj;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">


+ 1
- 1
Alogrithm/UnitTest/pch.h Переглянути файл

@@ -19,7 +19,7 @@
#include"../Alogrithm/include/4_IsPalindrome.h"
#include"../Alogrithm/include/5_MinDepth.h"
#include"../Alogrithm/include/6_ContainsDuplicate.h"
#include"../Alogrithm/include/7_MaxDepth.h"


int CalcCount(int n, char(*str)[10],const char *FileName);


Завантаження…
Відмінити
Зберегти