【问题标题】:C: How to sort a Struct by one of its element? Can't use pointersC:如何按其中一个元素对 Struct 进行排序?不能使用指针
【发布时间】:2017-10-09 13:31:21
【问题描述】:

我如何对这样的结构进行排序:

typedef struct
{
    int weight;
    int price;
    Color color;
    Equip equip;
}Cars;

通过价格或重量等属性之一? Automobil 数组是先前声明的。 我不能使用指针和任何其他内置函数。

Cars automobil[5]; 
Cars mobilOne={};

for(i=0; i<5; i++)
{
    if((i+1)==5)
    {
        break;
    }else
    {
        if (automobil[i].weight> automobil[i+1].weight)
        {
            mobilOne = automobil[i];
            automobil[i] = automobil[i+1];
            automobil[i+1] = mobilOne;
        }
    }
}

我试图这样做,这样,但它没有做任何事情...... 另外,如果有人能告诉我,我怎样才能将这样的结构传递到函数中,我将非常感激!

【问题讨论】:

  • 你会遇到编译错误,因为你拼错了“wieght”。
  • 你正在“发明”bubble sort。不幸的是,您需要 两个 循环,它们相互嵌套,才能工作。
  • 您的目标是完成排序工作,还是创建自己的排序例程?即你能使用qsort()吗?它已经存在,并且用途广泛。为什么你不能使用指针?这是你正在上课的练习吗?
  • 可能我可以使用某种功能,老师没有提到这一点。我认为,我只能使用我知道的东西。
  • 而不是在 i

标签: c function sorting struct


【解决方案1】:

好的,首先,您尝试做的事情并不像某些人可能告诉您的那么糟糕,因为小 N 冒泡排序仍然很快。以下将做你,当然你需要一个双循环:

int main() {
    Cars automobil[NC];
    // Initialiase automobil here

    for (int i = 0; i < NC - 1; ++i) {
        int am = i;
        for (int j = i+1; j < NC; ++j) {
            if ( automobil[am].weight > automobil[j].weight )
                am = j;
        }

        if ( am != i) {
            Cars tmp = automobil[am];
            automobil[am] = automobil[i];
            automobil[i] = tmp;
        }
    }

    for (int i = 0; i < NC; ++i)
        printf("%d\n", automobil[i].weight);

}

请注意,我们可以复制结构,但即使在这里我们也尽量少做。

但是,很容易说“我永远不会拥有超过十辆汽车”,然后发现您正在尝试对数千辆汽车进行排序,因此我敦促您学习和理解 qsort():

int carsSort(const void *a, const void *b) {
    return ((Cars *) a)->weight - ((Cars *) b)->weight;
}

int main() {
    Cars automobil[NC];
    // Initialiase automobil here

    qsort(automobil, NC, sizeof *automobil, carsSort);

    for (int i = 0; i < NC; ++i)
        printf("%d\n", automobil[i].weight);
}

约翰

PS:回复“如何将数组传递给函数?”记住 K&R 中的一句名言:“将数组名传递给函数时,传递的是数组开头的位置”。

因此:

int carsSort(const void *a, const void *b) {
    return ((Cars *) a)->weight - ((Cars *) b)->weight;
}

void sortThem(Cars autom[]) {
    qsort(autom, NC, sizeof *autom, carsSort);
}

int main() {
    Cars automobil[NC];       
    // Initialiase automobil here

    sortThem(automobil);

    for (int i = 0; i < NC; ++i)
        printf("%d\n", automobil[i].weight);
}

在sortThem()里面“autom”是一个变量,其值为automobil[0]的地址。

【讨论】:

    【解决方案2】:

    这里没有详细介绍实现细节,这里是一个冒泡排序的过程算法(不是 C 语言):Bubble Sort Algorithm。请注意,如 cmets 中所述,此冒泡排序实现使用嵌套循环。

    还有一点要记住:为了切换两个对象,需要使用第三个相同类型的临时对象。例如:

    int temp
    int arr1[]={2,5,7,2,9,1,8,0,5,2,1};
    int count = sizeof(arr1)/sizeof(arr1[0])
    for(int i = 0; i < count-1; i++ )
    {
        if(arr1[i]>arr1[i+1])
        {
            temp = arr1[i];
            arr1[i] = arr1[i+1];
            arr[i+1] = temp;
        }
    }
    

    因为您正在对成员集合中的单个成员进行排序,所以每次交换条件存在时,赋值交换例程都需要交换每个成员,即,尽管确定交换条件是否存在exists 只考虑一个成员,交换将包括所有个成员:weightpriceColorEquip。并且,如果ColorEquip 属于struct 类型(您的帖子未指定),则属于被比较的数组元素的这些对象的每个成员也需要交换。

    你应该期待最终使用指针,因为这将大大减少完成这种排序所需的赋值语句的数量。

    【讨论】:

      猜你喜欢
      • 2021-09-02
      • 2012-07-07
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多