私の英語はとても下手なので、間違いがあれば許してください。ありがとうございました !
qsort を使用してこの構造体をソートすると、この問題が発生します。
typedef struct
{
double cost,earn;
}ac;
私はこのようにそれを並べたい:
int cmp(const void *a,const void *b)
{
ac this_a=*(ac*)a;
ac this_b=*(ac*)b;
return (this_b.earn/this_b.cost-this_a.earn/this_a.cost)>0.0;
}
しかし、うまくいきませんでした。私がこれに変更したとき、それは働いた:
int cmp(const void *a,const void *b)
{
ac this_a=*(ac*)a;
ac this_b=*(ac*)b;
return (this_a.cost*this_b.earn-this_a.earn*this_b.cost);
}
なぜこれが起こったのですか?2つの機能に違いはありますか? それとも、コードの他の部分が間違っているのでしょうか?