【问题标题】:Sorting does not sort first item in the array排序不对数组中的第一项进行排序
【发布时间】: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&lt;16;j++) ... 当你弄清楚一切时,你可能想要删除硬编码的 16(只是说)。干杯。
  • 我尝试了你的建议,但它只是颠倒了字母顺序,仍然跳过了名字。

标签: arrays string sorting structure strcmp


【解决方案1】:

变化:

for(i=0;i<16;i++) {
    for(j=1;j<15;j++){ 

到:

for(i=0;i<16;i++) {
    for(j=0;j<15;j++){

编辑: 您将 SelectionSortBubbleSort 混淆了。 SelectionSort 与i = 0j = i + 1 一起使用,但也与i &lt; 15j &lt; 16 一起使用,而BubbleSort 是从i = 1i &lt; 16j = 0j &lt; 15。上面给定的版本的缺点是它只做了比需要的多一点的交换,但最后仍然产生了正确的结果。请参阅我对它在 ideone.com 环境中的实施的评论。

因为您在第一次迭代后跳过检查第一个元素。

【讨论】:

  • 当它应该在 Tardif Guy 和 Vachon Jean 之间进行时,它现在走到了尽头......
  • 不,我猜对了here。这可能是因为如果最后没有Vachon Jean,则使用&lt;15 而不是&lt;16 将其运行到第15 个元素。通常Tremblay Alain 是排序后的最后一个。
  • 没有区别,通过结构体数组中的每个结构体访问元素(名称)即可。
猜你喜欢
  • 2021-11-11
  • 1970-01-01
  • 2021-08-26
  • 2020-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多