高级语言程序设计作业 11/04
- 2024高级语言程序设计:https://edu.cnblogs.com/campus/fzu/2024C
- 高级语言程序设计课程第五次作业:https://edu.cnblogs.com/campus/fzu/2024C/homework/13303
- 学号:102400215
- 姓名:胡加乘
1
#include <iostream>
#include <cmath>
using namespace std;
void fun(int m, int* k, int xx[]);
int main()
{
int m;
int count = 0;
int arr[100] = { 0 };
cout << "Please enter an integer m (m >= 1): " << endl;
cin >> m;
cout << "Executing fun()..." << endl;
fun(m, &count, arr);
cout << "So after execution, *k = " << count << "," << endl;
cout << "and xx[] = ";
for (int i = 0; i < count; i++)
{
cout << arr[i] << " ";
}
return 0;
}
bool isPrime(int num)
{
if (num <= 1)
return false;
for (int current = 2; current <= sqrt(num); current++)
{
if (num % current == 0)
return false;
}
return true;
}
void fun(int m, int* k, int xx[])
{
*k = 0;
for (int i = 0; i < m; i++)
{
if (!isPrime(i))
{
xx[*k] = i;
(*k)++;
}
}
}
2
#include <iostream>
#include <cmath>
using namespace std;
/*
baseStr: 原来的字符串
n: 原来字符串的长度
m: 从第m个字符开始复制
replacement: 复制的字符串
reLen: 复制的字符串的总长度
*/
void replaceStr(char* baseStr, int n, int m, char* replacement, int reLen);
int main()
{
char baseStr[] = "This is a cpp program.";
char replacement[] = "margorp=====";
replaceStr(baseStr, strlen(baseStr), 14,
replacement, strlen(replacement));
cout << baseStr << endl;
return 0;
}
void replaceStr(char* baseStr, int n, int m, char* replacement, int reLen)
{
for (int i = m; i < n; i++)
{
if (i - m < reLen)
baseStr[i] = replacement[i - m];
}
}
3
#include <iostream>
#include <cmath>
using namespace std;
void bubbleSort(int* nums, int len);
int main()
{
int nums[] = { 3, 1, 7, 34, 23, 9, 6, -10, 2, 6, -666 };
int len = sizeof(nums) / sizeof(nums[0]);
for (int i = 0; i < len; i++)
cout << nums[i] << " ";
cout << endl;
cout << "Here goes the bubble sort!" << endl;
bubbleSort(nums, len);
for (int i = 0; i < len; i++)
cout << nums[i] << " ";
cout << endl;
return 0;
}
void bubbleSort(int* nums, int len)
{
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (nums[j] > nums[j + 1])
{
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
}
}
}
}
4
#include <iostream>
#include <cmath>
using namespace std;
// 3 4 2 7 67 -102 45
// 45 -102 67 7 2 4 3
void reverse(int* nums, int len);
int main()
{
int nums[100];
int length = 0;
while (scanf_s("%d", &nums[length++])); // 读到字母结束,回车不算!
length--; // 多加一个,减回来
reverse(nums, length);
for (int i = 0; i < length; i++)
{
cout << nums[i] << " ";
}
return 0;
}
void reverse(int* nums, int len)
{
for (int i = 0; i < len / 2; i++)
{
int tmp = nums[i];
nums[i] = nums[len - 1 - i];
nums[len - 1 - i] = tmp;
}
}
5
#include <iostream>
#include <cmath>
using namespace std;
void printArray(int* nums, int len);
int main()
{
int array[] = { 1, 2, 3, 4, 5, 6 };
int len = sizeof(array) / sizeof(array[0]);
printArray(array, len);
return 0;
}
void printArray(int* nums, int len)
{
for (int i = 0; i < len; i++)
{
cout << *(nums + i) << " ";
}
}
6
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
typedef struct
{
int value;
int r, c;
} memberInfo;
void iterateArray(int** arr, int rows, int cols, memberInfo* min, memberInfo* max);
int** generate2dArray(int* rows, int* cols, int maxRows = 10, int maxCols = 10);
void print2dArray(int** array, int rows, int cols);
void clean2dArray(int** array, int rows);
int main()
{
int rows,
cols;
int** array = generate2dArray(&rows, &cols);
print2dArray(array, rows, cols);
cout << endl;
memberInfo min = { INT_MAX, -1, -1 };
memberInfo max = { INT_MIN, -1, -1 };
iterateArray(array, rows, cols, &min, &max);
printf("min value: %d, row=%d, col=%d\n", min.value, min.r, min.c);
printf("max value: %d, row=%d, col=%d\n", max.value, max.r, max.c);
clean2dArray(array, rows);
return 0;
}
int** generate2dArray(int* rows, int* cols, int maxRows, int maxCols)
{
*rows = rand() % maxRows + 1;
*cols = rand() % maxCols + 1;
int** arr = (int**)malloc(sizeof(int*) * *rows);
for (int i = 0; i < *rows; i++)
{
arr[i] = (int*)malloc(sizeof(int) * *cols);
for (int j = 0; j < *cols; j++)
{
arr[i][j] = rand();
}
}
return arr;
}
void print2dArray(int** array, int rows, int cols)
{
cout << "(" << rows << "x" << cols << ")" << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
}
void clean2dArray(int** array, int rows)
{
for (int i = 0; i < rows; i++)
free(array[i]);
free(array);
}
void iterateArray(int** arr, int rows, int cols, memberInfo* min, memberInfo* max)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (arr[i][j] < min->value)
{
min->value = arr[i][j];
min->r = i;
min->c = j;
}
else if (arr[i][j] > max->value)
{
max->value = arr[i][j];
max->r = i;
max->c = j;
}
}
}
}
7
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
bool isUpperMatrix(int** matrix, int n);
int** generate2dArray(int rows, int cols, bool ensureUpperMatrix = true, int maxRows = 10, int maxCols = 10);
void print2dArray(int** array, int rows, int cols);
void clean2dArray(int** array, int rows);
void testUpperMatrix(int n, bool genUpperMatrix);
int main()
{
testUpperMatrix(3, true);
cout << endl;
cout << endl;
testUpperMatrix(4, false);
return 0;
}
bool isUpperMatrix(int** matrix, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (matrix[i][j] != 0)
return false;
}
}
return true;
}
int** generate2dArray(int rows, int cols, bool ensureUpperMatrix, int maxRows, int maxCols)
{
int** arr = (int**)malloc(sizeof(int*) * rows);
for (int i = 0; i < rows; i++)
{
arr[i] = (int*)malloc(sizeof(int) * cols);
for (int j = 0; j < cols; j++)
{
if (j < i && ensureUpperMatrix)
arr[i][j] = 0;
else
arr[i][j] = rand();
}
}
return arr;
}
void print2dArray(int** array, int rows, int cols)
{
cout << "(" << rows << "x" << cols << ")" << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
}
void clean2dArray(int** array, int rows)
{
for (int i = 0; i < rows; i++)
free(array[i]);
free(array);
}
void testUpperMatrix(int n, bool genUpperMatrix)
{
int** matrix = generate2dArray(n, n, genUpperMatrix);
print2dArray(matrix, n, n);
if (isUpperMatrix(matrix, n))
{
cout << "Is upper triangle matrix.";
}
else
{
cout << "Is not upper triangle matrix.";
}
clean2dArray(matrix, n);
}
8
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
bool FindValue(int** matrix, int rows, int cols, int value, int& r, int& c);
int** generate2dArray(int* rows, int* cols, int maxRows = 10, int maxCols = 10);
void print2dArray(int** array, int rows, int cols);
void clean2dArray(int** array, int rows);
int main()
{
int rows,
cols;
int** array = generate2dArray(&rows, &cols);
print2dArray(array, rows, cols);
cout << "Looking for value 9..." << endl;
int r, c;
if (FindValue(array, rows, cols, 9, r, c))
{
cout << "Found! at row=" << r << ", col=" << c << endl;
}
else
{
cout << "Not found!" << endl;
}
return 0;
}
bool FindValue(int** matrix, int rows, int cols, int value, int& r, int& c)
{
// 确定目标值所在的行索引。
int row = -1;
{
int start = 0;
int end = rows - 1;
while (start <= end)
{
int mid = (start + end) / 2;
int min = matrix[mid][0];
int max = matrix[mid][cols - 1];
if (min <= value && value <= max)
{
row = mid;
break;
}
else if (value < min)
end = mid - 1;
else if (value > max)
start = mid + 1;
}
}
if (row == -1)
return false;
int* column = matrix[row];
// 确定目标值所在的列索引。
int col = -1;
{
int start = 0;
int end = cols - 1;
while (start <= end)
{
int mid = (start + end) / 2;
if (column[mid] == value)
{
col = mid;
break;
}
else if (column[mid] < value)
start = mid + 1;
else
end = mid - 1;
}
}
if (col == -1)
return false;
r = row;
c = col;
return true;
}
int** generate2dArray(int* rows, int* cols, int maxRows, int maxCols)
{
*rows = rand() % maxRows + 1;
*cols = rand() % maxCols + 1;
int** arr = (int**)malloc(sizeof(int*) * *rows);
int current = 0;
for (int i = 0; i < *rows; i++)
{
arr[i] = (int*)malloc(sizeof(int) * *cols);
for (int j = 0; j < *cols; j++)
{
arr[i][j] = ++current;
}
}
return arr;
}
void print2dArray(int** array, int rows, int cols)
{
cout << "(" << rows << "x" << cols << ")" << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
}
void clean2dArray(int** array, int rows)
{
for (int i = 0; i < rows; i++)
free(array[i]);
free(array);
}
9
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
/*
n: an integer.
a, b: reference to two numbers whose sum equals to n, both prime number.
return value: 1 if a, b exist; 0 if fail.
*/
int split(int n, int& a, int& b);
int main()
{
int p, q;
for (int n = 1; n <= 50; n++)
{
if (split(n, p, q))
printf("%d = %d + %d\n", n, p, q);
else
printf("%d\n", n);
}
return 0;
}
bool isPrime(int num)
{
if (num <= 1)
return false;
for (int i = 2; i <= sqrt(num); i++)
{
if (num % i == 0)
return false;
}
return true;
}
int split(int n, int& a, int& b)
{
a = 2;
b = n - 2;
do
{
if (isPrime(a) && isPrime(b))
return 1;
a++;
b = n - a;
} while (a <= n);
return 0;
}
10
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
/*
s: a string to shift.
n: circular left shift n times.
*/
void shift(char* s, int n);
int main()
{
char str[] = "Hello";
int n = 3;
shift(str, n);
printf("%s", str);
return 0;
}
void shift(char* s, int n)
{
if (n <= 0)
return;
int len = strlen(s);
if (len == 0)
return;
for (int r = 0; r < n; r++)
{
int head = s[0];
for (int i = 1; i < len; i++)
{
s[i - 1] = s[i];
}
s[len - 1] = head;
}
}
11
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
int main()
{
int cube[3][3] =
{
{ 4, 9, 2 },
{ 3, 5, 7 },
{ 8, 1, 6 },
};
int n = 3; // n阶矩阵
// m为要比较的和。
int m = 0;
for (int j = 0; j < n; j++)
m += cube[0][j];
// 比较行
for (int i = 0; i < n; i++)
{
int sum = 0;
for (int j = 0; j < n; j++)
sum += cube[i][j];
if (sum != m)
goto not_cube;
}
// 比较列
for (int j = 0; j < n; j++)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += cube[i][j];
if (sum != m)
goto not_cube;
}
// 比较对角线1
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += cube[i][i];
if (sum != m)
goto not_cube;
}
// 比较对角线2
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += cube[n - 1 - i][i];
if (sum != m)
goto not_cube;
}
is_cube:
printf("is cube!!!");
return 0;
not_cube:
printf("not cube");
return 0;
}
12
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
/*判断两个字符串是否相等。忽略大小写。*/
int mystrcmp(const char* s1, const char* s2);
int main()
{
const char* s1 = "hello";
const char* s2 = "hELlO";
if (mystrcmp(s1, s2))
cout << "ok" << endl;
else
cout << "not equal" << endl;
return 0;
}
int mystrcmp(const char* s1, const char* s2)
{
int len;
if ((len = strlen(s1)) != strlen(s2))
return 0;
for (int i = 0; i < len; i++)
{
if (s1[i] == s2[i]
|| (isupper(s1[i]) && (s1[i] | 0B00100000) == s2[i]) // upper -> lower
|| (islower(s1[i]) && (s1[i] & 0B11011111) == s2[i])) // lower -> upper
continue;
return false;
}
return true;
}
总结与收获
二维数组传参
形参为... int** arr, ...
双重指针时,不可传入静态二维数组,如int array[3][5];
关于*p++
相当于两个步骤
- 解引用
p
,得到*p
的值。 - 指针
p
自增一。如果是char
指针,往后挪一个字节;int
指针,则往后挪4个字节。