我如何组织一个元素存储在结构中的列表?

时间:2016-12-22 03:32:15

标签: c sorting

我目前正在开展一个项目(对于学校,是的),其中我花了几个小时试图让它工作,但现在我发现自己有一个我似乎无法解决的错误,并且所以我来这里寻求帮助。以下是我认为必要的代码,而不是完整的代码。

typedef struct
{
    int day, month, year;
} typeDate;

typedef struct
{
    int number;
    char name[20];
    char lastname[20];
    typeDate date_of_birth;
} typeCard;

typedef struct
{
    int associate_number;
    typeCard associates[MAX_ELEM];
} typeAssociation;

typeDate date;
typeCard card;
typeCard aux;

int get_data(typeAssociation association)
{
    association.associate_number = 0;

    int e, i;

    FILE* read = fopen("associados.txt", "r");

    for (i = 0; fscanf(read, "%s %s %d %d %d %d", card.name, card.lastname, &card.number,
                    &date.day, &date.month, &date.year)
         != EOF;
         i++)
    {
        association.associate_number++;
        card.date_of_birth = date;
        association.associates[i] = card;
    }
    fclose(read);

    printf("Sort list[1/2/3]?");

    scanf("%d", &e);

    if (e == 1)
    {
       ordenar_n(&associacao);       
    }
    (...)
}

int sort_by_date(typeAssociation association)
{
    int g, m, i;

    for (i = 0; i < association.associate_number - 1; i++)
    {
        m = i;
        for (g = i + 1; g < data.year; g++)
        {
            if (date.year[g] < date.year[m])
            {
                m = g;
            }

            if (date.year[g] == date.year[m])
            {
                if (date.month[g] < date.month[m])
                {
                    m = g;
                }

                else if (date.month[g] == date.month[m])
                {
                    if (date.day[g] < date.day[m])
                    {
                        m = g;
                    }
                }
            }

            aux = association.associates[i];
            association.associates[i] = association.associates[m];
            association.associates[m] = aux;
        }
    }
}

在另一个文件上(调用文件后):

int main(void)
{
   typeAssociation association;

   get_data(association);

}

错误在函数sort_by_date中,并且此函数应该按日期(存储在结构associados.txt中)对列表(由typeDate给出)进行排序(从最新成员到最老的)。我想(如果我错了,请纠正我)我有一般的想法,但不是代码。谁能帮我? (这是我的第一篇文章,对任何错误感到抱歉)。

  

我的错误在sort_by_date函数中。

2 个答案:

答案 0 :(得分:1)

您正在按值传递typeAssociation。您的排序不适用于传入的对象。另外,不要编写自己的排序。 qsort是一个库函数。

int sort_by_date(typeAssociation *association)
{
    qsort(association->associates, association->associate_number, sizeof(typeCard), compare_typeCard);
}

int compare_typeCard(const void *avoid, const void *bvoid)
{
    typeCard *a = avoid;
    typeCard *b = bvoid;
    /* put your comparison code here */
}

而且,调用getAssociation时会出现相同的pass-by-value错误。

答案 1 :(得分:0)

您需要传递typeAssociation变量的地址。 同样在您的代码中,您将数组元素与一些错误的全局变量进行比较。

int sort_by_date(typeAssociation *association)
{
    int g=0, m=0, i=0;

    for (i = 0; i < association->associate_number - 2; i++)
    {
        m = i;
        for (g = i + 1; g < association->associate_number; g++)
        {
            if (association->associates[g].date.year < association->associates[m].date.year)
            {
                m = g;
            }

            else if (association->associates[g].date.year == association->associates[m].date.year)
            {
                if (association->associates[g].date.month < association->associates[m].date.month)
                {
                    m = g;
                }

                else if (association->associates[g].date.month == association->associates[m].date.month)
                {
                    if (association->associates[g].date.day < association->associates[m].date.day)
                    {
                        m = g;
                    }
                }
            }
        }

        //No need to do it in i loop
        if(i != m)
        {
            aux = association.associates[i];
            association.associates[i] = association.associates[m];
            association.associates[m] = aux;
        }
    }

    return 0; //Need to return an int value.
}