Arrays 排序不会对数组中的第一项进行排序
现在我得到了这个:Arrays 排序不会对数组中的第一项进行排序,arrays,string,sorting,structure,strcmp,Arrays,String,Sorting,Structure,Strcmp,现在我得到了这个: 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
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;
}
我试图按照姓名的字母顺序对我的雇员结构进行排序(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
如果有人知道原因,我会非常感激你的回答。提前谢谢。更改:
for(i=0;i<16;i++) {
for(j=1;j<15;j++){
for(i=0;我认为这个链接应该对你有所帮助我还没有试过你的代码,但是从我对BubbleSort(gasp!)的记忆来看,内部循环应该类似于for(j=i;jI尝试了你的建议,但它只是颠倒了字母顺序,仍然跳过了名字。现在它应该位于Tardif Guy和Vachon Jean之间的末尾…不,我理解正确。可能是因为你用将它运行到第15个元素没有区别,你只需访问元素(名称)通过结构数组中的每个结构。
for(i=0;i<16;i++) {
for(j=1;j<15;j++){
for(i=0;i<16;i++) {
for(j=0;j<15;j++){