使用qsort()时出现问题。请帮忙! [英] Problem using qsort(). Please help!

查看:75
本文介绍了使用qsort()时出现问题。请帮忙!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构:


typedef struct GROUPED

{

short val;

短代码;

短组;

短预测_cd;

短double_ind;

短min;

short max;

} GROUPED;


和一个定义数组:


GROUPED测试[30];

这个结构数组可以包含的价值的一个可能的例子是:
包含:


test [0] .val = 1

test [1] .val = 1

test [2] .val = 2

test [3] .val = 3


test [0] .code = 111

test [1] .code = 112

test [2] .code = 113

test [2] .code = 114

假设selfung字段为0.


我想按val排序结果我正在尝试使用qsort()来实现这个目标。


我的比较功能是:


int compare(const void * arg1,const void * arg2)

{

if(((GROUPED *)arg1) - > val == 0)

返回1;


if(((GROUPED *)arg2) - > val == 0)

返回-1;


if(((GROUPED *)arg1) - > val>((GROUPED *)arg2) - > val)

return(1) ;


返回(-1);

}


和我这样调用qsort:


qsort(匹配,(size_t)30,sizeof(GROUPED),比较);


调用qsort后的结果是:


test [0] .val = 1

test [1] .val = 1

test [2] .val = 2

test [3] .val = 3

test [0] .code = 112

test [1] .code = 111

test [2] .code = 113

test [2] .code = 114


因为很明显它会反转test [0] .code和test [1]。

的代码值是.code t的值帽子有相同的.val值!


我做错了什么?


非常感谢。


Thanos

解决方案



t _ ****** @ yahoo.com 写道:

我有以下结构:

typedef struct GROUPED
{
短val;
短代码;
短组;
短预测_cd;
短双联;
短分;
最短;
} GROUPED;

和一个定义数组:

GROUPED测试[30];

一个可能的例子这个结构数组可以包含的值是:

test [0] .val = 1
test [1] .val = 1
test [2]。 val = 2
测试[3] .val = 3
测试[0] .code = 111
测试[1] .code = 112
测试[2] ] .code = 113
test [2] .code = 114

假设selfung字段为0.

我想按排序对结果进行排序。 VAL"我正在尝试用qsort()来实现这个目标。

我的比较函数是:

int compare(const void * arg1,const void * arg2 )
{
if(((GROUPED *)arg1) - > val == 0)
返回1;

if(((GROUPED *)arg2 ) - > val == 0)
返回-1;

if(((GROUPED *)arg1) - > val>((GROUPED *)arg2) - > val)
返回(1);

返回(-1);
}


怎么样:


if((GROUPED *)arg1-> val> 0)

返回1;

if((GROUPED *)arg1-> ; val< 0)

返回-1;


返回0;


我打电话给qsort就像这个:

qsort(匹配,(size_t)30,sizeof(GROUPED),比较);

调用qsort后的结果如下:
test [0] .val = 1
test [1] .val = 1
test [2] .val = 2
test [3] .val = 3
<测试[0] .code = 112
测试[1] .code = 111
测试[2] .code = 113
测试[2] .code = 114
很明显它反转了测试[0] .code和测试[1]。
的代码值,.code的值具有相同的.val值!

我做错了什么很感谢。

Thanos






< t _ ****** @ yahoo.com>在消息中写道

新闻:7b ************************* @ posting.google.co m ...

< snip>

int compare(const void * arg1,const void * arg2)
{
if((GROUPED) *)arg1) - > val == 0)
返回1;

if(((GROUPED *)arg2) - > val == 0)
返回 - 1;

if(((GROUPED *)arg1) - > val>((GROUPED *)arg2) - > val)
return(1);

返回(-1);
}


从它的外观来看,你的比较功能并没有让我期待

你发布的输出序列。这有几个问题。我会

为了清晰一点,省略了类型转换。


1.如果arg1-> val == arg2-> val == 0,你的比较产生-1,这是错误的
。它应该是0,因为arg1-> val == arg2-> val。


2.如果arg1-> val == arg2-> val == 1,你的比较产生-1,这也是

错误。


比较值应该返回


* -1当且仅当arg1-> val< arg2-> val

* 0当且仅当arg1-> val == arg2-> val

* 1当且仅当arg1-> val > arg2-> val


所以


int compare(const void * arg1,const void * arg2)

{

if(arg1-> val> arg2-> val)

{

返回1;

}

else if(arg1-> val< arg2-> val)

{

返回-1; < br $>
}

其他

{

返回0;

}

}

我打电话给qsort:

qsort(匹配,(size_t)30,sizeof(GROUPED),比较);
<调用qsort后的结果如下:

test [0] .val = 1
test [1] .val = 1
test [2] .val = 2
测试[3] .val = 3
测试[0] .code = 112
测试[1] .code = 111
测试[2]。 code = 113
test [2] .code = 114

很明显它会反转test [0] .code和test [1]。
的代码值,具有相同.val值的.code的值!




