[英]Why are my qsort results incorrect?
When I run add cards using add_card, on the 7th card it is supposed to sort all of the cards. 当我使用add_card运行添加卡时,应该在第7张卡上对所有卡进行排序。 But when I run this I get a semi-ordered result.
但是当我运行它时,我得到了一个半排序的结果。
>> require 'ext/straight_count' #=> true >> s = EV::StraightCount.new; s.add_card(5,0); s.add_card(8,1); s.add_card(12,2); s.add_card(14,3); s.add_card(12,4); s.add_card(3,5); s.add_card(5,6)
card: 12
card: 5
card: 12
card: 14
card: 8
card: 5
card: 3
I don't think there is a problem with NUM2INT, because when I print the array back unordered, it comes out as expected. 我认为NUM2INT没问题,因为当我无序打印阵列时,结果如预期的那样。
straight.h straight.h
int *pCards, *pSortedCards;
int cCards[NUM_CARDS], cSortedCards[NUM_CARDS];
straight.c straight.c
void Init_straight()
{
pCards = &cCards[0];
}
static VALUE
add_card(VALUE self, int rCardValue, int rCardIndex)
{
*(pCards + NUM2INT(rCardIndex)) = NUM2INT(rCardValue);
if (NUM2INT(rCardIndex) == 6)
check_for_straight();
return Qnil;
}
check_for_straight()
{
sort_by_value(pCards);
}
card_sort.c card_sort.c
int compare_card_values (const void *a, const void *b)
{
const double *da = (const double *) a;
const double *db = (const double *) b;
return (*da > *db) - (*da < *db);
}
void sort_by_value(int *pCards)
{
qsort(pCards, NUM_CARDS, sizeof(pCards[0]), compare_card_values);
}
You're casting the card values to double
in compare_card_values
even though the array contains int
. 即使数组包含
int
您也将卡值转换为compare_card_values
double
。 Try this instead: 尝试以下方法:
int compare_card_values (const void *a, const void *b)
{
const int *da = (const int *) a;
const int *db = (const int *) b;
return (*da > *db) - (*da < *db);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.