From 002bec597f12f66a72412d90404437ff3e6abe79 Mon Sep 17 00:00:00 2001 From: sparklesea Date: Mon, 15 Jul 2024 04:59:00 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=AC=AC=E4=B8=83?= =?UTF-8?q?=E7=AB=A0=E4=B9=A0=E9=A2=987.10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\351\273\204\345\261\261/.gitignore" | 2 ++ .../chapter7/CMakeLists.txt" | 5 +++ .../chapter7/src/7-10.cpp" | 33 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/.gitignore" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/chapter7/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/chapter7/src/7-10.cpp" 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 0000000..1cb8379 --- /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 0000000..ebf35c2 --- /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 0000000..1278b22 --- /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 -- Gitee From abff818be97ae53812125da52d47128789774850 Mon Sep 17 00:00:00 2001 From: sparklesea Date: Mon, 15 Jul 2024 14:06:12 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=AC=AC=E5=85=AB?= =?UTF-8?q?=E7=AB=A0=E4=B9=A0=E9=A2=988-4,8-6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chapter8/CMakeLists.txt" | 19 ++++++++ .../chapter8/src/8-4.cpp" | 48 +++++++++++++++++++ .../chapter8/src/8-6.cpp" | 40 ++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/chapter8/CMakeLists.txt" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-4.cpp" create mode 100644 "C++/codes/2024-07/\351\273\204\345\261\261/chapter8/src/8-6.cpp" 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 0000000..53ac84f --- /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 0000000..b34f61d --- /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 0000000..ebb76ec --- /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 -- Gitee