From 863a7e677c0188445a2ff1c5c751086bb6069c69 Mon Sep 17 00:00:00 2001 From: lizichao <1010635424@qq.com> Date: Thu, 11 Jul 2024 09:40:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Chapter7/7-10.cpp" | 27 +++++++++++ .../Chapter7/7-3.cpp" | 30 ++++++++++++ .../Chapter7/7-6.cpp" | 48 +++++++++++++++++++ .../Chapter8/8-1.cpp" | 25 ++++++++++ .../Chapter8/8-5.cpp" | 25 ++++++++++ .../Chapter8/8-6.cpp" | 44 +++++++++++++++++ 6 files changed, 199 insertions(+) create mode 100644 "C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter7/7-10.cpp" create mode 100644 "C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter7/7-3.cpp" create mode 100644 "C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter7/7-6.cpp" create mode 100644 "C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter8/8-1.cpp" create mode 100644 "C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter8/8-5.cpp" create mode 100644 "C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter8/8-6.cpp" diff --git "a/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter7/7-10.cpp" "b/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter7/7-10.cpp" new file mode 100644 index 0000000..9109270 --- /dev/null +++ "b/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter7/7-10.cpp" @@ -0,0 +1,27 @@ +#include +double caculate(double a, double b,double (*ptr)(double,double)); +inline double add(double x, double y) +{ + std::cout << "add: "; + return x + y; +} +inline double sub(double x, double y) +{ + std::cout << "sub: "; + return x - y; +} +int main() { + int x, y; + double(*pf[2]) (double, double) = { add,sub }; + while (std::cin >> x >> y) { + double a1=caculate(x, y, pf[0]); + std::cout << a1 << std::endl; + double a2 = caculate(x, y, pf[1]); + std::cout << a2 << std::endl; + } + +} +double caculate(double a, double b, double(*ptr)(double, double)) +{ + return (*ptr)(a, b); +} diff --git "a/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter7/7-3.cpp" "b/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter7/7-3.cpp" new file mode 100644 index 0000000..a1612f8 --- /dev/null +++ "b/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter7/7-3.cpp" @@ -0,0 +1,30 @@ +#include +using std::cout; +using std::endl; +struct box +{ + char maker[40]; + float height; + float width; + float length; + float volume; +}; +void show_box(const box b); +void setVolume(box *b); +int main() { + box testbox{ "testbox",10.0,20.0,30.0 }; //创建10*20*30的大小的箱子,未提供容量,默认为0.0 + show_box(testbox); //显示成员的值 + setVolume(&testbox); //设置volunme成员的值 + show_box(testbox); //重新显示成员的值 + return 0; +} + +void show_box(const box b) +{ + cout << b.maker << " " << b.height << " " << b.width << " " << b.length << " " << b.volume << endl; +} + +void setVolume(box * b) +{ + b->volume = b->height*b->length*b->width; +} diff --git "a/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter7/7-6.cpp" "b/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter7/7-6.cpp" new file mode 100644 index 0000000..b342278 --- /dev/null +++ "b/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter7/7-6.cpp" @@ -0,0 +1,48 @@ +#include +using std::cout; +using std::cin; +using std::endl; +int Fill_array(double arr[],int length); +void Show_array(const double arr[], int length); +void Reverse_array(double arr[], int length); +const int MAXSIZE = 10; + +int main() +{ + double test[MAXSIZE]; + int Len=Fill_array(test, 5); + Show_array(test, Len); + Reverse_array(test, Len); + Show_array(test, Len); +} +int Fill_array(double arr[], int length) +{ + cout << "please input double value" << endl; + double v; + int index = 0; + while (length--&&cin>>v) { + arr[index++] = v; + } + return index ; +} + +void Show_array(const double arr[], int length) +{ + cout << "the array is:" << endl; + for (int i = 0; i < length; i++) + cout << arr[i] << " "; + cout << endl; +} + +void Reverse_array(double arr[], int length) +{ + cout << "Reverse the array!" << endl; + int l = 0, r = length - 1; + while (l < r) { + double tmp = arr[l]; + arr[l] = arr[r]; + arr[r] = tmp; + l++; + r--; + } +} diff --git "a/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter8/8-1.cpp" "b/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter8/8-1.cpp" new file mode 100644 index 0000000..f645e09 --- /dev/null +++ "b/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter8/8-1.cpp" @@ -0,0 +1,25 @@ +#include +using std::cout; +using std::endl; +using std::cin; +void print_str(char *s, int i = 0); + +void print_str(char * s, int i) +{ + static int total = 0; + total++; + if (i == 0) { + cout << s; + } + else { + for (int j = 0; j < total; j++) + cout << s << endl; + } +} +int main() { + char str[6] = "abced"; + int i; + while (cin >> i) { + print_str(str, i); + } +} diff --git "a/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter8/8-5.cpp" "b/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter8/8-5.cpp" new file mode 100644 index 0000000..b7cc2f9 --- /dev/null +++ "b/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter8/8-5.cpp" @@ -0,0 +1,25 @@ +#include +using std::cout; +using std::endl; +using std::cin; +template +T Maxvalue(const T *arr); +int main() +{ + int arr1[5] = { 5,4,3,2,1 }; + double arr2[5] = { 5.5,4.4,3.3,2.2,1.1 }; + cout << "Max of int array: "; + cout << Maxvalue(arr1) << endl; + cout << "Max of double array: "; + cout << Maxvalue(arr2) << endl; +} + +template +T Maxvalue(const T * arr) +{ + T ans = arr[0]; + for (int i = 1; i < 5; i++) { + ans = ans > arr[i] ? ans : arr[i]; + } + return ans; +} diff --git "a/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter8/8-6.cpp" "b/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter8/8-6.cpp" new file mode 100644 index 0000000..9d183c6 --- /dev/null +++ "b/C++/codes/2024-07/\346\235\216\345\255\220\350\266\205/C++PrimerPlus\350\257\276\345\220\216\344\271\240\351\242\230/Chapter8/8-6.cpp" @@ -0,0 +1,44 @@ +#include +#include +using std::cout; +using std::endl; +using std::cin; +const char* maxn(const char* arr[], size_t n); +template +T maxn(const T *arr, size_t n); + +int main() +{ + int arr1[6] = { 1,5,3,7,9,13}; + double arr2[4] = { 5.3,10.3,20.4,1.1 }; + const char *str[5] = { "abc","df","gfaga","fa","n"}; + + cout << "Max of int array: "; + cout << maxn(arr1,6) << endl; + cout << "Max of double array: "; + cout << maxn(arr2,4) << endl; + cout << "longest of str array: "; + cout << maxn(str, 5) << endl; + +} +template +T maxn(const T * arr, size_t n) +{ + T ans = arr[0]; + for (size_t i = 1; i < n; i++) { + ans = ans > arr[i] ? ans : arr[i]; + } + return ans; +} + + +const char* maxn(const char* arr[], size_t n) { + const char* maxStr = arr[0]; + for (size_t i = 1; i < n; ++i) { + if (strlen(arr[i]) > strlen(maxStr)) { + maxStr = arr[i]; + } + } + return maxStr; +} + -- Gitee