From 1424dc887418b0aa662817d3b83fa311b840ac5d Mon Sep 17 00:00:00 2001 From: flykhan Date: Mon, 26 Jun 2023 08:34:05 +0800 Subject: [PATCH] day9 coding --- day9/case1.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ day9/case2.cpp | 28 ++++++++++++++++++++++++++ day9/case4.cpp | 32 ++++++++++++++++++++++++++++++ day9/case5.cpp | 5 ++++- day9/d1.cpp | 25 +++++++++++++++++++++++ day9/d2.cpp | 25 +++++++++++++++++++++++ day9/d3.cpp | 30 ++++++++++++++++++++++++++++ day9/d4.cpp | 24 ++++++++++++++++++++++ day9/d4_2.cpp | 24 ++++++++++++++++++++++ day9/d5.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 297 insertions(+), 1 deletion(-) create mode 100644 day9/case1.cpp create mode 100644 day9/case2.cpp create mode 100644 day9/case4.cpp create mode 100644 day9/d1.cpp create mode 100644 day9/d2.cpp create mode 100644 day9/d3.cpp create mode 100644 day9/d4.cpp create mode 100644 day9/d4_2.cpp create mode 100644 day9/d5.cpp diff --git a/day9/case1.cpp b/day9/case1.cpp new file mode 100644 index 0000000..e2906fd --- /dev/null +++ b/day9/case1.cpp @@ -0,0 +1,54 @@ +// 接收一个数值数组,实现数组中元素值从小到大排序 +// 例如:输入 3 2 1 5 4 +// 输出 1 2 3 4 5 +#include + +using namespace std; + +void show(int arr[], int size) +{ + int i = 0; + while (i < size) + { + cout << arr[i] << "\t"; + i++; + } + cout << endl; +} + +void sort(int arr[], int size) +{ + for (int i = 0; i < size - 1; i++) + { + for (int j = 0; j < size - 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]; + } + } + } +} + +int main() +{ + // 从键盘输入 5 个数值 + int arr[5]; + for (int i = 0; i < 5; i++) + { + cout << "请输入第" << i + 1 << "个数:"; + cin >> arr[i]; + } + cout << endl; + + cout << "排序前:" << endl; + show(arr, 5); + + sort(arr, 5); + cout << "排序后:" << endl; + show(arr, 5); + + return 0; +} \ No newline at end of file diff --git a/day9/case2.cpp b/day9/case2.cpp new file mode 100644 index 0000000..ec2931f --- /dev/null +++ b/day9/case2.cpp @@ -0,0 +1,28 @@ +// 接收一个数值数组,对数组中的数值求最大值并输出。 +#include + +using namespace std; + +int maxNum(int arr[], int size) +{ + int max = arr[0]; + + while (--size) + { + if (arr[size] > max) + { + max = arr[size]; + } + } + + return max; +} + +int main() +{ + int arr[] = {18, 2, 3, 9, 5, 7}; // 完全初始化 + int max = maxNum(arr, sizeof(arr) / sizeof(arr[0])); + cout << "最大值是: " << max << endl; + + return 0; +} \ No newline at end of file diff --git a/day9/case4.cpp b/day9/case4.cpp new file mode 100644 index 0000000..04565a2 --- /dev/null +++ b/day9/case4.cpp @@ -0,0 +1,32 @@ +// 定义函数, 实现字符串中指定位置字符删除 +#include + +using namespace std; + +void delChar(char str[], int index) +{ + int i = index; + while (str[i] != '\0') + { + str[i] = str[i + 1]; + i++; + } +} + +int main() +{ + char str[] = "abcdefg"; + int index; + cout << "原字符串: " << str << endl; + cout << "请输入要删除的位置: "; + cin >> index; + if (index < 0 || index > sizeof(str) / sizeof(str[0])) + { + cout << "位置不合法" << endl; + return 0; + } + delChar(str, index); + cout << "删除后: " << str << endl; + + return 0; +} \ No newline at end of file diff --git a/day9/case5.cpp b/day9/case5.cpp index 41a297e..16bc940 100644 --- a/day9/case5.cpp +++ b/day9/case5.cpp @@ -15,7 +15,10 @@ int findCharAt(char str[], int size, char ch) int main() { - char str[] = "abcdefg"; + // char str[] = "abcdefg"; + char str[128]; + cout << "请输入字符串: "; + cin >> str; char ch; cout << "请输入要查找的字符: "; cin >> ch; diff --git a/day9/d1.cpp b/day9/d1.cpp new file mode 100644 index 0000000..2012d25 --- /dev/null +++ b/day9/d1.cpp @@ -0,0 +1,25 @@ +// 实现2个整数之和并返回的功能 +#include + +using namespace std; + +// 1. 定义函数 +// a, b是形参 +// addNums是函数名 +int addNums(int a, int b) +{ + return a + b; +} + +int main() +{ + int x, y; + cout << "请输入两个整数:"; + cin >> x >> y; + + // 2. 调用函数 + // [变量 = ] 函数名(实参列表); + int ret = addNums(x, y); // x, y 是实际变量的数据, 称之为实参 + cout << "两个整数之和为:" << ret << endl; + return 0; +} \ No newline at end of file diff --git a/day9/d2.cpp b/day9/d2.cpp new file mode 100644 index 0000000..2b70de4 --- /dev/null +++ b/day9/d2.cpp @@ -0,0 +1,25 @@ +// 函数的声明 +#include + +using namespace std; + +// 函数的声明 +int addNums(int a, int b); + +int main() +{ + int x, y; + cout << "请输入两个整数:"; + cin >> x >> y; + + // 2. 调用函数 + int ret = addNums(x, y); // x, y 是实际变量的数据, 称之为实参 + cout << "两个整数之和为:" << ret << endl; + return 0; +} + +// 函数的定义 +int addNums(int a, int b) +{ + return a + b; +} \ No newline at end of file diff --git a/day9/d3.cpp b/day9/d3.cpp new file mode 100644 index 0000000..dce0dee --- /dev/null +++ b/day9/d3.cpp @@ -0,0 +1,30 @@ +// 有效循环n次,则addNums调用n次,每一次调用都会入栈(创建栈帧)。 +#include + +using namespace std; + +int addNums(int a, int b); // 函数的声明 + +int main() +{ + int x, y; + while (1) + { + cout << "请输入两个整数:"; + cin >> x; + if (x == 0) + break; + cin >> y; + // 调用函数 + int ret = addNums(x, y); + cout << "计算结果为:" << ret << endl; + } + + return 0; +} + +// 函数的定义 +int addNums(int a, int b) +{ + return a + b; +} \ No newline at end of file diff --git a/day9/d4.cpp b/day9/d4.cpp new file mode 100644 index 0000000..92d8b2f --- /dev/null +++ b/day9/d4.cpp @@ -0,0 +1,24 @@ +// 判断给定的整数是否为偶数 +#include + +using namespace std; + +int isEven(int n) +{ + if (n % 2 == 0) + return 1; + return 0; +} + +int main() +{ + int n; + cout << "请输入一个整数:"; + cin >> n; + if (isEven(n)) + cout << "是偶数" << endl; + else + cout << "不是偶数" << endl; + + return 0; +} \ No newline at end of file diff --git a/day9/d4_2.cpp b/day9/d4_2.cpp new file mode 100644 index 0000000..970fe71 --- /dev/null +++ b/day9/d4_2.cpp @@ -0,0 +1,24 @@ +// 判断给定的整数是否为偶数 +#include + +using namespace std; + +int isEven(int *n) +{ + if (*n % 2 == 0) + return 1; + return 0; +} + +int main() +{ + int n; + cout << "请输入一个整数:"; + cin >> n; + if (isEven(&n)) + cout << "是偶数" << endl; + else + cout << "不是偶数" << endl; + + return 0; +} \ No newline at end of file diff --git a/day9/d5.cpp b/day9/d5.cpp new file mode 100644 index 0000000..36c17c7 --- /dev/null +++ b/day9/d5.cpp @@ -0,0 +1,51 @@ + +// 定义函数,接收一个数组和一个整数,从数组中查找到第一个比给定的整数大的数的位置且返回。 +#include + +using namespace std; + +void show(int arr[], int size) +{ + int i = 0; + while (i < size) + { + cout << arr[i] << " "; + i++ + } + cout << endl; +} + +// arr[] 是 int 类型的一维数组, 此形参准备从外部接收一个 int[] 类型的数组的首地址 +int find(int arr[], int n, int size) +{ + for (int i = 0; i < size; i++) + { + if (arr[i] > n) + return i; // 结束循环、结束函数并返回 i + } + + return -1; +} + +int main() +{ + int arr[] = {1, 2, 3, 0, 5, 7}; // 完全初始化 + // [注意] 计算数组的长度, 在定义数组的区域可以计算, 如果作为参数传递, 就无法计算了 + // 这是因为数组作为参数传递时, 会退化为指针, 无法计算长度; + // 如果改为指针, 就可以计算长度了, 但是指针无法初始化, 所以必须在定义时初始化 + int len = sizeof(arr) / sizeof(arr[0]); + + cout << "--- main() ---" << endl; + show(arr, len); + int n = 0; + cout << "查找的数:"; + cin >> n; + + int index = find(arr, n, len); + if (index == -1) + cout << "没有找到" << endl; + else + cout << "找到了,位置是:" << index << ", 第一个比" << n << "大的数是:" << arr[index] << endl; + + return 0; +} \ No newline at end of file