如何根据成员的值订购结构实例

时间:2019-04-16 19:35:35

标签: 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(the car with priority 0 has passed AND the car with priority 1 has passed)
{
    car with priority 2 can pass
}

1 个答案:

答案 0 :(得分:0)

首先最好将“ passed”变量作为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!

该程序可能无法满足您的要求,但希望您能够以所需的方式对其进行操作。

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