From 3e49c318a75d891d4a25d099033105e579b87100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E7=8E=8B=E6=89=AB=E5=85=AD=E5=90=88?= <14633990+sjr0126@user.noreply.gitee.com> Date: Sun, 7 Jul 2024 11:38:44 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E8=8B=8F=E5=9D=9A?= =?UTF-8?q?=E8=8D=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/.keep" diff --git "a/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/.keep" "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From c558b3d1ebe21b1bb0ea41bbe4341be4c50b0ba7 Mon Sep 17 00:00:00 2001 From: SuJianrong <1014150286@qq.com> Date: Mon, 8 Jul 2024 20:08:22 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/test.txt" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/test.txt" diff --git "a/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/test.txt" "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/test.txt" new file mode 100644 index 0000000..e69de29 -- Gitee From c02b54eb6a6cf04bf9c3bfc2168b63bae39d84d3 Mon Sep 17 00:00:00 2001 From: SuJianrong <1014150286@qq.com> Date: Fri, 12 Jul 2024 16:23:36 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=8F=90=E4=BA=A4chapter7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../C++PrimerPlus/Chapter7/7_1.cpp" | 21 +++++ .../C++PrimerPlus/Chapter7/7_2.cpp" | 44 ++++++++++ .../C++PrimerPlus/Chapter7/7_3.cpp" | 40 +++++++++ .../C++PrimerPlus/Chapter7/7_4.cpp" | 30 +++++++ .../C++PrimerPlus/Chapter7/7_5.cpp" | 22 +++++ .../C++PrimerPlus/Chapter7/7_6.cpp" | 61 ++++++++++++++ .../C++PrimerPlus/Chapter7/7_7.cpp" | 81 +++++++++++++++++++ .../C++PrimerPlus/Chapter7/7_8.cpp" | 79 ++++++++++++++++++ .../C++PrimerPlus/Chapter7/7_9.cpp" | 72 +++++++++++++++++ .../C++PrimerPlus/Chapter7/CMakeLists.txt" | 21 +++++ 10 files changed, 471 insertions(+) create mode 100644 "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_1.cpp" create mode 100644 "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_2.cpp" create mode 100644 "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_3.cpp" create mode 100644 "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_4.cpp" create mode 100644 "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_5.cpp" create mode 100644 "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_6.cpp" create mode 100644 "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_7.cpp" create mode 100644 "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_8.cpp" create mode 100644 "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_9.cpp" create mode 100644 "C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/CMakeLists.txt" diff --git "a/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_1.cpp" "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_1.cpp" new file mode 100644 index 0000000..3fc984c --- /dev/null +++ "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_1.cpp" @@ -0,0 +1,21 @@ +#include +using namespace std; + +double Calculate(double x, double y) { + return 2.0 * x * y / (x + y); +} + +int main() +{ + double a, b; + + while (1) { + cout << "请输入两个数:" << endl; + cin >> a >> b; + if (a == 0 || b == 0) + break; + cout << "这两个数的调和平均数为:" << Calculate(a, b) << endl; + } + + return 0; +} diff --git "a/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_2.cpp" "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_2.cpp" new file mode 100644 index 0000000..bd7b385 --- /dev/null +++ "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_2.cpp" @@ -0,0 +1,44 @@ +#include +using namespace std; + +int Init(int arr[]); +void Show(int arr[], int s); +double Cal(int arr[], int s); + +int main() { + int arr[10]; + int size = Init(arr); + Show(arr, size); + cout << "平均成绩为:" << Cal(arr, size) << endl; + return 0; +} + +int Init(int arr[]) { + int s = 0; + cout << "请输入成绩:(输入-1提前结束输入)" << endl; + for (int i = 0; i < 10; i++) { + int temp; + cin >> temp; + if (temp == -1) + break; + arr[i] = temp; + s++; + } + return s; +} + +void Show(int arr[], int s) { + cout << "以下是高尔夫成绩:" << endl; + for (int i = 0; i < s; i++) { + cout << arr[i] << " "; + } + cout << endl; +} + +double Cal(int arr[], int s) { + double sum = 0; + for (int i = 0; i < s; i++) { + sum += arr[i]; + } + return sum / s; +} diff --git "a/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_3.cpp" "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_3.cpp" new file mode 100644 index 0000000..636a24d --- /dev/null +++ "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_3.cpp" @@ -0,0 +1,40 @@ +#include +using namespace std; + +struct box { + char maker[40]; + float height; + float width; + float length; + float volume; +}; + +void Init(box* b); +void Show(const box* b); +void SetVolume(box* b); + +int main() { + box B; + Init(&B); + SetVolume(&B); + Show(&B); + return 0; +} + +void Init(box* b) { + cout << "请输入这个box的名称:"; + cin >> b->maker; + cout << endl << "请分别输入这个box的高宽长:"; + cin >> b->height >> b->width >> b->length; + cout << endl; +} + +void Show(const box* b) { + cout << "这个box的名称为:" << b->maker << endl; + cout << "这个box的高宽长分别为:" << b->height << " " << b->width << " " << b->length << endl; + cout << "这个box的体积为:" << b->volume << endl; +} + +void SetVolume(box* b) { + b->volume = b->height * b->width * b->length; +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_4.cpp" "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_4.cpp" new file mode 100644 index 0000000..8072e64 --- /dev/null +++ "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_4.cpp" @@ -0,0 +1,30 @@ +#include + +long double probability(unsigned numbers, unsigned picks); + +int main() +{ + using namespace std; + double total1, choices1, total2, choices2; + cout << "请分别输入两组数:"; + while ((cin >> total1 >> choices1) && (cin >> total2 >> choices2) && choices1 <= total1 && choices2 <= total2) + { + cout << "You have one chance in "; + cout << probability(total1, choices1) * probability(total2, choices2); // compute the odds + cout << " of winning.\n"; + cout << "Next two numbers (q to quit): "; + } + cout << "bye\n"; + return 0; +} + +long double probability(unsigned numbers, unsigned picks) +{ + long double result = 1.0; // here come some local variables + long double n; + unsigned p; + + for (n = numbers, p = picks; p > 0; n--, p--) + result = result * n / p; + return result; +} diff --git "a/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_5.cpp" "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_5.cpp" new file mode 100644 index 0000000..cda1e85 --- /dev/null +++ "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_5.cpp" @@ -0,0 +1,22 @@ +#include +using namespace std; + +int Jiecheng(int x); + +int main() { + int n; + while (1) { + cout << "请输入一个数:"; + cin >> n; + cout << n << "的阶乘为:" << Jiecheng(n) << endl; + } + + return 0; +} + +int Jiecheng(int x) { + if (x == 0) + return 1; + return x * Jiecheng(x - 1); + +} \ No newline at end of file diff --git "a/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_6.cpp" "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_6.cpp" new file mode 100644 index 0000000..d18a96b --- /dev/null +++ "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_6.cpp" @@ -0,0 +1,61 @@ +#include +using namespace std; + +int Fill_array(double a[], int s); +void Show_array(double a[], int s); +void Reverse_array(double a[], int s); + +int main() { + int size; + cout << "输入数组的长度:"; + cin >> size; + double* arr = new double[size]; + int cnt = Fill_array(arr, size); + cout << "实际输入了" << cnt << "个数" << endl; + Show_array(arr, cnt); + Reverse_array(arr, cnt); + cout << "反转数组:" << endl; + Show_array(arr, cnt); + return 0; +} + +int Fill_array(double a[], int s) { + int len = 0; + double t; + cout << "请输入数字:"; + while (s--) { + if (cin >> t) { + a[len++] = t; + } + else + break; + } + + return len; +} + +void Show_array(double a[], int s) { + for (int i = 0; i < s; i++) { + cout << a[i] << " "; + } + cout << endl; +} + +void Reverse_array(double a[], int s) { + if (s % 2 == 0) { + int k = (s - 2) / 2; + for (int i = 1; i <= k; i++) { + double temp = a[i]; + a[i] = a[s - 1 - i]; + a[s - 1 - i] = temp; + } + } + else { + int k = s / 2; + for (int i = 1; i < k; i++) { + double temp = a[i]; + a[i] = a[s - 1 - i]; + a[s - 1 - i] = temp; + } + } +} diff --git "a/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_7.cpp" "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_7.cpp" new file mode 100644 index 0000000..9c5096a --- /dev/null +++ "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_7.cpp" @@ -0,0 +1,81 @@ +#include +const int Max = 5; + +// function prototypes +double* fill_array(double ar[]); +void show_array(const double* begin, const double* end); // don't change data +void revalue(double r, double* begin, double* end); + + +int main() +{ + using namespace std; + double properties[Max]; + + double* last = fill_array(properties); //last是指向数组末尾的指针 + + show_array(properties, last); + if (last > properties) + { + cout << "Enter revaluation factor: "; + double factor; + while (!(cin >> factor)) // bad input + { + cin.clear(); + while (cin.get() != '\n') + continue; + cout << "Bad input; Please enter a number: "; + } + revalue(factor, properties, last); + show_array(properties, last); + } + cout << "Done.\n"; + cin.get(); + cin.get(); + return 0; +} + +double* fill_array(double ar[]) +{ + using namespace std; + double temp; + int i; + for (i = 0; i < Max; i++) + { + cout << "Enter value #" << (i + 1) << ": "; + cin >> temp; + if (!cin) // bad input + { + cin.clear(); + while (cin.get() != '\n') + continue; + cout << "Bad input; input process terminated.\n"; + break; + } + else if (temp < 0) // signal to terminate + break; + ar[i] = temp; + } + return ar + i; +} + +// the following function can use, but not alter, +// the array whose address is ar +void show_array(const double* begin, const double* end) +{ + using namespace std; + const double* pt; + int i = 1; + for (pt = begin; pt != end; pt++) { + cout << "Property #" << i++ << ": $"; + cout << *pt << endl; + } +} + +// multiplies each element of ar[] by r +void revalue(double r, double* begin, double* end) +{ + double* pt; + for (pt = begin; pt != end; pt++) + *pt *= r; +} diff --git "a/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_8.cpp" "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_8.cpp" new file mode 100644 index 0000000..21ca882 --- /dev/null +++ "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_8.cpp" @@ -0,0 +1,79 @@ +//版本2 +#include +#include +using namespace std; + +const int Seasons = 4; +const char* Snames[] = { "Spring", "Summer", "Fall", "Winter" }; + +struct S_exp { + double expense[4]; +}; + +void fill(S_exp& s); +void show(S_exp& s); + +int main() { + S_exp s; + fill(s); + show(s); + + + return 0; +} + +void fill(S_exp& s) { + for (int i = 0; i < Seasons; i++) { + cout << "Enter " << Snames[i] << " expenses: "; + cin >> s.expense[i]; + } +} + +void show(S_exp& s) { + double total = 0.0; + cout << "EXPENSES" << endl; + for (int i = 0; i < Seasons; i++) { + cout << Snames[i] << ": $" << s.expense[i] << endl; + total += s.expense[i]; + } + cout << "Total Expenses: $" << total << endl; +} + +//版本1 +/* +#include +#include +using namespace std; + +const int Seasons = 4; +const char* Snames[] = { "Spring", "Summer", "Fall", "Winter" }; + +void fill(double a[]); +void show(double a[]); + +int main() { + double expense[4]; + fill(expense); + show(expense); + + + return 0; +} + +void fill(double a[]) { + for (int i = 0; i < Seasons; i++) { + cout << "Enter " << Snames[i] << " expenses: "; + cin >> a[i]; + } +} + +void show(double a[]) { + double total = 0.0; + cout << "EXPENSES" << endl; + for (int i = 0; i < Seasons; i++) { + cout << Snames[i] << ": $" << a[i] << endl; + total += a[i]; + } + cout << "Total Expenses: $" << total << endl; +} +*/ \ No newline at end of file diff --git "a/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_9.cpp" "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_9.cpp" new file mode 100644 index 0000000..12e1168 --- /dev/null +++ "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/7_9.cpp" @@ -0,0 +1,72 @@ +#include +#include +#include +using namespace std; + +const int SLEN = 30; + +struct student { + char fullname[SLEN]; + char hobby[SLEN]; + int ooplevel; +}; + +int getinfo(student pa[], int n); +void display1(student st); +void display2(const student* ps); +void display3(const student pa[], int n); + +int main() +{ + cout << "Enter class size: "; + int class_size; + cin >> class_size; + + while (cin.get() != '\n') + continue; + + student* ptr_stu = new student[class_size]; + int entered = getinfo(ptr_stu, class_size); + for (int i = 0; i < entered; i++) + { + display1(ptr_stu[i]); + display2(&ptr_stu[i]); + } + display3(ptr_stu, entered); + delete[] ptr_stu; + cout << "Done\n"; + return 0; +} + +int getinfo(student pa[], int n) { + int i; + for (i = 0; i < n; i++) { + cout << "请输入学生的姓名:"; + cin.getline(pa[i].fullname, SLEN); + if (strlen(pa[i].fullname) == 0) //输入空行则暂停 + break; + cout << "请输入学生的兴趣:"; + cin.getline(pa[i].hobby, SLEN); + cout << "请输入学生的ooplevel:"; + cin >> pa[i].ooplevel; + cin.get(); + } + return i; +} + +void display1(student st) { + cout << "display1" << endl; + cout << "姓名:" << st.fullname << " 兴趣:" << st.hobby << " ooplevel:" << st.ooplevel << endl; +} + +void display2(const student* ps) { + cout << "display2" << endl; + cout << "姓名:" << ps->fullname << " 兴趣:" << ps->hobby << " ooplevel:" << ps->ooplevel << endl; + +} +void display3(const student pa[], int n) { + cout << "display3" << endl; + for (int i = 0; i < n; i++) { + cout << "姓名:" << pa[i].fullname << " 兴趣:" << pa[i].hobby << " ooplevel:" << pa[i].ooplevel << endl; + } +} diff --git "a/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/CMakeLists.txt" "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/CMakeLists.txt" new file mode 100644 index 0000000..2a1d14c --- /dev/null +++ "b/C++/codes/2024-07/\350\213\217\345\235\232\350\215\243/C++PrimerPlus/Chapter7/CMakeLists.txt" @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.0) +project(Chapter7) + +# 定义所有的源文件 +set(SOURCES + 7_1.cpp + 7_2.cpp + 7_3.cpp + 7_4.cpp + 7_5.cpp + 7_6.cpp + 7_7.cpp + 7_8.cpp + 7_9.cpp +) + +# 为每个源文件生成一个可执行文件 +foreach(SOURCE ${SOURCES}) + get_filename_component(EXE_NAME ${SOURCE} NAME_WE) + add_executable(${EXE_NAME} ${SOURCE}) +endforeach() -- Gitee