【发布时间】:2016-09-02 16:21:16
【问题描述】:
首先,我看到了一些例子:
但它们对我不起作用。
我的结构是
typedef struct Team {
int total;
char name[N][N]; // Nombre
int points[N]; // Points
int pg[N]; // Won Matches
int pe[N]; // Tied Matches
int pp[N]; // Lost Matches
int gf[N]; // Goals For
int gc[N]; // Goals Against
} Teams;
int compareTeams(struct Team *e1, struct Team *e2){
int comparer;
int dg1 = e1->gf - e1->gc;
int dg2 = e2->gf - e2->gc;
//classified according to the points
if (e1->points > e2->points) {
comparer=-1;
} else if (e1->points < e2->points) {
comparer=1;
} else {
// with the same points, goal difference check
if (dg1 > dg2) {
comparer=-1;
} else if (dg1 < dg2) {
comparer=1;
} else {
// with the same goal difference , we check who scored more goals
if(e1->gf > e2->gf) {
comparer=-1;
} else if (e1->gf < e2->gf) {
comparer=1;
} else
comparer=0;
}
}
return comparer;
}
在主函数中,我有这个:
Teams teams[100];
qsort(teams, teams->total, sizeof(struct Team), &compareTeams);
但显然,它不起作用:(
【问题讨论】:
-
您是遇到错误还是数组没有正确排序?
-
如果您有
N团队,最好有一个N团队结构数组,而不是一个包含许多N数据点数组的结构。