c - 如何根据成员的值对结构实例进行排序

标签 c logic

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

这是我正在做的示例代码

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 的一部分,实际上可能将 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!

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

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

关于c - 如何根据成员的值对结构实例进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55715475/

相关文章:

c - 将两位数字打印为字符

c++ - 'char *args[]' 和 'char **argv' 作为参数的区别

c - 在 gcc 32 位代码中未定义的对 `_GLOBAL_OFFSET_TABLE_' 的引用,用于一个简单的函数,独立的操作系统

c - 允许 "overriding"具有内部链接的零初始化对象

javascript - 我们如何在没有条件的情况下递增然后递减计数器?

javascript - 确保多个不重叠时间范围的好方法

python - 如何用循环缩短这个网格移动逻辑(python)

java - Android:循环遍历字符串数组 - Intent 方法的逻辑需要帮助

javascript - javascript 中带有字符串的简单 if 条件的问题

c - 需要从 stdin 读取不确定数量的整数到数组中