From af5944bc1272c4c2276b3363dee84de636c92baa Mon Sep 17 00:00:00 2001 From: flykhan Date: Fri, 23 Jun 2023 17:31:07 +0800 Subject: [PATCH] day8 coding --- day8/d1.cpp | 13 +++++++++++++ day8/d10.cpp | 41 ++++++++++++++++++++++++++++++++++++++++ day8/d11.cpp | 29 ++++++++++++++++++++++++++++ day8/d12.cpp | 32 +++++++++++++++++++++++++++++++ day8/d12_2.cpp | 33 ++++++++++++++++++++++++++++++++ day8/d13.cpp | 32 +++++++++++++++++++++++++++++++ day8/d14.cpp | 23 +++++++++++++++++++++++ day8/d15.cpp | 28 +++++++++++++++++++++++++++ day8/d16.cpp | 22 ++++++++++++++++++++++ day8/d17.cpp | 22 ++++++++++++++++++++++ day8/d2.cpp | 0 day8/d5.cpp | 21 +++++++++++++++++++++ day8/d6.cpp | 44 +++++++++++++++++++++++++++++++++++++++++++ day8/d7.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ day8/d8.cpp | 15 +++++++++++++++ day8/d9.cpp | 29 ++++++++++++++++++++++++++++ 16 files changed, 435 insertions(+) create mode 100644 day8/d1.cpp create mode 100644 day8/d10.cpp create mode 100644 day8/d11.cpp create mode 100644 day8/d12.cpp create mode 100644 day8/d12_2.cpp create mode 100644 day8/d13.cpp create mode 100644 day8/d14.cpp create mode 100644 day8/d15.cpp create mode 100644 day8/d16.cpp create mode 100644 day8/d17.cpp create mode 100644 day8/d2.cpp create mode 100644 day8/d5.cpp create mode 100644 day8/d6.cpp create mode 100644 day8/d7.cpp create mode 100644 day8/d8.cpp create mode 100644 day8/d9.cpp diff --git a/day8/d1.cpp b/day8/d1.cpp new file mode 100644 index 0000000..43ba3d9 --- /dev/null +++ b/day8/d1.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main() +{ + cout << "running app" << endl; + int i = 0; + for (;;) // 死循环: 原理是 for 语句中的三个表达式都可以省略 (但是分号不能省略), 省略后的 for 语句相当于 while (1) + ; + cout << "app end" << endl; + return 0; +} \ No newline at end of file diff --git a/day8/d10.cpp b/day8/d10.cpp new file mode 100644 index 0000000..c45e22f --- /dev/null +++ b/day8/d10.cpp @@ -0,0 +1,41 @@ +#include +#include + +using namespace std; + +int main() +{ + int rows = 1; + cout << "请输入行数: "; + cin >> rows; + int arr[rows][3] = {}; // ?变量是否可以作为定义数组时的维度 + int num; + int j = rows; + while (j) + { + for (int i = 0; i < 3; i++) + { + cout << "请输入值: "; + cin >> arr[rows - j][i]; + + // break 结束 for 循环 + // 如何结束外部的 while 循环 + if (arr[rows - j][i] == -1) + // break; + goto show; + } + j--; + } + +show: + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < 3; j++) + { + cout << arr[i][j] << "\t"; + } + cout << endl; + } + + return 0; +} \ No newline at end of file diff --git a/day8/d11.cpp b/day8/d11.cpp new file mode 100644 index 0000000..8ad5a1b --- /dev/null +++ b/day8/d11.cpp @@ -0,0 +1,29 @@ +// 求出每个人的平均成绩 +#include +#include + +using namespace std; + +int main() +{ + int score[5][4] = { + {56, 75, 78, 89}, + {89, 98, 76, 67}, + {88, 88, 77, 66}, + {67, 78, 89, 90}, + {98, 97, 96, 95}}; + + float avg = 0.0f; + // 每个人的平均成绩 + for (int i = 0; i < 5; i++) + { + avg = 0.0f; // 重置平均成绩, 目的: 避免上一次的平均成绩影响到这一次的平均成绩 + for (int j = 0; j < 4; j++) + { + avg += score[i][j] / 4.0f; // 求出每个人的平均成绩 + } + cout << "第" << i + 1 << "个人的平均成绩: " << avg << endl; + } + + return 0; +} \ No newline at end of file diff --git a/day8/d12.cpp b/day8/d12.cpp new file mode 100644 index 0000000..9690cb5 --- /dev/null +++ b/day8/d12.cpp @@ -0,0 +1,32 @@ +// 计算某一个学科的平均成绩 +#include +#include + +using namespace std; + +int main() +{ + int score[5][4] = { + {56, 75, 78, 89}, + {89, 98, 76, 67}, + {88, 88, 77, 66}, + {67, 78, 89, 90}, + {98, 97, 96, 95}}; + + string subject[] = {"语文", "数学", "化学", "物理"}; + + float avg = 0.0f; + // 每个人的平均成绩 + for (int j = 0; j < 4; j++) + { + avg = 0.0f; // 重置平均成绩, 目的: 避免上一次的平均成绩影响到这一次的平均成绩 + for (int i = 0; i < 5; i++) + { + + avg += score[i][j] / 5.0f; // 求出某一个学科的平均成绩 + } + cout << subject[j] << "的平均成绩: " << avg << endl; + } + + return 0; +} \ No newline at end of file diff --git a/day8/d12_2.cpp b/day8/d12_2.cpp new file mode 100644 index 0000000..3faa2ce --- /dev/null +++ b/day8/d12_2.cpp @@ -0,0 +1,33 @@ +#include +#include + +using namespace std; + +int main(int argc, char const *argv[]) +{ + + int score[5][4] = { + {56, 75, 78, 89}, + {89, 98, 76, 67}, + {88, 88, 77, 66}, + {67, 78, 89, 90}, + {98, 97, 96, 95}}; + + float avg1 = 0.0f; + float avg2 = 0.0f; + float avg3 = 0.0f; + float avg4 = 0.0f; + // 计算物理学科平均 + for (int i = 0; i < 5; i++) + { + avg1 += score[i][0] / 5.0; + avg2 += score[i][1] / 5.0; + avg3 += score[i][2] / 5.0; + avg4 += score[i][3] / 5.0; + } + cout << "语文的平均成绩: " << avg1 << endl; + cout << "数学的平均成绩: " << avg2 << endl; + cout << "化学的平均成绩: " << avg3 << endl; + cout << "物理的平均成绩: " << avg4 << endl; + return 0; +} diff --git a/day8/d13.cpp b/day8/d13.cpp new file mode 100644 index 0000000..f3948bd --- /dev/null +++ b/day8/d13.cpp @@ -0,0 +1,32 @@ +// 数组排序 +#include +#include + +using namespace std; + +int main() +{ + int arr[] = {5, 2, 3, 1, 7, 6}; + int len = sizeof(arr) / sizeof(int); // 这里的 sizeof(int) 相当于 sizeof(arr[0]) + // 冒泡排序 + for (int i = 0; i < len - 1; i++) + { + // i 代表轮数 + for (int j = 0; j < len - i - 1; j++) + { + if (arr[j] > arr[j + 1]) + { + // 使用异或运算交换两个数的值 + arr[j] = arr[j] ^ arr[j + 1]; + arr[j + 1] = arr[j] ^ arr[j + 1]; + arr[j] = arr[j] ^ arr[j + 1]; + } + } + } + + for (int i = 0; i < len; i++) + cout << arr[i] << "\t"; + + cout << endl; + return 0; +} \ No newline at end of file diff --git a/day8/d14.cpp b/day8/d14.cpp new file mode 100644 index 0000000..06acc14 --- /dev/null +++ b/day8/d14.cpp @@ -0,0 +1,23 @@ +// 按字符串读取,读取到第一`\0`结束。 +#include +#include + +using namespace std; + +int main() +{ + char arr[] = "hello"; + cout << sizeof(arr) << endl; // 6B + + // 逐个读取 + int i = 0; + while (arr[i]) // arr[i] != '\0', 因为 '\0' 的 ASCII 码为 0 + { + cout << arr[i++] << endl; + } + cout << endl; + + // 一次读取 + cout << arr << endl; + return 0; +} \ No newline at end of file diff --git a/day8/d15.cpp b/day8/d15.cpp new file mode 100644 index 0000000..cadd993 --- /dev/null +++ b/day8/d15.cpp @@ -0,0 +1,28 @@ +// 从键盘输入字符 +#include +#include +#include + +using namespace std; + +int main() +{ + char name[50] = ""; // 字符数组的初始值建议给一个空字符串, 即 "" + cout + << "请输入姓名: "; + cin >> name; + + // sizeof(字符数组)计算数组分配内存空间的大小 + cout << "你输入的内容为" << name << ", 它的字节长度为" << sizeof(name) << endl; + // strlen(字符数组)输出第一个`\0`之前的字符个数 + cout << "字符个数为" << strlen(name) << endl; + + int i = 0; + while (name[i]) + { + cout << name[i++] << "\t"; + } + cout << (int)name[i] << endl; // 0 ,用于验证`\0`的 ASCII 码为 0 + + return 0; +} \ No newline at end of file diff --git a/day8/d16.cpp b/day8/d16.cpp new file mode 100644 index 0000000..f271c84 --- /dev/null +++ b/day8/d16.cpp @@ -0,0 +1,22 @@ +// 获取一个字符串 求该字符串的长度(不使用strlen) +#include + +using namespace std; + +int main() +{ + // 从键盘接收字符 + char name[50] = ""; + + cout << "请输入姓名: "; + // 获取整行的内容,包含空格 + cin.getline(name, sizeof(name)); //用法: cin.getline(字符数组, 字符数组的长度), 用于获取整行的内容,包含空格 + + // 获取输入字符的个数 + int len = 0; + while (name[len++]) + ; + cout << "输入的字符个数为: " << len - 1 << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/d17.cpp b/day8/d17.cpp new file mode 100644 index 0000000..2eeeea2 --- /dev/null +++ b/day8/d17.cpp @@ -0,0 +1,22 @@ +// 从键盘输入5位学生的姓名 +#include + +using namespace std; + +int main() +{ + // 键盘输入 5 位学生的姓名 + char names[5][50] = {""}; // "": 用于初始化字符数组, 也可以写成 "{}" + for (int i = 0; i < 5; i++) + { + cout << "姓名: "; + cin.getline(names[i], sizeof(names[i])); + } + + for (int i = 0; i < 5; i++) + { + cout << names[i] << endl; + } + + return 0; +} \ No newline at end of file diff --git a/day8/d2.cpp b/day8/d2.cpp new file mode 100644 index 0000000..e69de29 diff --git a/day8/d5.cpp b/day8/d5.cpp new file mode 100644 index 0000000..c2eed5c --- /dev/null +++ b/day8/d5.cpp @@ -0,0 +1,21 @@ +#include +#include + +using namespace std; + +int main() +{ + srand(time(NULL)); + + int arr[5]; + int i = 0; + // 生成 5 个随机数并输出 + while (i < 5) + { + arr[i] = rand() % 100; // 生成 0~99 之间的随机数: max = 99, min = 0 -> rand() % (max - min + 1) + min + cout << "arr[" << i << "] = " << arr[i] << endl; + i++; + } + + return 0; +} \ No newline at end of file diff --git a/day8/d6.cpp b/day8/d6.cpp new file mode 100644 index 0000000..ccd2700 --- /dev/null +++ b/day8/d6.cpp @@ -0,0 +1,44 @@ +#include +#include // 作用: 用于获取整型的最大值 INT_MAX + +using namespace std; + +int main() +{ + int arr[10] = {}; + int sum = 0; // 总和 + int max = INT_MIN; // 最大值: 初始化为整型的最小值 + int min = INT_MAX; // 最小值: 初始化为整型的最大值 + + // 输入 10 个数 + int n = 0; + while (n < 10) + { + cout << "请输入第" << n + 1 << "个数:"; + cin >> arr[n]; + n++; + } + + // 输出 10 个数 + int i = 0; + while (i < 10) + { + sum += arr[i]; // 累加求和 + + if (arr[i] > max) + max = arr[i]; // 更新最大值 + + if (arr[i] < min) + min = arr[i]; // 更新最小值 + + cout << arr[i] << " "; // 输出 + i++; + } + cout << endl; + + cout << "平均值为: " << sum / 10 << endl; + cout << "最大值为: " << max << endl; + cout << "最小值为: " << min << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/d7.cpp b/day8/d7.cpp new file mode 100644 index 0000000..bb53c9d --- /dev/null +++ b/day8/d7.cpp @@ -0,0 +1,51 @@ +#include +#include // 作用: 用于获取整型的最大值 INT_MAX +#include + +using namespace std; + +int main() +{ + srand(time(NULL)); + const int N = 5; // 常量: 用于表示数组的长度 + + double arr[N] = {}; + double sum = 0; // 总和 + double avg = 0; // 平均值 + double max = INT_MIN; // 最大值: 初始化为整型的最小值 + double min = INT_MAX; // 最小值: 初始化为整型的最大值 + + // 输入 N 个数 + int n = 0; + while (n < N) + { + arr[n] = rand() % 100; // 生成 0~99 之间的随机数: max = 99, min = 0 -> rand() % (max - min + 1) + min + n++; + } + + // 输出 N 个数 + int i = 0; + while (i < N) + { + sum += arr[i]; // 累加求和 + + if (arr[i] > max) + max = arr[i]; // 更新最大值 + + if (arr[i] < min) + min = arr[i]; // 更新最小值 + + cout << arr[i] << " "; // 输出 + + avg += arr[i] / 5.0; // 求平均值 + i++; + } + cout << endl; + + cout << "N 位同学的C++课程平均值为: " << sum / N << endl; + cout << "N 位同学的C++课程最大值为: " << max << endl; + cout << "N 位同学的C++课程最小值为: " << min << endl; + cout << avg << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/d8.cpp b/day8/d8.cpp new file mode 100644 index 0000000..6444121 --- /dev/null +++ b/day8/d8.cpp @@ -0,0 +1,15 @@ +#include +#include + +using namespace std; + +int main() +{ + int arr[][3] = {1, 2, 3, 4, 5}; + // 计算行数 + int rows = sizeof(arr) / sizeof(arr[0]); + cout << "行数: " << rows << endl; + cout << "第一行最后一列的值: " << arr[0][2] << endl; + + return 0; +} \ No newline at end of file diff --git a/day8/d9.cpp b/day8/d9.cpp new file mode 100644 index 0000000..049eaf8 --- /dev/null +++ b/day8/d9.cpp @@ -0,0 +1,29 @@ +#include +#include + +using namespace std; + +int main() +{ + int arr[3][5] = { + {1, 2, 3, 4, 5}, + {10, 20, 30, 40, 50}, + {100, 200, 300, 400, 500}}; + + cout << "第一行数据: "; + for (int i = 0; i < 5; i++) + { + cout << arr[0][i] << "\t"; + } + cout << endl; + + // 读取所有行的数据 + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 5; j++) + cout << arr[i][j] << "\t"; + cout << endl; + } + + return 0; +} \ No newline at end of file