【问题标题】:How to order structure instances based on the value of a member如何根据成员的值对结构实例进行排序
【发布时间】:2019-04-16 19:35:34
【问题描述】:

我正在尝试为通过十字路口的汽车制定算法。我目前在根据其他汽车的优先级决定应该通过哪辆车时遇到问题。

这是我正在做的示例代码

int main(void)
{
    struct Cars{
        int priority;
    };

    struct Cars c1;
    struct Cars c2;
    struct Cars c3;
    struct Cars c4;

    c1.priority=2;
    c2.priority=1;
    c3.priority=3;
    c4.priority=0;

    int Priorities[4] = {c1.priority,c2.priority,c3.priority,c4.priority};

    int Order[4];
}

我想要做的是试图弄清楚如何用接下来要通过的汽车的 ID 填充 Order 数组。 在本例中,我希望 Order 数组为 [4,2,1,3]。 0 为最高优先级,3 为最低优先级

虽然如果你能帮我弄清楚哪辆车有 哪个优先级,所以我可以写一些 if 之类的:

if(the car with priority 0 has passed AND the car with priority 1 has passed)
{
    car with priority 2 can pass
}

【问题讨论】:

  • 使用计数器和链表。
  • 对不起,我不太明白。列表对我有什么帮助?
  • 一个链接列表。如果您有一个列表,其中每个结构都有一个指向下一个结构的指针(按优先级顺序),您的工作会容易得多。
  • 有很多方法可以解决这个问题。你应该提供更多它出现的背景——展示你被分配的课程作业。一种简单的方法是编写代码,遍历Cars 的列表,并将每个列表与迄今为止最好的列表进行比较。当它发现一辆汽车的优先级高于目前最好的汽车时,它会更新目前最好的汽车。...
  • ... 更复杂的方法包括按汽车声明的优先级对汽车列表进行排序,按排序顺序维护列表并将新车插入列表中的适当位置,以及使用更复杂的数据结构维护有关订单的信息。你应该使用哪一个取决于你的学习阶段、课程所教的内容以及具体的任务是什么。

标签: c logic


【解决方案1】:

首先将“传递”变量作为 Cars 的一部分可能会更好,实际上可能将 Cars 重命名为 Car,因为它只代表一辆车。它可能会帮助您更好地想象问题。

struct Car{
    int priority;
    int passed; // lets set this to 1 if passed
};

还有一个交集结构

struct Intersection
{
    struct Car cars[4]; // let's give an intersection 4 cars
}

设置与一些汽车的交叉路口结构,将它们的“通过”变量初始化为 0,表示“未通过”。

struct Intersection intersection;

// set the car priority
intersection.cars[0].priority = 2;
intersection.cars[1].priority = 1;
intersection.cars[2].priority = 3;
intersection.cars[3].priority = 0;

intersection.cars[0].passed = 0;
intersection.cars[1].passed = 0;
intersection.cars[2].passed = 0; /** Could do a loop here to initialize instead! */
intersection.cars[3].passed = 0;

您可以使用循环,循环直到所有汽车都通过

一个示例程序(效率不高),因为我考虑到您可能是 C 新手,并且可能还没有学习过链表或指针等之类的东西。

#include <stdio.h>

int main()
{
    struct Car{
        int priority;
        int passed;
    };

    // four cars per intersection
    struct Intersection{
        struct Car cars[4];
    };

    struct Intersection intersection;

    // set the car priority
    intersection.cars[0].priority = 2;
    intersection.cars[1].priority = 1;
    intersection.cars[2].priority = 3;
    intersection.cars[3].priority = 0;

    intersection.cars[0].passed = 0;
    intersection.cars[1].passed = 0;
    intersection.cars[2].passed = 0;
    intersection.cars[3].passed = 0;

    // keep looping until all cars passed
    while (!intersection.cars[0].passed ||
        !intersection.cars[1].passed ||   /* <--- this could be improved !! */
        !intersection.cars[2].passed ||
        intersection.cars[3].passed)
    {

        int best_priority = -1;
        int best_car_index = -1;

        // look for next car that can pass
        for (int i = 0; i < 4; i++)
        {
            // this car hasn't passed yet -- check priority
            if (!intersection.cars[i].passed)
            {
                           // if not found a car yet to pass or this car is better, then choose this one so far...                  
                if ((best_priority == -1) || (intersection.cars[i].priority < best_priority))
                {
                    best_car_index = i;
                    best_priority = intersection.cars[i].priority;
                }
            }
        }

        if (best_car_index == -1)
            break; // nothing found
        else
        {
            // set the car with best priority to 'passed'
            intersection.cars[best_car_index].passed = 1;

            printf("Car ID %d with priority %d just passed the intersection!\r\n", best_car_index, best_priority);
        }
    }

    return 0;

}

程序输出

Car ID 3 with priority 0 just passed the intersection!
Car ID 1 with priority 1 just passed the intersection!
Car ID 0 with priority 2 just passed the intersection!
Car ID 2 with priority 3 just passed the intersection!

该程序可能不会执行您想要的操作,但希望您能够以您想要的方式操作它。

(您可以填写新列表而不是打印数据)

【讨论】:

  • 嗯,是的,它对我有用,我确实需要根据我的需要对其进行一些修改,但它是迄今为止我想要的最接近的。感谢朋友的帮助
猜你喜欢
  • 1970-01-01
  • 2020-04-24
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
相关资源
最近更新 更多