gpt4 book ai didi

c - 两个 block 的字典顺序比较

转载 作者:太空宇宙 更新时间:2023-11-04 03:53:17 25 4
gpt4 key购买 nike

执行两个 block 的字典顺序比较。我测试我的使用各种类型的 block 的程序。人品测试都是货。第一个测试不好,因为好的答案是 0 但现在是 1。第二次测试没问题(1 == 1)第三次测试ok(-1 == -1)帮助我进行第一次测试。谢谢

这是我的代码:

    int compare_ints(const void* v1, const void* v2) {
int *a = (int*)v1;
int *b = (int*)v2;

if (*a == *b) return 0;
else if (*a > *b) return 1;
else return -1;
}

int compare_gen(const void *block1, const void *block2,
size_t elem_size, size_t block1size, size_t block2size,
int (*compare_it)(const void*, const void*)) {
int i = 0;
int result = -2;

if ( elem_size == sizeof(char) )
{
while ( i < block1size && i < block2size)
{
result = (*compare_it)((&((char*)block1)[i]),(&((char*)block2)[i]));
if ( result == 0)
{
i++;
}
else if ( result == -1 || result == 1 )
{

return result;
}
}
if ( result == 0 )
{
if (i == block1size && i == block2size)
{
return result;
}
else if (i != block1size && i == block2size)
{
result = -1;
return result;
}
else
{
result = 1;
return result;
}
}
}
else if (elem_size == sizeof(int))
{
while ( i < block1size && i < block2size)
{
result = (*compare_it)((&((int*)block1)[i]),(&((int*)block2)[i]));
if ( result == 0)
{
i++;
}
else if ( result == -1 || result == 1 )
{
return result;
}
}
if ( result == 0 )
{
if (i == block1size && i == block2size)
{
return result;
}
else if (i != block1size && i == block2size)
{
result = -1;
return result;
}
else
{
result = 1;
return result;
}
}
}
return result;
}

int main()
{
int array1 [10] = {10};
int array2 [10] = {10};
int array3 [10] = {10};
int array4 [9] = {10};
int i;

for (i = 0; i < 10; i++) {
array1[i] = 10;
array2[i] = 10;
array3[i] = 10;
if (i < 9) array4[i] = 10;
}
array3[5] = 9;

// First test
int result = compare_gen(array1, array2, sizeof(int), sizeof(array1), sizeof(array2), (&compare_ints));
printf("1 --> %d\n",result);
// Second test
result = compare_gen(array1, array3, sizeof(int), sizeof(array1), sizeof(array3), (&compare_ints));
printf("2 --> %d\n",result);
// third test
result = compare_gen(array1, array4, sizeof(int), sizeof(array1), sizeof(array4), (&compare_ints));
printf("3 --> %d\n",result);

return 0;
}

最佳答案

索引超出范围

while() 中,凡使用 blockXsize 的地方,都应替换为 blockXsize/elem_size。完成此操作后,i 将在其适当范围内建立索引。此外,将不再需要 if ( elem_size == ...。其他简化也是可能的。

if (1)
{
while ( i < block1size/elem_size && i < block2size/elem_size)
{
//result = (*compare_it)((&((int*)block1)[i]),(&((int*)block2)[i]));
result = (*compare_it)(
(&((char*)block1)[i*elem_size]),
(&((char*)block2)[i*elem_size]));
if ( result == 0)
{
i++;
}
else if ( result == -1 || result == 1 )
{
return result;
}
}
if ( result == 0 )
{
if (i == block1size/elem_size && i == block2size/elem_size)
{
return result;
}
else if (i != block1size/elem_size && i == block2size/elem_size)
{
result = -1;
return result;
}
else
{
result = 1;
return result;
}
}

关于c - 两个 block 的字典顺序比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19127574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com