|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <assert.h>
/*用字符e表示一个数字数组的结束,el = endline*/
#define el "'e'"
/*数组长度函数*/
int arrlen(size_t arr[])
{
assert(arr != NULL);
size_t i = 0;
for (; *arr != el; arr++)
{
i++;
}
/*while (*arr != el)
{
arr++;
i++;
}*/
return i;
}
/*数字数组排序函数*/
int arrsrt(size_t arr[])
{
assert(arr != NULL);
for (size_t i = 0; i < arrlen(arr); i++)
{
for (size_t j = 0, k = 0; j < arrlen(arr) - 1; j++)
{
if (*(arr + j) > *(arr + j + 1))
{
k = *(arr + j);
*(arr + j) = *(arr + j + 1);
*(arr + j + 1) = k;
}
}
}
return arr;
}
/*数字数组拼接函数arrcat(arr1, arr2)*/
void arrcat(size_t arr1[], size_t arr2[])
{
assert((arr1 != NULL) && (arr2 != NULL));
while (*arr1 != el)
{
arr1++; //跳到arri尾地址
}
while (*arr2 != el)
{
*(arr1++) = *(arr2++); //复制arr2数据到arr1尾部
}
*arr1 = el;
}
/*数组搜索函数*/
void arrsch(size_t arr[], size_t key)
{
assert(arr != NULL);
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);
}
}
/*数字数组替换函数*/
int arrrpl(size_t arr[], size_t key, size_t num)
{
assert(arr != NULL);
size_t i = 0;
for (; *arr != el; arr++)
{
if (*arr == key)
{
i++;
break;
}
}
switch (i)
{
case 0:
return 0;
break;
case 1:
if ((*arr == 0) && (key == 0))
{
*arr = num;
}
else
{
for (; *arr != el; arr++)
{
if (*arr == key)
{
*arr = num;
}
}
}
return arr;
break;
}
}
#include <stdio.h>
#include <time.h>
#include "E:\Administrator\Documents\My C\My C Function\Function.c"
/*数组搜索函数*/
void main()
{
int m, n;
m = rand()%5+5;
n = rand()%5+5;
int a[m], b[n];
srand(time(NULL));
for (size_t i = 0; i < m; i++)
{
a[i] = rand()%5+5;
printf("%d ", a[i]);
}
a[m] = el;
printf("\n%d", arrlen(a));//运行正常
printf("\n\n");
for (size_t i = 0; i < n; i++)
{
b[i] = rand()%5+5;
printf("%d ", b[i]);
}
b[n] = el;
printf("\n%d", arrlen(b));//运行正常
arrcat(a, b);
printf("\n%d", arrlen(a));//运行到这里出现分段故障,注释掉这一句又会变得正常,头疼找不到问题在哪
}
看你写了这么多感觉也算是复杂的代码了,看得我都晕。
但是你没注意到,要拼接2个数组,得要先有足够大的空间来拼接。
假如你数组a 分了10个, 数组b也分了10个,而且都存满了,这怎么拼?
假如你数组a 分了10个, 数组b也分了10个, a,b都各存了4个元素,这时你要把b拼入a是可以的。
#include <stdio.h>
#include <time.h>
#include <stdio.h>
#include <assert.h>
/*用字符e表示一个数字数组的结束,el = endline*/
#define el "'e'"
/*数组长度函数*/
int arrlen(size_t arr[])
{
assert(arr != NULL);
size_t i = 0;
for (; *arr != el; arr++)
{
i++;
}
/*while (*arr != el)
{
arr++;
i++;
}*/
return i;
}
/*数字数组排序函数*/
int arrsrt(size_t arr[])
{
assert(arr != NULL);
for (size_t i = 0; i < arrlen(arr); i++)
{
for (size_t j = 0, k = 0; j < arrlen(arr) - 1; j++)
{
if (*(arr + j) > *(arr + j + 1))
{
k = *(arr + j);
*(arr + j) = *(arr + j + 1);
*(arr + j + 1) = k;
}
}
}
return arr;
}
/*数字数组拼接函数arrcat(arr1, arr2)*/
void arrcat(size_t arr1[], size_t arr2[], int *p)
{
assert((arr1 != NULL) && (arr2 != NULL));
while (*arr1 != el)
{
*p++ = *arr1++; //跳到arri尾地址
}
while (*arr2 != el)
{
*p++ = *arr2++;
}
*p = el;
}
/*数组搜索函数*/
void arrsch(size_t arr[], size_t key)
{
assert(arr != NULL);
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);
}
}
/*数字数组替换函数*/
int arrrpl(size_t arr[], size_t key, size_t num)
{
assert(arr != NULL);
size_t i = 0;
for (; *arr != el; arr++)
{
if (*arr == key)
{
i++;
break;
}
}
switch (i)
{
case 0:
return 0;
break;
case 1:
if ((*arr == 0) && (key == 0))
{
*arr = num;
}
else
{
for (; *arr != el; arr++)
{
if (*arr == key)
{
*arr = num;
}
}
}
return arr;
break;
}
}
/*数组搜索函数*/
void main()
{
int m, n;
int len_m, len_n, len_mn;
int *p = NULL;
m = rand()%5+5;
n = rand()%5+5;
int a[m], b[n];
srand(time(NULL));
for (size_t i = 0; i < m; i++)
{
a[i] = rand()%5+5;
printf("%d ", a[i]);
}
a[m] = el;
len_m = arrlen(a);
printf("\n%d", len_m);//运行正常
printf("\n\n");
for (size_t i = 0; i < n; i++)
{
b[i] = rand()%5+5;
printf("%d ", b[i]);
}
b[n] = el;
len_n = arrlen(b);
printf("\n%d", arrlen(b));//运行正常
len_mn = len_m + len_n;
p = malloc(len_mn*sizeof(int));
arrcat(a, b, p);
printf("\n%d", arrlen(p));//运行到这里出现分段故障,注释掉这一句又会变得正常,头疼找不到问题在哪
}
|
|