本帖最后由 bin554385863 于 2019-5-30 12:36 编辑
2019年5月29日17:58:03
自定义数字数组函数,arrlen(size_t arr)/*数组长度函数*/
#define el "'e'"
/*用字符e表示一个数字数组的结束,el = endline*/
size_t arrlen(size_t arr[])
{
size_t i = 1;
while (*(arr + i) != el)
{
i++;
}
return i;
}
数字数组拼接函数arrcat()/*数字数组拼接函数arrcat(arr1, arr2)*/
void arrcat(size_t arr1[], size_t arr2[])
{
size_t temp[arrlen(arr1) + arrlen(arr2)];
size_t i, j, k;
for (i = 0; i < arrlen(arr1); i++)
{
temp[i] = arr1[i];
}
for (j = 0; j < arrlen(arr2); j++)
{
temp[i + j] = arr2[j];
}
for (k = 0; k < i + j; k++)
{
arr1[k] = temp[k];
arr1[k + 1] = el;
printf("%d ", arr1[k]);
}
}
请确保arr1有足够的空间接受arr2的数据!!!!!
数组冒泡排序函数arrsrt(arr)/*冒泡排序函数*/
size_t arrsrt(size_t arr[])
{
int t;
for (size_t i = 0; i < arrlen(arr); i++)
{
for (size_t j = 0; j <arrlen(arr) - 1; j++)
{
if (arr[j] >= arr[j + 1])
{
t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
-------------------------------------------------------------------------------------------------------------------------------#include <stdio.h>
#include "E:\Administrator\Documents\My C\My C Function\Function.c"
void main()
{
int a[14] = {0, 1, 2, 3, el};
int b[] = {7, 5, 8, 4, el};
int c[] = {6, 9, 10, 11, el};
printf("拼接a[]和b[]\n");
arrcat(a, b);
printf("\n\n");
printf("然后拼接c[]\n");
arrcat(a, c);
printf("\n\n");
printf("对数组排序\n");
arrsrt(a);
printf("\n\n");
printf("打印数组长度\n");
printf("%d", arrlen(a));
}
---------------------------------------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-rbvr20ft.zwy --stdout=Microsoft-MIEngine-Out-bh5shobw.1iv --stderr=Microsoft-MIEngine-Error-fj1kw2qv.ntj --pid=Microsoft-MIEngine-Pid-l5zgjw2m.gcu --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
拼接a[]和b[]
0 1 2 3 7 5 8 4
然后拼接c[]
0 1 2 3 7 5 8 4 6 9 10 11
对数组排序
0 1 2 3 4 5 6 7 8 9 10 11
打印数组长度
12
E:\Administrator\Documents\My C>
===================================================================================================
数字数组查找函数arrsch()/*数组搜索函数*/
void arrsch(size_t arr[], size_t key)
{
size_t temp[arrlen(arr)];
size_t j = 0;
for (size_t i = 0; i < arrlen(arr); i++)
{
if (*(arr + i) == key)
{
temp[j] = i;
j++;
}
}
if (j >= 1)
{
printf("数据:%d\n出现次数:%d\n下标为:", key, j);
for (size_t i = 0; i < j; i++)
{
printf("%02d ", temp[i]);
}
}
else
{
printf("数据: %d\n出现次数: %d\n下标为: NULL", key, 0);
}
}
|