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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 0000000000000000000000000000000000000000..3fc984ca81baf03054f1c8888b9d7a5962800cf8 --- /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 0000000000000000000000000000000000000000..bd7b385d4b04328005732f928dd33deed9813371 --- /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 0000000000000000000000000000000000000000..636a24dc0e785991ae9594b7f4077df6735f6e79 --- /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 0000000000000000000000000000000000000000..8072e6464fde787541b7d53c0ab5bf854fe024fb --- /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 0000000000000000000000000000000000000000..cda1e85bbd243bdb00701839a9471241b0a5e834 --- /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 0000000000000000000000000000000000000000..d18a96b58f0fdc5c188f1717580d185ee9b265cf --- /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 0000000000000000000000000000000000000000..9c5096a73a6d27d36ad5ea13473b0f5a5f404e8b --- /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 0000000000000000000000000000000000000000..21ca8823aef68bdca461792a2b1da5272fe78e3c --- /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 0000000000000000000000000000000000000000..12e11688f096a7ddc419ab7981120c0cbd70dae0 --- /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 0000000000000000000000000000000000000000..2a1d14c9e217b39327f1922d3b9b49da52d1f17f --- /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() 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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391