diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/.gitignore" "b/C++/codes/2024-07/\351\273\204\345\261\261/.gitignore" new file mode 100644 index 0000000000000000000000000000000000000000..1cb83798b6420225ff38b8b0a8ad98d3ef42ebfd --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/.gitignore" @@ -0,0 +1,2 @@ +bin/ +build/ \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..ebf35c2118756ea67b4d4a25bddc1780288d70da --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/CMakeLists.txt" @@ -0,0 +1,5 @@ +PROJECT(exercises_7_10) +SET(SRC_LIST src/7-10.cpp) +ADD_EXECUTABLE(exercise_7_10 ${SRC_LIST}) + +set_target_properties(exercise_7_10 PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin") diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/src/7-10.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/src/7-10.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..1278b22869e85cafbec05e059099919e38d74795 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter7/src/7-10.cpp" @@ -0,0 +1,33 @@ +#include + +using namespace std; + +double add(double x, double y){ + return x + y; +} + +double multiply(double x, double y){ + return x * y; +} + +double calculate(double x, double y, double (*pf)(double, double)){ + return pf(x, y); +} + +int main(int argc, char **argv){ + double x, y; + while (true) + { + std::cout << "请输入两个数字(用空格分隔): "; + if(!(cin>>x>>y)){ + cout << "输入格式错误,退出" << endl; + break; + } + + double sum = calculate(x, y, add); + double product = calculate(x, y, multiply); + cout << x << "+" << y << "=" << sum << endl; + cout << x << "*" << y << "=" << product << endl; + } + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/CMakeLists.txt" "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/CMakeLists.txt" new file mode 100644 index 0000000000000000000000000000000000000000..53ac84f583b5b29e2e32644b81f5e3b379266c6d --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/CMakeLists.txt" @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.10) +project(chapter8) + +set(SOURCE_DIR ${CMAKE_SOURCE_DIR}/src) +set(BINARY_DIR ${CMAKE_SOURCE_DIR}/bin) + +file(MAKE_DIRECTORY ${BINARY_DIR}) + +file(GLOB SOURCES "${SOURCE_DIR}/*.cpp") + +foreach(SOURCE_FILE ${SOURCES}) + get_filename_component(FILENAME ${SOURCE_FILE} NAME_WE) + + add_executable("exercise_${FILENAME}" ${SOURCE_FILE}) + + set_target_properties("exercise_${FILENAME}" PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${BINARY_DIR} + ) +endforeach() diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-4.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-4.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..b34f61d5f9db245b8ee6639b344df51d4ad3a7a9 --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-4.cpp" @@ -0,0 +1,48 @@ +#include +#include +using namespace std; + +struct stringy +{ + char *str; //points to a string + int ct; //lenght of string(not counting '\0') +}; + +void set(stringy& to_string, char* from_string){ + int length_str = 0; + while (from_string[length_str] != '\0'){ + length_str++; + } + char *copy_string = new char[length_str + 1]; + for (int i = 0; i < length_str+1;++i){ + copy_string[i] = from_string[i]; + } + to_string.str = copy_string; + to_string.ct = length_str; +} + +void show(stringy &string, int count=1){ + for (int i = 0; i < count;++i){ + cout << string.str << endl; + } +} + +void show(char *string, int count=1){ + for (int i = 0; i < count;++i){ + cout << string << endl; + } +} + +int main(){ + stringy beany; + char testing[] = "Reality isn't what it used to be."; + set(beany, testing); + show(beany); + show(beany, 2); + testing[0] = 'D'; + testing[1] = 'u'; + show(testing); + show(testing, 3); + show("Done!"); + return 0; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-6.cpp" "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-6.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..ebb76ec7ede25f9616dad0332f7887d61597be1d --- /dev/null +++ "b/C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-6.cpp" @@ -0,0 +1,40 @@ +#include +#include +using namespace std; + +template +T maxn(T *nums, int counts){ + int max_i = 0; + for (int i = 1; i < counts;++i){ + if(nums[i] > nums[max_i]){ + max_i = i; + } + } + return nums[max_i]; +} + +template<> +char* maxn(char** strings, int counts){ + int max_i = 0; + for (int i = 1; i < counts;++i){ + if(strlen(strings[i]) > strlen(strings[i])){ + max_i = i; + } + } + return strings[max_i]; +} + +int main(int argc, char** argv){ + int int_arrays[6] = {0, 2, 3, 1, 5, 4}; + double double_arrays[4] = {0.5, -1.4, 4.5, 3.9}; + char *string_arrays[5] = {"hello", "world", "str", "bool", "int"}; + + int max_int = maxn(int_arrays, 6); + double max_double = maxn(double_arrays, 4); + char *max_string = maxn(string_arrays, 5); + + cout << "max int: " << max_int << endl; + cout << "max_double: " << max_double << endl; + cout << "max_string: " << max_string << endl; + return 0; +} \ No newline at end of file