就我所见,您所提供的代码并未解释这一点。




t_******@yahoo.com 写道:

我有以下结构:

typedef struct GROUPED
{
短val;
短代码;
短组;
short forecast_cd;
short double_ind;
short min;
short max;
} GROUPED;

和一个define数组:<分组测试[30];

此结构数组可能包含的值的一个可能示例是:

test [0 ] .val = 1
测试[1] .val = 1
测试[2] .val = 2
测试[3] .val = 3

测试[0] .code = 111
test [1] .code = 112
test [2] .code = 113
test [2] .code = 114
假设selfung字段为0.

我想按val对结果进行排序。我正在尝试用qsort()来实现这个目标。

我的比较函数是:

int compare(const void * arg1,const void * arg2 )
{
if(((GROUPED *)arg1) - > val == 0)
返回1;

if(((GROUPED *)arg2 ) - > val == 0)
返回-1;

if(((GROUPED *)arg1) - > val>((GROUPED *)arg2) - > val)
返回(1);

返回(-1);
}


比较函数有缺陷,因为你正在比较

零而不是相互比较。它应该是

之类的东西:


int compare2(const void * v1,const void * v2)

{

const GROUPED * g1 = v1;

const GROUPED * g2 = v2;


if(g1-> val< g2 - > val)返回-1;

if(g1-> val> g2-> val)返回1;

返回0;

}

和我这样叫qsort:

qsort(匹配,(size_t)30,sizeof(GROUPED),比较);


什么是匹配?


我做错了什么?



重做比较功能再试一次。


示例:


#include< stdio.h>

#include< stdlib.h>


typedef struct GROUPED

{

短val;

短代码;

短组;

短预测_cd;

短double_ind;

短分钟;

short max;

} GROUPED;


int compare(const void * v1,const void * v2)

{

const GROUPED * g1 = v1;

const GROUPED * g2 = v2;


return(g1-> val< g2-> val)? - 1:(g1-> val!= g2-> val);

}


int main(void )

