diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000000000000000000000000000000000000..7900aa9507187e641ceed7404c3d59e455d54165 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "C++/codes/2024-07/宋彬彬/MyAStar/external/googletest"] + path = C++/codes/2024-07/宋彬彬/MyAStar/external/googletest + url = https://github.com/google/googletest.git diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter13Test4/CMakeLists.txt" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter13Test4/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..409606b3cd9894453cea3126fc522c3a7ca1eec5 --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter13Test4/CMakeLists.txt" @@ -0,0 +1,23 @@ +# 设置CMake的最低版本要求 +cmake_minimum_required(VERSION 3.10) + +# 设置项目名称和版本 +project(MyWineProject VERSION 1.0) + +# 设置C++标准 +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +# 添加可执行文件 +add_executable(myapp main.cpp Port.cpp VintagePort.cpp) + +# 如果你的项目更复杂,可能需要更详细的设置,比如: +# 查找库、设置包含目录、链接库等 +# 但对于这个简单的例子,上面的 add_executable 就足够了 + +# 如果你有头文件,通常不需要在 CMakeLists.txt 中特别指定它们, +# 因为编译器在编译源文件时会自动查找它们(只要它们位于正确的包含路径中)。 +# 但是,如果你的头文件位于非标准位置,你可以使用 include_directories() 来指定它们。 +# 例如: +# include_directories(${CMAKE_SOURCE_DIR}/include) +# 注意:在这个例子中,我们不需要这样做,因为头文件和源文件都在同一目录下。 \ No newline at end of file diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter13Test4/Port.cpp" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter13Test4/Port.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..5305589aa12c1bde732360899ef88084bde92f97 --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter13Test4/Port.cpp" @@ -0,0 +1,56 @@ +#include"Port.h" + +Port::Port(const Port &p) +{ + this->brand = new char[strlen(p.brand)+1]; + strcpy(this->brand,p.brand); + strcpy(this->style,p.style); + this->bottles = p.bottles; +} + +Port::Port(const char *br,const char *st,int b) +{ + this->brand = new char(strlen(br)+1); + strcpy(this->brand,br); + strcpy(this->style,st); + this->bottles = b; +} + +Port::~Port() +{ + delete []brand; +} + +Port & Port::operator=(const Port &p) +{ + if(this == &p){return *this;} + this->brand = new char[strlen(p.brand)+1]; + strcpy(this->brand,p.brand); + strcpy(this->style,p.style); + this->bottles = p.bottles; + return *this; +} + +Port & Port::operator+=(int b) +{ + this->bottles += b; + return *this; +} + +Port & Port::operator-=(int b) +{ + this->bottles -= b; + return *this; +} + +void Port::Show()const{ + cout<<"Brand: "<brand<style<bottles< +#include +#include + +using namespace std; + +class Port +{ +private: + char *brand; + char style[20]; + int bottles; +public: + Port(const char *br="none",const char *st="none",int b=0); + Port(const Port &p); + virtual ~Port(); + Port & operator=(const Port &p); + Port & operator+=(int b); //add b to bottles + Port & operator-=(int b); //subtracts b from bottles + int BottleCount() const ; + virtual void Show() const; + friend ostream &operator<<(ostream &os,const Port &p); +}; + +#endif \ No newline at end of file diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter13Test4/VintagePort.cpp" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter13Test4/VintagePort.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..54ff50bb612dd1663a179d03f95c26916b0049d8 --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter13Test4/VintagePort.cpp" @@ -0,0 +1,44 @@ +#include"VintagePort.h" + +VintagePort::VintagePort():Port() +{ + nickname = "none"; + year = 0; +} + +VintagePort::VintagePort(const char *br, int b, const char *nn, int y) +{ + nickname = new char[strlen(nn)+1]; + strcpy(nickname,nn); + year = y; +} + +VintagePort::VintagePort(const VintagePort &vp):Port(vp) +{ + nickname = new char[strlen(vp.nickname)+1]; + strcpy(nickname,vp.nickname); + year = vp.year; +} + +VintagePort &VintagePort::operator=(const VintagePort &vp) +{ + if(&vp == this) {return *this;} + delete []nickname; + nickname = new char[strlen(vp.nickname)+1]; + strcpy(nickname,vp.nickname); + year = vp.year; + return *this; +} + +void VintagePort::Show() const +{ + Port::Show(); + cout<<"NickName: "< + + +double add(double ,double); +double kMinus(double ,double); +double mult(double,double); +double calculate(double (*p)(double,double),double,double); + +int main() +{ + double opera_a,opera_b,result; + int opera_type=-1; + char ctn; + + double (*pa[3])(double,double) = {add,kMinus,mult}; + + while (true) + { + opera_a = -999; + opera_b = -999; + result = -999; + opera_type = -10; + ctn = '\0'; + + std::cout<<"输入两个数用于计算:"<>opera_a>>opera_b; + std::cout<>opera_type; + + + switch(opera_type) + { + case 1: result=pa[0](opera_a,opera_b);break; + case 2: result=pa[1](opera_a,opera_b);break; + case 3: result=pa[2](opera_a,opera_b);break; + default:break; + } + + std::cout<<"计算结果为:"<>ctn; + + if(ctn == 'N') + { + break; + } + } + return 0; +} + +double add(double a,double b) +{ + return a+b; +} +double kMinus(double a,double b) +{ + return a-b; +} +double mult(double a,double b) +{ + return a*b; +} +double calculate(double (*p)(double,double),double a,double b) +{ + return (*p)(a,b); +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter7Test9.cpp" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter7Test9.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..523c5e7e0508bd29e3d1154bd3b6217f8bd44e8d --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter7Test9.cpp" @@ -0,0 +1,36 @@ +#include +#include +#include + +const int Seasons = 4; +const char Snames[4][10] = {"Spring","Summer","Fall","Winter"}; + +void fill(double [],int count); +void show(const char str[][10],double [],int count); + + +int main() +{ + double spand[4] = {0.0}; + fill(spand,Seasons); + show(Snames,spand,Seasons); + + return 0; +} + +void fill(double arr[],int count) +{ + for(int i=0;i>arr[i]; + } +} + +void show(const char str[][10],double arr[],int count) +{ + for(int i=0;i + +template +T ReturnMax(T a[],T b[],T c[],T d[],T e[]) +{ + T max = -999.0; + for(int i=0;i<3;i++) + { + if(a[i] > max) {max = a[i];} + if(b[i] > max) {max = b[i];} + if(c[i] > max) {max = c[i];} + if(d[i] > max) {max = d[i];} + if(e[i] > max) {max = e[i];} + } + return max; +} + +int main() +{ + /*int arr1[3] = {1,3,5}; + int arr2[3] = {4,56,7}; + int arr3[3] = {4,6,7}; + int arr4[3] = {4,9,7}; + int arr5[3] = {3,5,7}; + int max = -999;*/ + + double arr1[3] = {1.0,3.4,5.6}; + double arr2[3] = {4.8,56.0,7.9}; + double arr3[3] = {4,6,7}; + double arr4[3] = {4,9,7}; + double arr5[3] = {3,5,7}; + double max = -999.0; + + max = ReturnMax(arr1,arr2,arr3,arr4,arr5); + + //std::cout< +#include + +template +T1 ReturnMax(T1 a[],T2 b) +{ + T1 max = -999; + for(int i=0;i max) {max = a[i];} + } + return max; +} +template +T1 *ReturnMax(T1 *a[],T2 b); + +int main() +{ + /*int arr1[6] = {4,5,7,9,23,432}; + int cnt = 6; + int max = -9999;*/ + + double arr1[6] = {4.9,5.7,7,9,23,432.65}; + double cnt = 6; + double max = -9999.0; + + char const *arr2[5] = {"erqerdsf","dfa","trtrt","trt-oerw","rqwer"}; + + max = ReturnMax(arr1,cnt); + + std::cout< +T1 *ReturnMax(T1 *a[],T2 b) +{ + T1 *addr = NULL; + int len = -1; + for(int i=0;i len) + { + len = tmp_len; + addr = a[i]; + } + } + return addr; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter8Test7.cpp" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter8Test7.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..47dfc56f8fed34258c1ae683a81a5eb79a87a30c --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/Chapter8Test7.cpp" @@ -0,0 +1,57 @@ +#include + +template +T ShowArray(T a[],int n); + +template +T ShowArray(T *a[],int n); + +struct debts +{ + char name[50]; + double amount; +}; + +int main() +{ + int things[6] = {13,31,103,301,310,130}; + struct debts mr_E[3] = + { + {"IMa woof",2400.0}, + {"fadif afda",1300.0}, + {"idj adf2d",1800.0} + }; + + double *pt[3]; + for(int i=0;i<3;i++) + { + pt[i] = &mr_E[i].amount; + } + + std::cout<<"things:"< +T ShowArray(T a[],int n) +{ + T count = 0; + for(int i=0;i +T ShowArray(T *a[],int n) +{ + T count = 0; + for(int i=0;i +#include +#include + +const int SIZE = 512; + + +struct chaff +{ + char dross[20]; + int slag; +}; + +int main() +{ + char *buffer = new char[SIZE]; + struct chaff *MyChaff1 = new (buffer) chaff; + struct chaff *MyChaff2 = new (buffer + sizeof(struct chaff)) chaff; + + strcpy(MyChaff1->dross,"fjdajlkljdkaljkk"); + strcpy(MyChaff2->dross,"]fakhjkffjl"); + + std::cout<dross<dross< + +// Detect target's platform and set some macros in order to wrap platform +// specific code this library depends on. +#if defined(_WIN32) || defined(_WIN64) +# define TERMCOLOR_TARGET_WINDOWS +#elif defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) +# define TERMCOLOR_TARGET_POSIX +#endif + +// If implementation has not been explicitly set, try to choose one based on +// target platform. +#if !defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) && !defined(TERMCOLOR_USE_WINDOWS_API) && !defined(TERMCOLOR_USE_NOOP) +# if defined(TERMCOLOR_TARGET_POSIX) +# define TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES +# define TERMCOLOR_AUTODETECTED_IMPLEMENTATION +# elif defined(TERMCOLOR_TARGET_WINDOWS) +# define TERMCOLOR_USE_WINDOWS_API +# define TERMCOLOR_AUTODETECTED_IMPLEMENTATION +# endif +#endif + +// These headers provide isatty()/fileno() functions, which are used for +// testing whether a standard stream refers to the terminal. +#if defined(TERMCOLOR_TARGET_POSIX) +# include +#elif defined(TERMCOLOR_TARGET_WINDOWS) +# include +# include +#endif + + +namespace termcolor +{ + // Forward declaration of the `_internal` namespace. + // All comments are below. + namespace _internal + { + inline int colorize_index(); + inline FILE* get_standard_stream(const std::ostream& stream); + inline FILE* get_standard_stream(const std::wostream& stream); + template + bool is_colorized(std::basic_ostream& stream); + template + bool is_atty(const std::basic_ostream& stream); + + #if defined(TERMCOLOR_TARGET_WINDOWS) + template + void win_change_attributes(std::basic_ostream& stream, int foreground, int background = -1); + #endif + } + + template + std::basic_ostream& colorize(std::basic_ostream& stream) + { + stream.iword(_internal::colorize_index()) = 1L; + return stream; + } + + template + std::basic_ostream& nocolorize(std::basic_ostream& stream) + { + stream.iword(_internal::colorize_index()) = 0L; + return stream; + } + + template + std::basic_ostream& reset(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[00m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, -1); + #endif + } + return stream; + } + + template + std::basic_ostream& bold(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[1m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + #endif + } + return stream; + } + + template + std::basic_ostream& dark(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[2m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + #endif + } + return stream; + } + + template + std::basic_ostream& italic(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[3m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + #endif + } + return stream; + } + + template + std::basic_ostream& underline(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[4m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, COMMON_LVB_UNDERSCORE); + #endif + } + return stream; + } + + template + std::basic_ostream& blink(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[5m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + #endif + } + return stream; + } + + template + std::basic_ostream& reverse(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[7m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + #endif + } + return stream; + } + + template + std::basic_ostream& concealed(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[8m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + #endif + } + return stream; + } + + template + std::basic_ostream& crossed(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[9m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + #endif + } + return stream; + } + + template + std::basic_ostream& color(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[38;5;" << +code << "m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + #endif + } + return stream; + } + + template + std::basic_ostream& on_color(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[48;5;" << +code << "m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + #endif + } + return stream; + } + + template + std::basic_ostream& color(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[38;2;" << +r << ";" << +g << ";" << +b << "m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + #endif + } + return stream; + } + + template + std::basic_ostream& on_color(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[48;2;" << +r << ";" << +g << ";" << +b << "m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + #endif + } + return stream; + } + + template + std::basic_ostream& grey(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[30m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + 0 // grey (black) + ); + #endif + } + return stream; + } + + template + std::basic_ostream& red(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[31m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_RED + ); + #endif + } + return stream; + } + + template + std::basic_ostream& green(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[32m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_GREEN + ); + #endif + } + return stream; + } + + template + std::basic_ostream& yellow(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[33m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_GREEN | FOREGROUND_RED + ); + #endif + } + return stream; + } + + template + std::basic_ostream& blue(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[34m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_BLUE + ); + #endif + } + return stream; + } + + template + std::basic_ostream& magenta(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[35m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_BLUE | FOREGROUND_RED + ); + #endif + } + return stream; + } + + template + std::basic_ostream& cyan(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[36m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_BLUE | FOREGROUND_GREEN + ); + #endif + } + return stream; + } + + template + std::basic_ostream& white(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[37m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED + ); + #endif + } + return stream; + } + + + template + std::basic_ostream& bright_grey(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[90m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + 0 | FOREGROUND_INTENSITY // grey (black) + ); + #endif + } + return stream; + } + + template + std::basic_ostream& bright_red(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[91m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_RED | FOREGROUND_INTENSITY + ); + #endif + } + return stream; + } + + template + std::basic_ostream& bright_green(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[92m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_GREEN | FOREGROUND_INTENSITY + ); + #endif + } + return stream; + } + + template + std::basic_ostream& bright_yellow(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[93m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY + ); + #endif + } + return stream; + } + + template + std::basic_ostream& bright_blue(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[94m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_BLUE | FOREGROUND_INTENSITY + ); + #endif + } + return stream; + } + + template + std::basic_ostream& bright_magenta(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[95m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY + ); + #endif + } + return stream; + } + + template + std::basic_ostream& bright_cyan(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[96m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY + ); + #endif + } + return stream; + } + + template + std::basic_ostream& bright_white(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[97m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, + FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY + ); + #endif + } + return stream; + } + + + template + std::basic_ostream& on_grey(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[40m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + 0 // grey (black) + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_red(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[41m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_RED + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_green(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[42m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_GREEN + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_yellow(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[43m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_GREEN | BACKGROUND_RED + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_blue(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[44m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_BLUE + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_magenta(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[45m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_BLUE | BACKGROUND_RED + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_cyan(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[46m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_GREEN | BACKGROUND_BLUE + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_white(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[47m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED + ); + #endif + } + + return stream; + } + + + template + std::basic_ostream& on_bright_grey(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[100m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + 0 | BACKGROUND_INTENSITY // grey (black) + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_bright_red(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[101m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_RED | BACKGROUND_INTENSITY + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_bright_green(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[102m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_GREEN | BACKGROUND_INTENSITY + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_bright_yellow(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[103m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_bright_blue(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[104m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_BLUE | BACKGROUND_INTENSITY + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_bright_magenta(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[105m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_bright_cyan(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[106m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY + ); + #endif + } + return stream; + } + + template + std::basic_ostream& on_bright_white(std::basic_ostream& stream) + { + if (_internal::is_colorized(stream)) + { + #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) + stream << "\033[107m"; + #elif defined(TERMCOLOR_USE_WINDOWS_API) + _internal::win_change_attributes(stream, -1, + BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY + ); + #endif + } + + return stream; + } + + + + //! Since C++ hasn't a way to hide something in the header from + //! the outer access, I have to introduce this namespace which + //! is used for internal purpose and should't be access from + //! the user code. + namespace _internal + { + // An index to be used to access a private storage of I/O streams. See + // colorize / nocolorize I/O manipulators for details. Due to the fact + // that static variables ain't shared between translation units, inline + // function with local static variable is used to do the trick and share + // the variable value between translation units. + inline int colorize_index() + { + static int colorize_index = std::ios_base::xalloc(); + return colorize_index; + } + + //! Since C++ hasn't a true way to extract stream handler + //! from the a given `std::ostream` object, I have to write + //! this kind of hack. + inline + FILE* get_standard_stream(const std::ostream& stream) + { + if (&stream == &std::cout) + return stdout; + else if (&stream == &std::cerr || &stream == &std::clog) + return stderr; + + return nullptr; + } + + //! Since C++ hasn't a true way to extract stream handler + //! from the a given `std::wostream` object, I have to write + //! this kind of hack. + inline + FILE* get_standard_stream(const std::wostream& stream) + { + if (&stream == &std::wcout) + return stdout; + else if (&stream == &std::wcerr || &stream == &std::wclog) + return stderr; + + return nullptr; + } + + // Say whether a given stream should be colorized or not. It's always + // true for ATTY streams and may be true for streams marked with + // colorize flag. + template + bool is_colorized(std::basic_ostream& stream) + { + return is_atty(stream) || static_cast(stream.iword(colorize_index())); + } + + //! Test whether a given `std::ostream` object refers to + //! a terminal. + template + bool is_atty(const std::basic_ostream& stream) + { + FILE* std_stream = get_standard_stream(stream); + + // Unfortunately, fileno() ends with segmentation fault + // if invalid file descriptor is passed. So we need to + // handle this case gracefully and assume it's not a tty + // if standard stream is not detected, and 0 is returned. + if (!std_stream) + return false; + + #if defined(TERMCOLOR_TARGET_POSIX) + return ::isatty(fileno(std_stream)); + #elif defined(TERMCOLOR_TARGET_WINDOWS) + return ::_isatty(_fileno(std_stream)); + #else + return false; + #endif + } + + #if defined(TERMCOLOR_TARGET_WINDOWS) + + //! same hack as used in get_standard_stream function, but for Windows with `std::ostream` + inline HANDLE get_terminal_handle(std::ostream& stream) + { + if (&stream == &std::cout) + return GetStdHandle(STD_OUTPUT_HANDLE); + else if (&stream == &std::cerr || &stream == &std::clog) + return GetStdHandle(STD_ERROR_HANDLE); + return nullptr; + } + + //! same hack as used in get_standard_stream function, but for Windows with `std::wostream` + inline HANDLE get_terminal_handle(std::wostream& stream) + { + if (&stream == &std::wcout) + return GetStdHandle(STD_OUTPUT_HANDLE); + else if (&stream == &std::wcerr || &stream == &std::wclog) + return GetStdHandle(STD_ERROR_HANDLE); + return nullptr; + } + + //! Change Windows Terminal colors attribute. If some + //! parameter is `-1` then attribute won't changed. + template + void win_change_attributes(std::basic_ostream& stream, int foreground, int background) + { + // yeah, i know.. it's ugly, it's windows. + static WORD defaultAttributes = 0; + + // Windows doesn't have ANSI escape sequences and so we use special + // API to change Terminal output color. That means we can't + // manipulate colors by means of "std::stringstream" and hence + // should do nothing in this case. + if (!_internal::is_atty(stream)) + return; + + // get terminal handle + HANDLE hTerminal = INVALID_HANDLE_VALUE; + hTerminal = get_terminal_handle(stream); + + // save default terminal attributes if it unsaved + if (!defaultAttributes) + { + CONSOLE_SCREEN_BUFFER_INFO info; + if (!GetConsoleScreenBufferInfo(hTerminal, &info)) + return; + defaultAttributes = info.wAttributes; + } + + // restore all default settings + if (foreground == -1 && background == -1) + { + SetConsoleTextAttribute(hTerminal, defaultAttributes); + return; + } + + // get current settings + CONSOLE_SCREEN_BUFFER_INFO info; + if (!GetConsoleScreenBufferInfo(hTerminal, &info)) + return; + + if (foreground != -1) + { + info.wAttributes &= ~(info.wAttributes & 0x0F); + info.wAttributes |= static_cast(foreground); + } + + if (background != -1) + { + info.wAttributes &= ~(info.wAttributes & 0xF0); + info.wAttributes |= static_cast(background); + } + + SetConsoleTextAttribute(hTerminal, info.wAttributes); + } + #endif // TERMCOLOR_TARGET_WINDOWS + + } // namespace _internal + +} // namespace termcolor + + +#undef TERMCOLOR_TARGET_POSIX +#undef TERMCOLOR_TARGET_WINDOWS + +#if defined(TERMCOLOR_AUTODETECTED_IMPLEMENTATION) +# undef TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES +# undef TERMCOLOR_USE_WINDOWS_API +#endif + +#endif // TERMCOLOR_HPP_ diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/out.txt" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/out.txt" new file mode 100644 index 0000000000000000000000000000000000000000..fb204877700faa4effed9fd1c30a8042b7ba983f --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/out.txt" @@ -0,0 +1,178 @@ +当前路径为: 63 + +_tmp_list 43 42 84 +第0次的open_list 43 42 84 +第0次删除后的open_list 43 42 +第0次的close_list 63 +当前路径为: 63 84 + +_tmp_list 103 104 105 +第1次的open_list 43 42 103 104 105 +第1次删除后的open_list 43 42 103 104 +第1次的close_list 63 84 +当前路径为: 63 84 105 + +_tmp_list 125 +第2次的open_list 43 42 103 104 125 +第2次删除后的open_list 43 42 103 104 +第2次的close_list 63 84 105 +当前路径为: 63 84 105 125 + +_tmp_list 145 146 +第3次的open_list 43 42 103 104 145 146 +第3次删除后的open_list 43 42 103 104 145 +第3次的close_list 63 84 105 125 +当前路径为: 63 84 105 125 146 + +_tmp_list 147 127 165 166 167 +第4次的open_list 43 42 103 104 145 147 127 165 166 167 +第4次删除后的open_list 43 42 103 104 145 147 127 165 166 +第4次的close_list 63 84 105 125 146 +当前路径为: 63 84 105 125 146 167 + +_tmp_list 168 148 186 187 +第5次的open_list 43 42 103 104 145 147 127 165 166 168 148 186 187 +第5次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 187 +第5次的close_list 63 84 105 125 146 167 +当前路径为: 63 84 105 125 146 167 168 + +_tmp_list 149 +第6次的open_list 43 42 103 104 145 147 127 165 166 148 186 187 149 +第6次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 +第6次的close_list 63 84 105 125 146 167 168 +当前路径为: 63 84 105 125 146 167 187 + +_tmp_list 206 207 208 +第7次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 208 +第7次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 +第7次的close_list 63 84 105 125 146 167 168 187 +当前路径为: 63 84 105 125 146 167 187 208 + +_tmp_list 209 228 229 +第8次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 229 +第8次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 +第8次的close_list 63 84 105 125 146 167 168 187 208 +当前路径为: 63 84 105 125 146 167 187 208 229 + +_tmp_list 210 248 +第9次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 210 248 +第9次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 +第9次的close_list 63 84 105 125 146 167 168 187 208 229 +当前路径为: 63 84 105 125 146 167 187 208 229 210 + +_tmp_list 211 191 231 +第10次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 231 +第10次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 +第10次的close_list 63 84 105 125 146 167 168 187 208 229 210 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 + +_tmp_list 232 251 252 +第11次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 252 +第11次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 +第11次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 + +_tmp_list 253 271 +第12次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 253 271 +第12次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 271 +第12次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 252 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 253 + +_tmp_list +第13次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 271 +第13次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 +第13次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 252 253 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 271 + +_tmp_list 291 +第14次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 291 +第14次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 +第14次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 252 253 271 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 271 291 + +_tmp_list 310 311 +第15次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 311 +第15次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 +第15次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 252 253 271 291 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 271 291 311 + +_tmp_list 330 331 332 +第16次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 332 +第16次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 +第16次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 252 253 271 291 311 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 271 291 311 332 + +_tmp_list 333 313 353 +第17次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 353 +第17次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 +第17次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 252 253 271 291 311 332 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 271 291 311 332 353 + +_tmp_list 354 373 +第18次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 354 373 +第18次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 373 +第18次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 252 253 271 291 311 332 353 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 271 291 311 332 353 354 + +_tmp_list 355 +第19次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 373 355 +第19次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 373 +第19次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 252 253 271 291 311 332 353 354 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 271 291 311 332 353 354 355 + +_tmp_list 356 336 376 +第20次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 373 356 336 376 +第20次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 373 356 336 +第20次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 252 253 271 291 311 332 353 354 355 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 271 291 311 332 353 354 355 376 + +_tmp_list 377 357 396 397 +第21次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 373 356 336 377 357 396 397 +第21次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 373 356 336 377 357 396 +第21次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 252 253 271 291 311 332 353 354 355 376 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 271 291 311 332 353 354 355 376 397 + +_tmp_list 398 378 417 +第22次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 373 356 336 377 357 396 398 378 417 +第22次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 373 356 336 377 357 396 398 378 +第22次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 252 253 271 291 311 332 353 354 355 376 397 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 271 291 311 332 353 354 355 376 397 417 + +_tmp_list 436 437 438 +第23次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 373 356 336 377 357 396 398 378 436 437 438 +第23次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 373 356 336 377 357 396 398 378 436 438 +第23次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 252 253 271 291 311 332 353 354 355 376 397 417 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 271 291 311 332 353 354 355 376 397 417 437 + +_tmp_list 457 458 +第24次的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 373 356 336 377 357 396 398 378 436 438 457 458 +第24次删除后的open_list 43 42 103 104 145 147 127 165 166 148 186 149 206 207 209 228 248 211 191 232 251 310 330 331 333 313 373 356 336 377 357 396 398 378 436 438 458 +第24次的close_list 63 84 105 125 146 167 168 187 208 229 210 231 252 253 271 291 311 332 353 354 355 376 397 417 437 +当前路径为: 63 84 105 125 146 167 187 208 229 210 231 252 271 291 311 332 353 354 355 376 397 417 437 457 + +O O O O * O * O * O O O O O O O * O O O 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 +O O O O * * * O O * O O * * O O O O O O 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 +O * * * O * O O O * O O O * * * * * * O 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 +O * O S O O O * * * * O * * * O * O * * 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 +* O O O 1 O O * * * O * O * * * * * * O 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 +O * * * * 1 O * * O * O O * * * * O * O 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 +O * * * O 1 O * O O * O * * * * * O O * 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 +O O O * O * 1 * * * * * O O O * * * * * 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 +O * O O * * * 1 * O * * * * * O O O * * 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 +* * * * O O * 1 O O O * * O * * O * O O 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 +* * * * * * * * 1 * 1 * O * O * O O O * 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 +* O * * * * * O * 1 O 1 * O O * * * * * 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 +O * * * * * O O * O O * 1 * O * O O * O 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 +O * * * O O O O * * O 1 O O O * O * O O 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 +* O O O O * * * * O O 1 O * * * * O * * 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 +* * * * * O O O * * * 1 O * * O * * O * 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 +O O O * * O O O * * * * 1 * O O * * * O 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 +O O * O O * O * * * * O O 1 1 1 * * * O 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 +O O * O * * O * O * O * O * O O 1 * * * 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 +* O * * * * O * * * O * O * * O * 1 * * 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 +O * O * O * O * * * * * * * * * O 1 O * 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 +O * O * O * * * * O * * O * O * * 1 * * 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 +O * * * O O * * * * * * * * * * O E * O 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 +O * O * * * * * * O * * * * * O * * * O 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 +O O * * * * O * O O * * * O O * * O O * 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 +有通路,路径为: 63 84 105 125 146 167 187 208 229 210 231 252 271 291 311 332 353 354 355 376 397 417 437 457 \ No newline at end of file diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/readme.md" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/readme.md" new file mode 100644 index 0000000000000000000000000000000000000000..70c8ace18216f7a6a845aa85de86f36a809949f6 --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/readme.md" @@ -0,0 +1,25 @@ +算法流程 + 初始化地图,设置起点终点和障碍物,从起点开始,将其周围的点加入到开放列表里并计算法f值,选择f值最小的点作为新的起点, +若该点是终点,则计算路径,输出地图和路径。若不是则继续上述操作,直到找到终点或者所有点都访问完。 + +临时列表 + 访问每个起点时,将该起点周围且没有加入到开放列表和关闭列表里面的点,加入到列表中,然后合并到开放列表,在开放列表里找 +f值最小的点。 + +路径回溯 + 当前起点向外扩展时,将被扩展到的节点的父节点参数标记为当前起点(MyPoint * _father_point = this->point),如果 +被扩展到的节点父节点不为空,则说明已经访问过,不再更改父节点。由于是广度优先扩展的,所以每个节点很容易知道从哪个方向的哪个 +节点来的,从当前终点开始,访问其父节点,直到找到原始起点,被访问到的点组成路径。 + +"S"表示起点 +"E"表示终点 +"O"表示障碍物 +"*"表示可行走点 +"1"表示最终的路径节点 + +如何编译运行项目 +如果已经编译过项目则cd build && make clean +cd build +cmake .. +make +./bin/main(可重复执行,每次结果都有变化) \ No newline at end of file diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/CMakeLists.txt" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..b6fc15997f5b278526ad7d7a8d9b69da3a55dd2f --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/CMakeLists.txt" @@ -0,0 +1,20 @@ +add_executable(main main.cpp) + +add_subdirectory(MyAStar) +add_subdirectory(MyMap) +add_subdirectory(MyPoint) + +target_include_directories(main +PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/MyAStar + ${CMAKE_CURRENT_SOURCE_DIR}/MyMap + ${CMAKE_CURRENT_SOURCE_DIR}/MyPoint +) + +target_link_libraries(main +PRIVATE + astar_lib + map_lib + point_lib + termcolor +) \ No newline at end of file diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyAStar/CMakeLists.txt" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyAStar/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..9c01d0f00f1631e76a5f554d567e8336be036364 --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyAStar/CMakeLists.txt" @@ -0,0 +1,13 @@ +add_library(astar_lib "") + +target_sources(astar_lib +PRIVATE + MyAStar.cpp +PUBLIC + MyAStar.hpp +) + +target_include_directories(astar_lib +PUBLIC + ${CMAKE_CURRENT_LIST_DIR} +) \ No newline at end of file diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyAStar/MyAStar.cpp" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyAStar/MyAStar.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..2aac7691d6eac4e4437656df995fec652e462a62 --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyAStar/MyAStar.cpp" @@ -0,0 +1,429 @@ +#include "MyAStar.hpp" + +MyAStar::MyAStar():_out_file("out.txt") +{ + map = nullptr; + _map_length = 0; + _map_width = 0; + _end_point = nullptr; + _start_point = nullptr; + _no_path = false; +} + +MyAStar::~MyAStar() +{ + +} + +void MyAStar::initial_map() +{ + set_map_length_width(25,20); + map = new MyMap(_map_length,_map_width); + map->initial_map(); + set_start_end(3,3,10,10); + set_obstruction(200); +} + +void MyAStar::set_start_end(int start_x, int start_y, int end_x, int end_y) +{ + + std::random_device rd; // 获得一个随机种子 + std::mt19937 gen(rd()); // 使用Mersenne Twister算法的随机数引擎 + + // 创建一个随机数分布(整数) + std::uniform_int_distribution<> dis_int(1, 1000); // 在1到100之间的均匀分布 + + int tmp = 0; + tmp = (abs(start_x-end_x)+abs(start_y-end_y))/2; + while (tmp < ((_map_length+_map_width)/3)) + { + start_x = dis_int(gen)%_map_length; + start_y = dis_int(gen)%_map_width; + end_x = dis_int(gen)%_map_length; + end_y = dis_int(gen)%_map_width; + + tmp = (abs(start_x-end_x)+abs(start_y-end_y))/2; + } + + + + + map->_map_points[start_x][start_y]->set_start_point(); + _start_point = map->_map_points[start_x][start_y]; + _start_point->set_start_point(); + map->_map_points[end_x][end_y]->set_end_point(); + _end_point = map->_map_points[end_x][end_y]; + _end_point->set_end_point(); +} + +void MyAStar::set_obstruction(int n) +{ + // 设置随机数生成器 + std::random_device rd; // 用于种子 + std::mt19937 gen(rd()); // 采用 Mersenne Twister 算法的随机数生成器 + + // 生成一个范围在 [0, 99] 之间的整数 + std::uniform_int_distribution<> dis_x(0, _map_length); + std::uniform_int_distribution<> dis_y(0, _map_width); + // 生成并打印 10 个随机数 + + int count = 0; + while (count < n) + { + int l = dis_x(gen)%_map_length; + int w = dis_y(gen)%_map_width; + if(map->_map_points[l][w]->is_start_point() || map->_map_points[l][w]->is_end_point()\ + || map->_map_points[l][w]->get_obstruction_station()) + { + continue; + } + map->_map_points[l][w]->set_obstruction_station(true); + count++; + } + +} + +void MyAStar::set_map_length_width(int length, int width) +{ + _map_length = length; + _map_width = width; +} + +void MyAStar::draw_map() +{ + for(int i=0;i<_map_length;i++) + { + for(int j=0;j<_map_width;j++) + { + if(map->_map_points[i][j]->get_obstruction_station()) + { + std::cout<<"O "; + _out_file<<"O "; + } + else if(map->_map_points[i][j]->is_start_point()) + { + //std::cout<<"S "; + //_out_file<<"S "; + std::cout << termcolor::red << "S " << termcolor::reset; + _out_file << termcolor::red << "S " << termcolor::reset; + } + else if(map->_map_points[i][j]->is_end_point()) + { + //std::cout<<"E "; + //_out_file<<"E "; + std::cout << termcolor::red << "E " << termcolor::reset; + _out_file << termcolor::red << "E " << termcolor::reset; + } + else + { + auto it = find(_path.begin(),_path.end(),map->_map_points[i][j]); + if(it != _path.end()) + { + std::cout << termcolor::red << "1 " << termcolor::reset; + _out_file << termcolor::red << "1 " << termcolor::reset; + } + else + { + std::cout<<"* "; + _out_file<<"* "; + } + + } + } + std::cout<<" "; + _out_file<<" "; + for(int k=0;k<_map_width;k++) + { + std::cout << std::right << std::setw(5) << map->_map_points[i][k]->get_id(); + _out_file<< std::right << std::setw(5) << map->_map_points[i][k]->get_id(); + } + std::cout<get_id()<<" "; + _out_file<<(*it)->get_id()<<" "; + } + std::cout<set_father_point(p); +} + +MyPoint* MyAStar::get_start_point() +{ + return _start_point; +} + +double MyAStar::calculate_f_value(MyPoint* p1,MyPoint* p2) +{ + double fv = 0.0; + MyPoint * tmp2 = _end_point; + int end_x = tmp2->get_coordinate_X(); + int end_y = tmp2->get_coordinate_Y(); + int p1_x = p1->get_coordinate_X(); + int p1_y = p1->get_coordinate_Y(); + int p2_x = p2->get_coordinate_X(); + int p2_y = p2->get_coordinate_Y(); + + + fv = sqrt((p1_x-p2_x)*(p1_x-p2_x)+(p1_y-p2_y)*(p1_y-p2_y))+abs(p2_x-end_x)+abs(p2_y-end_y); + return fv; +} + +MyPoint* MyAStar::id_to_point(int id) +{ + for(int i=0;i<_map_length;i++) + { + for(int j=0;j<_map_width;j++) + { + if(map->_map_points[i][j]->get_id() == id) + { + return map->_map_points[i][j]; + } + } + } + return nullptr; +} + +void MyAStar::push_open_list(MyPoint* p) +{ + _open_list.push_back(p); +} + +void MyAStar::pop_open_list(MyPoint* p) +{ + auto it = std::find(_open_list.begin(),_open_list.end(),p); + if(it != _open_list.end()) + { + _open_list.erase(it); + } +} + +void MyAStar::push_close_list(MyPoint* p) +{ + _close_list.push_back(p); +} + +void MyAStar::pop_close_list(MyPoint* p) +{ + auto it = std::find(_close_list.begin(),_close_list.end(),p); + if(it != _close_list.end()) + { + _close_list.erase(it); + } +} + +void MyAStar::update_path(MyPoint* p) +{ + MyPoint *tmp = p; + _path.clear(); + while (1) + { + if(tmp == _start_point) + { + _path.push_back(tmp); + break; + } + _path.push_back(tmp); + tmp = tmp->get_father_point(); + } + tmp = nullptr; +} + +void MyAStar::a_star() +{ + //初始化地图 + initial_map(); + MyPoint * tmp_start_point = get_start_point(); + std::cout<get_id()<<" "<get_id()<get_id()<<" "; + } + _out_file<set_f_value(f); + _open_list.push_back(point); + } + + //取open_list里面f值最小的 + for(auto it = _open_list.begin();it != _open_list.end();it++) + { + double f = (*it)->get_f_value(); + if(tmp_fv > f) + { + tmp_fv = f; + tmp_start_point = *it; + } + } + //检查开放列表和关闭列表,后续为打印当前运行信息可不管 + _out_file<<"第"<get_id()<<" "; + } + _out_file<get_id()<<" "; + } + _out_file<get_id()<<" "; + } + _out_file<get_coordinate_X(); + int p_y = p->get_coordinate_Y(); + + _tmp_list.clear(); + + if((p_y+1) < _map_width) + { + _tmp_list.push_back(id_to_point(p_x * _map_width + p_y + 1)); + } + if(((p_x-1) >=0) && ((p_y+1) < _map_width)) + { + _tmp_list.push_back(id_to_point((p_x-1) * _map_width + p_y +1)); + } + if(((p_x-1) >= 0)) + { + _tmp_list.push_back(id_to_point((p_x-1) * _map_width + p_y)); + } + if(((p_x-1) >=0) && ((p_y-1)>= 0)) + { + _tmp_list.push_back(id_to_point((p_x-1) * _map_width + p_y -1)); + } + if((p_y-1) >= 0) + { + _tmp_list.push_back(id_to_point((p_x) * _map_width + p_y -1)); + } + if(((p_x+1) < _map_length) && ((p_y-1) >= 0)) + { + _tmp_list.push_back(id_to_point((p_x+1) * _map_width + p_y -1)); + } + if((p_x+1) < _map_length) + { + _tmp_list.push_back(id_to_point((p_x+1) * _map_width + p_y)); + } + if(((p_x+1) < _map_length) && ((p_y+1) < _map_width)) + { + _tmp_list.push_back(id_to_point((p_x+1) * _map_width + p_y + 1)); + } + +//这里容易出问题 + for(auto it = _tmp_list.begin();it != _tmp_list.end();) + { + if(std::find(_open_list.begin(),_open_list.end(),*it) != _open_list.end()) + { + it = _tmp_list.erase(it); + } + else if(std::find(_close_list.begin(),_close_list.end(),*it) != _close_list.end()) + { + it = _tmp_list.erase(it); + } + else if((*it)->get_obstruction_station()) + { + it = _tmp_list.erase(it); + } + else + { + it++; + } + } + + for(auto it = _tmp_list.begin(); it!= _tmp_list.end(); it++) + { + if(!(*it)->get_father_point()) + { + (*it)->set_father_point(p); + } + } + _out_file<<"_tmp_list "; + for(auto it = _tmp_list.begin();it != _tmp_list.end(); it++) + { + + _out_file<<(*it)->get_id()<<" "; + } + _out_file< +#include +#include +#include +#include +#include +#include "../../external/termcolor.hpp" + +/// @brief 算法接口,但是没封装好,应该只是一个接口工具 +class MyAStar +{ + +public: + MyAStar(/* args */); + ~MyAStar(); + /// @brief 初始化地图、选定起终点、设置障碍物个数 + void initial_map(); + void set_start_end(int start_x,int start_y,int end_x,int end_y); + void set_obstruction(int n); + void set_map_length_width(int length,int width); + void draw_map(); + void set_end_point(MyPoint *p); + MyPoint *get_end_point(); + void set_start_point(MyPoint *p); + MyPoint *get_start_point(); + + double calculate_f_value(MyPoint* p1,MyPoint* p2); + + /// @brief 用id找到对应的MyPoint对象 + /// @param id + /// @return 返回对应点对象 + MyPoint *id_to_point(int id); + void push_open_list(MyPoint* p); + void pop_open_list(MyPoint* p); + void push_close_list(MyPoint* p); + void pop_close_list(MyPoint* p); + /// @brief 每走一步,根据起点和当前点更新路径 + void update_path(MyPoint *p); + + /// @brief 主算法 + void a_star(); + + /// @brief 将当前点周围节点加入到_tmp_list中 + /// @param p 当前点 + void create_tmp_list(MyPoint* p); + +private: + MyMap *map; + /// @brief 长宽变量map类里也有,这里定义显得冗余了 + int _map_length; + int _map_width; + MyPoint *_end_point; + MyPoint *_start_point; + /// @brief 用于储存当前节点的周围节点 + std::vector _tmp_list; + /// @brief 保存路径,没走一个点更换一次 + std::vector _path; + /// @brief 程序运行文件,打印每次运行的关键数据结构用以检查异常 + std::ofstream _out_file; + bool _no_path; + /// @brief 开放列表,用来保存已经就算了f值的点,并作为候选点 + std::vector _open_list; + /// @brief 已经走过的列表,不再走了 + std::vector _close_list; + +}; + diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyMap/CMakeLists.txt" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyMap/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..72d6bf60574662134bbd27676fb5efc9e675dce6 --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyMap/CMakeLists.txt" @@ -0,0 +1,13 @@ +add_library(map_lib "") + +target_sources(map_lib +PRIVATE + MyMap.cpp +PUBLIC + MyMap.hpp +) + +target_include_directories(map_lib +PUBLIC + ${CMAKE_CURRENT_LIST_DIR} +) \ No newline at end of file diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyMap/MyMap.cpp" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyMap/MyMap.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..d8e95938c48d9629b5ea7f39b7d8e07122bafde6 --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyMap/MyMap.cpp" @@ -0,0 +1,81 @@ +#include"MyMap.hpp" + +MyMap::MyMap(int length,int width):_map_length(length),_map_width(width) +{ + _map_points_number = length * width; + + _map_points.resize(_map_length); + for (size_t i = 0; i < _map_length; ++i) { + _map_points[i].resize(_map_width, nullptr); + } + + //initial_map(); +} + +MyMap::~MyMap() +{ + for (size_t i = 0; i < _map_length; ++i) { + for (size_t j = 0; j < _map_width; ++j) { + delete _map_points[i][j]; + } + } +} + +double MyMap::calculate_linear_distance(MyPoint p1, MyPoint p2) +{ + int p1_x,p1_y; + int p2_x,p2_y; + + p1_x = p1.get_coordinate_X(); + p1_y = p1.get_coordinate_Y(); + p2_x = p2.get_coordinate_X(); + p2_y = p2.get_coordinate_Y(); + + return std::sqrt((p1_x-p2_x)*(p1_x-p2_x) + (p1_y-p2_y)*(p1_y-p2_y)); +} + +double MyMap::calculate_manhattan_distance(MyPoint p1, MyPoint p2) +{ + int p1_x,p1_y; + int p2_x,p2_y; + + p1_x = p1.get_coordinate_X(); + p1_y = p1.get_coordinate_Y(); + p2_x = p2.get_coordinate_X(); + p2_y = p2.get_coordinate_Y(); + + + return (std::abs(p1_x-p2_x)+std::abs(p1_y-p2_y)); +} + +void MyMap::set_map_length(int length) +{ + _map_length = length; +} +void MyMap::set_map_width(int width) +{ + _map_width = width; +} +void MyMap::initial_map() +{ + for(int i=0;i<_map_length;i++) + { + for(int j=0;j<_map_width;j++) + { + MyPoint *tmp_point = new MyPoint(i,j,(i*_map_width)+j); + _map_points[i][j] = tmp_point; + } + } +} + +void MyMap::draw_map() +{ + for(int i=0;i<_map_length;i++) + { + for(int j=0;j<_map_width;j++) + { + std::cout<<"* "; + } + std::cout< +#include + +class MyMap +{ + +public: + MyMap(int length,int width); + ~MyMap(); + /// @brief 存储地图的数据结构 + std::vector>_map_points; + + /// @brief 起点与邻点的直线距离 + /// @param p1 当前起点 + /// @param p2 邻点 + /// @return 返回直线距离 + double calculate_linear_distance(MyPoint p1,MyPoint p2); + /// @brief 邻点与终点的曼哈顿距离,这两个函数冗余了,不需要这么细 + /// @param p1 邻点 + /// @param p2 终点 + double calculate_manhattan_distance(MyPoint p1,MyPoint p2); + void set_map_length(int length); + void set_map_width(int width); + + /// @brief 实例化地图节点 + void initial_map(); + void draw_map(); + +private: + + int _map_length; + int _map_width; + /// @brief 地图上的节点数 + int _map_points_number; + +}; + + + +#endif \ No newline at end of file diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyPoint/CMakeLists.txt" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyPoint/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..79cf48ad4717e73dd32391b2052d82362f6f0332 --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyPoint/CMakeLists.txt" @@ -0,0 +1,13 @@ +add_library(point_lib "") + +target_sources(point_lib +PRIVATE + MyPoint.cpp +PUBLIC + MyPoint.hpp +) + +target_include_directories(point_lib +PUBLIC + ${CMAKE_CURRENT_LIST_DIR} +) \ No newline at end of file diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyPoint/MyPoint.cpp" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyPoint/MyPoint.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e84a89ad4840c04fa099797b80ad95c49c95fdd3 --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyPoint/MyPoint.cpp" @@ -0,0 +1,85 @@ +#include "MyPoint.hpp" + +MyPoint::MyPoint(int x,int y,int id):_coordinate_X(x),_coordinate_Y(y),_id(id) +{ + _is_obstruction = false; + _has_visit = false; + _end_point = false; + _start_point = false; + _father_point = nullptr; +} + +MyPoint::~MyPoint() +{ + +} + +int MyPoint::get_coordinate_X() const +{ + return _coordinate_X; +} + +int MyPoint::get_coordinate_Y() const +{ + return _coordinate_Y; +} + +void MyPoint::set_obstruction_station(bool obs_station) +{ + _is_obstruction = obs_station; +} + +void MyPoint::set_visit_station(bool visit_station) +{ + _has_visit = visit_station; +} + +int MyPoint::get_id() +{ + return _id; +} + +void MyPoint::set_end_point() +{ + _end_point = true; +} + +bool MyPoint::is_end_point() +{ + return _end_point; +} + +void MyPoint::set_start_point() +{ + _start_point = true; +} + +bool MyPoint::is_start_point() +{ + return _start_point; +} + +bool MyPoint::get_obstruction_station() +{ + return _is_obstruction; +} + +void MyPoint::set_father_point(MyPoint *p) +{ + _father_point = p; +} + +MyPoint *MyPoint::get_father_point() +{ + return _father_point; +} + +void MyPoint::set_f_value(double v) +{ + _f_value = v; +} + +double MyPoint::get_f_value() +{ + return _f_value; +} diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyPoint/MyPoint.hpp" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyPoint/MyPoint.hpp" new file mode 100644 index 0000000000000000000000000000000000000000..b5ebfa840fc6706d5b5a79e54e9c7924ff4e8f78 --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/MyPoint/MyPoint.hpp" @@ -0,0 +1,48 @@ +#ifndef MYPOINT_HPP +#define MYPOINT_HPP + +#include + + +/// @brief 点类,用于记录地图上的点及其父节点、存储f值以及一些地图的点属性 +class MyPoint +{ +public: + MyPoint(int x,int y,int id); + ~MyPoint(); + + int get_coordinate_X() const; + int get_coordinate_Y() const; + void set_obstruction_station(bool obs_station); + void set_visit_station(bool visit_station); + int get_id(); + + void set_end_point(); + bool is_end_point(); + void set_start_point(); + bool is_start_point(); + bool get_obstruction_station(); + + /// @brief 用于保存路径,因为用的是广度优先,所以知道终点和起点就能回溯整条路径 + /// @param p 点节点 + void set_father_point(MyPoint *p); + MyPoint *get_father_point(); + + /// @brief 设置保存f值,每个点的f值只算一遍 + void set_f_value(double v); + double get_f_value(); + +private: + int _coordinate_X; + int _coordinate_Y; + bool _is_obstruction; + bool _has_visit; + int _id; + bool _end_point; + bool _start_point; + MyPoint * _father_point; + double _f_value; + +}; + +#endif \ No newline at end of file diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/main.cpp" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e66f1c590c0ab743b7aeb83b1839ca86ebf6144c --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/src/main.cpp" @@ -0,0 +1,10 @@ + +#include"MyAStar.hpp" + +int main(int argc, char **argv) +{ + MyAStar a_star; + a_star.a_star(); + a_star.draw_map(); + return 0; +} diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/tests/CMakeLists.txt" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/tests/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..d90d690c887bcb300b658fd5fdd9fedff67fbfe8 --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/tests/CMakeLists.txt" @@ -0,0 +1,20 @@ +# tests/CMakeLists.txt +add_executable(test_MyAStar test_MyAStar.cpp) + +target_include_directories(test_MyAStar + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../src/MyAStar + ${CMAKE_CURRENT_SOURCE_DIR}/../src/MyMap + ${CMAKE_CURRENT_SOURCE_DIR}/../src/MyPoint +) + +target_link_libraries(test_MyAStar + gtest_main + astar_lib + map_lib + point_lib + termcolor +) + +include(GoogleTest) +gtest_discover_tests(test_MyAStar) diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/tests/test_MyAStar.cpp" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/tests/test_MyAStar.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..6b330c24fb04e6da5f719db95c0cd14b75dce310 --- /dev/null +++ "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/MyAStar/tests/test_MyAStar.cpp" @@ -0,0 +1,16 @@ +#include +#include "MyAStar.hpp" +#include "MyMap.hpp" +#include "MyPoint.hpp" + +TEST(MyAStarTest, SimpleTest) { + // 初始化测试对象 + + // 执行一些操作并断言结果 + ASSERT_TRUE(true); // 示例测试 +} + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git "a/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/test.txt" "b/C++/codes/2024-07/\345\256\213\345\275\254\345\275\254/test.txt" deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000