1

我正在尝试对动态数组列表进行排序,但它不起作用,我不明白我应该怎么做。这是我到目前为止所做的:

    void ascending_sort(Controller* ctrl)
    {
    int i,j;
    Cost* aux;
    DynamicVector* c=getAllCosts(ctrl);
    for(i=0;i<getLen(c)-1;i++)
    {
        Cost* cost;
        for(j=i+1;j<getLen(c);j++)
        {
            if(cost[i].sum < cost[j].sum)
            {
                    aux=cost[i]; //error
                cost[i]=cost[j];
                cost[j]=aux; //error
            }
        }
    }
}

这是结构:

typedef struct{
    char* day;
    char* type;
    int sum;
}Cost;

我该如何解决?当我声明“Cost* aux”时,我认为我做错了什么。我希望你能帮帮我!

编辑:我更新了排序功能的新代码。现在它不打印我想要的。它只打印“成本 1”,然后我收到一个“结束程序”窗口,它会停止一切。可能是什么问题呢?

这是新的排序算法:

void ascending_sort(Controller* ctrl)
        {
        int i,j;
        DynamicVector* c=getAllCosts(ctrl);
        for(i=0;i<getLen(c)-1;i++)
        {
            Cost* cost=getElementAtPosition(c,i); //returns element on position
            for(j=i+1;j<getLen(c);j++)
            {
                if(cost[i].sum < cost[j].sum)
                {
                const Cost  aux=cost[i];
                            cost[i]=cost[j];
                            cost[j]=aux;
                }
            }
        }
    }

这是打印功能://此功能在控制台中

void PrintCosts(Console* console)
{
    DynamicVector* CostList=getAllCosts(console->ctrl);
    if (getLen(CostList))
    {
        int i;
        for(i=0;i<getLen(CostList);i++)
        {
            printf("\nCost %d\n\n",i+1);
            Cost *c=(Cost*)getElementAtPosition(CostList,i);
            PrintCost(c);
        }

    }
    else printf("No cost in the list!");
}

这是从控制器调用排序函数到控制台的函数:

void AscendingSort(Console* console)
{
    ascending_sort(console->ctrl);
}
4

3 回答 3

2

Cost是的,当您需要实际值时,您正在使用指向 a 的指针。这是最里面的范围应该是什么:

const Cost aux = cost[i];
cost[i] = cost[j];
cost[j] = aux;

请注意,我没有看到(或理解)cost、 的数组Cost以及您在循环中使用c的类型和长度之间的关系。DynamicVector *

此外,您的排序算法不是很好;你应该只使用qsort().

于 2013-03-18T12:13:36.740 回答
1

您使用Cost*而不是Costfor aux

于 2013-03-18T12:15:01.330 回答
0

The revised sorting code is better, but is probably making unwarranted assumptions:

void ascending_sort(Controller* ctrl)
{
    DynamicVector *c = getAllCosts(ctrl);
    for (int i = 0; i < getLen(c)-1; i++)
    {
        Cost *cost = getElementAtPosition(c,i);
        for (int j = i+1; j < getLen(c); j++)
        {
            if (cost[i].sum < cost[j].sum)
            {
                const Cost aux = cost[i];
                cost[i] = cost[j];
                cost[j] = aux;
            }
        }
    }
}

If you need to use getElementAtPosition(c, i), you need to use getElementAtPosition(c, j) to get the second value. That much seems non-controversial to me.

Additionally, if you need the function to get the values, you probably need a function to put the values too.

void ascending_sort(Controller* ctrl)
{
    DynamicVector *c = getAllCosts(ctrl);
    for (int i = 0; i < getLen(c)-1; i++)
    {
        Cost *cost_i = getElementAtPosition(c, i);
        for (int j = i+1; j < getLen(c); j++)
        {
            Cost *cost_j = getElementAtPosition(c, j);
            if (cost_i->sum < cost_j->sum)
                swapElements(c, i, j);
        }
    }
}

Or, maybe:

void ascending_sort(Controller* ctrl)
{
    DynamicVector *c = getAllCosts(ctrl);
    for (int i = 0; i < getLen(c)-1; i++)
    {
        Cost *cost_i = getElementAtPosition(c, i);
        for (int j = i+1; j < getLen(c); j++)
        {
            Cost *cost_j = getElementAtPosition(c, j);
            if (cost_i->sum < cost_j->sum)
            {
                setElementAtPosition(c, i, cost_j);
                setElementAtPosition(c, j, cost_i);
            }
        }
    }
}
于 2013-03-18T16:02:58.980 回答