{

GROUPED test [30] = {{0}};

short i;


for(i = 0; i <30; i ++)

{/ *赋值给val和代码* /

test [i] .val = 30-i;

test [i] .code = i;

}


put(" \tUnsorted。第一个五个);

for(i = 0; i< 5; i ++)

printf(" test [%hd] .val =%hd test [ %hd] .code =%hd \ n",

i,test [i] .val,i,test [i] .code);

qsort(test ,30,sizeof * test,compare2);

put(" \ n \ tSorted(Ascending)by val .First Five);

for(i = 0; i< 5; i ++)

printf(" test [%hd] .val =%hd test [%hd] .code =%hd \ n",
i,test [i] .val,i,test [i] .code);

返回0;

}

-

Al Bowers

美国佛罗里达州坦帕市

mailto: xa ****** @ myrapidsys.com (删除x发送电子邮件)
http://www.geocities.com/abowers822/


I ''ve got the following structure:

typedef struct GROUPED
{
short val ;
short code;
short group;
short forecast_cd;
short double_ind;
short min;
short max;
} GROUPED;

and an define array:

GROUPED test[30];
A possible example for the values tha this structure array could
contain are:

test[0].val=1
test[1].val=1
test[2].val=2
test[3].val=3

test[0].code=111
test[1].code=112
test[2].code=113
test[2].code=114

Suppose that the remainung fields are 0.

I want to sort the results by "val" and I am trying to use qsort() to
achieve this.

My comparison function is:

int compare( const void *arg1, const void *arg2 )
{
if (((GROUPED *)arg1)->val==0)
return 1;

if (((GROUPED *)arg2)->val==0)
return -1;

if (((GROUPED *)arg1)->val > ((GROUPED*)arg2)->val)
return (1);

return (-1);
}

and I call qsort like this:

qsort(match, (size_t)30, sizeof(GROUPED), compare);

The result after calling qsort is this:

test[0].val=1
test[1].val=1
test[2].val=2
test[3].val=3

test[0].code=112
test[1].code=111
test[2].code=113
test[2].code=114

As it is obvious it reverses test[0].code and test[1].code values that
is, the values of the .code that have the same .val value!

What am I doing wrong?

Thanks a lot.

Thanos

解决方案



t_******@yahoo.com wrote:

I ''ve got the following structure:

typedef struct GROUPED
{
short val ;
short code;
short group;
short forecast_cd;
short double_ind;
short min;
short max;
} GROUPED;

and an define array:

GROUPED test[30];
A possible example for the values tha this structure array could
contain are:

test[0].val=1
test[1].val=1
test[2].val=2
test[3].val=3

test[0].code=111
test[1].code=112
test[2].code=113
test[2].code=114

Suppose that the remainung fields are 0.

I want to sort the results by "val" and I am trying to use qsort() to
achieve this.

My comparison function is:

int compare( const void *arg1, const void *arg2 )
{
if (((GROUPED *)arg1)->val==0)
return 1;

if (((GROUPED *)arg2)->val==0)
return -1;

if (((GROUPED *)arg1)->val > ((GROUPED*)arg2)->val)
return (1);

return (-1);
}
how about :

if ( (GROUPED *)arg1->val > 0 )
return 1;
if ( (GROUPED *)arg1->val < 0 )
return -1;

return 0;


and I call qsort like this:

qsort(match, (size_t)30, sizeof(GROUPED), compare);

The result after calling qsort is this:

test[0].val=1
test[1].val=1
test[2].val=2
test[3].val=3

test[0].code=112
test[1].code=111
test[2].code=113
test[2].code=114

As it is obvious it reverses test[0].code and test[1].code values that
is, the values of the .code that have the same .val value!

What am I doing wrong?

Thanks a lot.

Thanos





<t_******@yahoo.com> wrote in message
news:7b*************************@posting.google.co m...
<snip>

int compare( const void *arg1, const void *arg2 )
{
if (((GROUPED *)arg1)->val==0)
return 1;

if (((GROUPED *)arg2)->val==0)
return -1;

if (((GROUPED *)arg1)->val > ((GROUPED*)arg2)->val)
return (1);

return (-1);
}
By the looks of it, your compare function does not lead me to expect the
output sequence you posted. There are several things wrong with it. I''ll
just omit the type-cast for clarity for a moment.

1. If arg1->val == arg2->val == 0, your comparison produces -1, which is
wrong. It should have been 0 since arg1->val == arg2->val.

2. If arg1->val == arg2->val == 1, your comparison produces -1, which is
wrong, too.

The comparison value should return

* -1 if and only if arg1->val < arg2->val
* 0 if and only if arg1->val == arg2->val
* 1 if and only if arg1->val > arg2->val

So

int compare( const void *arg1, const void *arg2 )
{
if(arg1->val > arg2->val)
{
return 1;
}
else if (arg1->val < arg2->val)
{
return -1;
}
else
{
return 0;
}
}

and I call qsort like this:

qsort(match, (size_t)30, sizeof(GROUPED), compare);

The result after calling qsort is this:

test[0].val=1
test[1].val=1
test[2].val=2
test[3].val=3

test[0].code=112
test[1].code=111
test[2].code=113
test[2].code=114

As it is obvious it reverses test[0].code and test[1].code values that
is, the values of the .code that have the same .val value!



That is not explained by the code you presented, as far as i can see.




t_******@yahoo.com wrote:

I ''ve got the following structure:

typedef struct GROUPED
{
short val ;
short code;
short group;
short forecast_cd;
short double_ind;
short min;
short max;
} GROUPED;

and an define array:

GROUPED test[30];
A possible example for the values tha this structure array could
contain are:

test[0].val=1
test[1].val=1
test[2].val=2
test[3].val=3

test[0].code=111
test[1].code=112
test[2].code=113
test[2].code=114

Suppose that the remainung fields are 0.

I want to sort the results by "val" and I am trying to use qsort() to
achieve this.

My comparison function is:

int compare( const void *arg1, const void *arg2 )
{
if (((GROUPED *)arg1)->val==0)
return 1;

if (((GROUPED *)arg2)->val==0)
return -1;

if (((GROUPED *)arg1)->val > ((GROUPED*)arg2)->val)
return (1);

return (-1);
}
The compare function is flawed in that you are comparing against
zero instead of comparing against each other. It should be something
like:

int compare2(const void *v1, const void *v2)
{
const GROUPED *g1 = v1;
const GROUPED *g2 = v2;

if(g1->val < g2->val) return -1;
if(g1->val > g2->val) return 1;
return 0;
}
and I call qsort like this:

qsort(match, (size_t)30, sizeof(GROUPED), compare);
What is match?

What am I doing wrong?


Redo the compare funcition and try again.

Example:

#include <stdio.h>
#include <stdlib.h>

typedef struct GROUPED
{
short val ;
short code;
short group;
short forecast_cd;
short double_ind;
short min;
short max;
} GROUPED;

int compare(const void *v1, const void *v2)
{
const GROUPED *g1 = v1;
const GROUPED *g2 = v2;

return (g1->val<g2->val)?-1:(g1->val!=g2->val);
}

int main(void)
{
GROUPED test[30] = {{0}};
short i;

for(i = 0; i < 30;i++)
{ /* Assign values to val and code */
test[i].val = 30-i;
test[i].code = i;
}

puts("\tUnsorted. First Five");
for(i = 0; i < 5; i++)
printf("test[%hd].val = %hd test[%hd].code = %hd\n",
i,test[i].val,i,test[i].code);
qsort(test,30,sizeof *test,compare2);
puts("\n\tSorted(Ascending) by val. First Five");
for(i = 0; i < 5; i++)
printf("test[%hd].val = %hd test[%hd].code = %hd\n",
i,test[i].val,i,test[i].code);
return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


这篇关于使用qsort()时出现问题。请帮忙!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