【发布时间】:2016-02-06 05:54:23
【问题描述】:
目前我得到了这个:
struct employe{
char nomE[25];
char posteE;
float nbHeureE;
float tauxE;
} employes[16];
int nbPers = 0;
void readFile(){
int i=0;
FILE * entree;
if(entree = fopen("employes.dat", "r"))
{
fscanf(entree, "%24c %c %f %f", &employes[i].nomE, &employes[i].posteE, &employes[i].nbHeureE, &employes[i].tauxE);
nbPers++;
while(!feof(entree))
{
i++;
fscanf(entree, "%24c %c %f %f", &employes[i].nomE, &employes[i].posteE, &employes[i].nbHeureE, &employes[i].tauxE);
nbPers++;
}
fclose(entree);
}
else printf("Impossible d'ouvrir le fichier!\n");
}
void trier(struct employe employes[], int nbPers){
int j,i,k;
struct employe temp;
for(i=0;i<16;i++)
{
for(j=1;j<15;j++)
{
if(strcmp(employes[i].nomE, employes[j].nomE) < 0)
{
temp = employes[i];
employes[i] =employes[j];
employes[j] = temp;
}
}
}
}
int main() {
int p=0;
readFile();
trier(employes, nbPers);
for(p=0; p<nbPers; p++)
printf("%s", employes[p].nomE);
return 0;
}
employes.dat 如下所示:
Tremblay Alain A 35.0 35.5
Vachon Jean P 40.0 22.75
Lapalme Justin O 40.0 15.75
Deschenes Sylvie P 35.0 25.0
Lachance Carl O 37.5 18.0
Labonte Chantal P 40.0 20.0
Doucet Michel A 40.0 33.75
Desjardins Alex P 35.0 25.0
Tardif Guy A 40.0 28.5
Clinclin Stephane O 40.0 20.75
Lafleur Marie A 37.5 32.75
Desbiens Robert P 35.0 25.0
Desautels Maryse P 35.0 26.0
St-germain guy O 37.5 15.0
Bourgeois Louis A 37.5 29.0
St-amour Flavie P 40.0 25.0
我正在尝试按名称 (char nomE[25];) 的字母顺序对我的员工结构进行排序。但是,由于某种原因,它不会对名字进行排序并输出:
Tremblay Alain
Bourgeois Louis
Clinclin Stephane
Desautels Maryse
Desbiens Robert
Deschenes Sylvie
Desjardins Alex
Doucet Michel
Labonte Chantal
Lachance Carl
Lafleur Marie
Lapalme Justin
St-amour Flavie
St-germain guy
Tardif Guy
Vachon Jean
如果有人知道原因,我将非常感谢您的回答。提前谢谢你。
【问题讨论】:
-
我认为这个链接应该可以帮助你stackoverflow.com/questions/28071593/…
-
我还没有尝试过你的代码,但从我对 BubbleSort 的记忆(喘气!)内循环应该类似于
for(j=i;j<16;j++)... 当你弄清楚一切时,你可能想要删除硬编码的 16(只是说)。干杯。 -
我尝试了你的建议,但它只是颠倒了字母顺序,仍然跳过了名字。
标签: arrays string sorting structure strcmp