【问题标题】:Sorting algorithm for a Dynamic Array in CC中动态数组的排序算法
【发布时间】:2013-03-18 13:49:14
【问题描述】:

我正在尝试对包含成本作为元素的动态数组进行排序。这是我的排序功能:

void ascending_sort(Controller* ctrl) //the function is in the controller
{
    int i,j;
    DynamicVector* CostList=getAll(ctrl->repo); //the getALL function returns the CostList that has Costs as elements
    for(i=0;i<getLen(CostList)-1;i++)
    {
        Cost* cost=(Cost*)getElementAtPosition(CostList,i); //getElementAtPosition returns the Cost at a certain position
        for(j=i+1;j<getLen(CostList);j++)
        {
            Cost* cost1=(Cost*)getElementAtPosition(CostList,j);
            if(cost->sum > cost1->sum)
            {
            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!");
}

这是结构:

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

这是在控制台中调用的排序函数:

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

这是一个返回 Cost* 类型的函数:

Cost* initCost(char* day, char* type, int sum)
{
    Cost* c=(Cost*)malloc(sizeof(Cost));
    if(strlen(day)>0 && strlen(day)<=10)
    {
        c->day = copyString(day);
    }
    else
    {
        c->day=0;
    }
    if(strlen(type)>0 && strlen(type)<=20)
    {
        c->type = copyString(type);
    }
    else
    {
        c->type=0;
    }
    if(sum>0)
    {
        c->sum= sum;
    }
    return c;
}

控制台:

Console* initConsole(Controller* ctrl)
{
    Console* console=(Console*)malloc(sizeof(Console));
    console->ctrl=ctrl;
    return console;
}

getElementAtPosition

TElem getElementAtPosition(DynamicVector* v, int pos)
{
    if(v->elem[pos])
        return v->elem[pos];
    else
        return 0;
}

typedef void* TElem;

【问题讨论】:

  • 您能说明Console-type 的定义和getElementAtPosition 的定义吗?
  • 你到处乱扔void*。如果您为系统定义了显式类型,您的错误可能会被编译器捕获。您要求编译器通过转换为 void* 来忽略类型不匹配:)

标签: c arrays sorting dynamic struct


【解决方案1】:

您正尝试在排序代码中以数组形式访问 cost

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

这是不正确的,假设 getElementAtPosition() 返回指向一个项目的指针,而不是可顺序访问的项目数组,即数组。

您只能通过指针通知访问一个元素,例如cost-&gt;sum,而不是cost[i].sum,除非i==0

【讨论】:

  • 我应该如何编写那部分代码?我写了这样的东西:Cost* aux=cost1;成本=成本1;成本1=辅助;但它根本不起作用。 :)
【解决方案2】:

听起来你遇到了段错误。您没有显示返回 Cost* 的函数,并且您在取消引用之前从未验证 Cost*。也许那个区域有问题?我想我需要更多源代码来验证这一点。

我也注意到了这一点。

for(i=0;i<getLen(CostList);i++)
{
    printf("\nCost %d\n\n",i+1);
    Cost *c=(Cost*)getElementAtPosition(CostList,i); <-- typeof(C) == Cost*
    PrintCost(c);  <-- typeof(C) == Console*
}

您将变量c 用作Cost*Console*。这不是一种类型安全的做事方式,所以我猜你这里有一些错误。虽然没有更多代码,但无法判断:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 2020-05-07
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多