繁体   English   中英

While循环后C中的空白值(LinkedList)

[英]Blank Values In C After While Loop (LinkedList)

我正在研究一个问题,该问题与解决N体问题的Barnes-Hut算法有关。

我相信这是一个C问题。 但是,为了给您一些上下文,此方法用于获取每个实体的新X和Y位置。

这就是问题所在,它似乎在正确地进行计算,当我打印出我想从循环中看到的值时,它们已正确填充。

但是,当我在循环外打印节点的值时,它们全为零。

我需要进行C编程,有人可以向我解释为什么会发生这种情况,或者如何解决这个问题?

我在下面附加了代码,在下面附加了从控制台看到的打印输出。 任何帮助都将不胜感激!

void reposition (struct Node *node)
{
struct Node *referenceNode = node;
int numberOfBodies = getAmountOfBodies(node->body);
float xValues [numberOfBodies];
float yValues [numberOfBodies];
int index = 0;

while (node->body->next != NULL)
{
    struct Forces * forces = getForce(node->body, node);

    //NOT SURE IF DIVIDING BY BODY MASS, OR NODE MASS
    float xVelocity = node->body->pos_x + (kDeltaTime * forces->x)/node->body->mass;
    float yVelocity = node->body->pos_y + (kDeltaTime * forces->y)/node->body->mass;
    xValues[index] = node->body->pos_x + xVelocity * kDeltaTime;
    yValues[index] = node->body->pos_y + yVelocity * kDeltaTime;
    node->body->v_x = xVelocity;
    node->body->v_y = yVelocity;
    index++;

    printf("RED: %d | GREEN: %d | BLUE: %d | xVelocity: %f | yVelocity: %f \n", node->body->red, node->body->green, node->body->blue, xVelocity, yVelocity);
    node->body = node->body->next;
}
node = referenceNode;

printf("\n");
printf("RED: %d | GREEN: %d | BLUE: %d | xVelocity: %f | yVelocity: %f \n", node->body->red, node->body->green, node->body->blue, xValues[0], yValues[0]);
}

循环之前

RED: 255 | GREEN: 255 | BLUE: 0 | xVelocity: 0.000000 | yVelocity: 0.000000 
 RED: 0 | GREEN: 255 | BLUE: 0 | xVelocity: 57899999232.000000 | yVelocity: 0.000000 
 RED: 255 | GREEN: 0 | BLUE: 255 | xVelocity: 108200001536.000000 | yVelocity: 0.000000 
 RED: 0 | GREEN: 50 | BLUE: 255 | xVelocity: 149599993856.000000 | yVelocity: 0.000000 
 RED: 255 | GREEN: 0 | BLUE: 0 | xVelocity: 227899998208.000000 | yVelocity: 0.000000 

循环后

RED: 0 | GREEN: 0 | BLUE: 0 | xVelocity: 0.000000 | yVelocity: 0.000000 

循环发布后的输出似乎表明LinkedList的最后一个非空节点中的值(因为循环末尾的位置)为零,这就是为什么要获得该输出的原因。

循环中的最后一条语句永久更改节点的body指针。 因此,在循环referenceNode被废弃之后。

这是带有注释的代码,以显示问题[请原谅免费的样式清理]:

void
reposition(struct Node *node)
{
    struct Node *referenceNode = node;
    int numberOfBodies = getAmountOfBodies(node->body);
    float xValues[numberOfBodies];
    float yValues[numberOfBodies];
    int index = 0;

    while (node->body->next != NULL) {
        struct Forces *forces = getForce(node->body, node);

        // NOT SURE IF DIVIDING BY BODY MASS, OR NODE MASS
        float xVelocity = node->body->pos_x + (kDeltaTime * forces->x) / node->body->mass;
        float yVelocity = node->body->pos_y + (kDeltaTime * forces->y) / node->body->mass;

        xValues[index] = node->body->pos_x + xVelocity * kDeltaTime;
        yValues[index] = node->body->pos_y + yVelocity * kDeltaTime;
        node->body->v_x = xVelocity;
        node->body->v_y = yVelocity;
        index++;

        printf("RED: %d | GREEN: %d | BLUE: %d | xVelocity: %f | yVelocity: %f \n",
            node->body->red, node->body->green, node->body->blue,
            xVelocity, yVelocity);

        // NOTE/BUG: on the first iteration this is trashing referenceNode and
        // each subsequent node on each subsequent iteration
        node->body = node->body->next;
    }

    node = referenceNode;

    printf("\n");
    printf("RED: %d | GREEN: %d | BLUE: %d | xVelocity: %f | yVelocity: %f \n",
        node->body->red, node->body->green, node->body->blue,
        xValues[0], yValues[0]);
}

这是更正的代码。 注意,我不得不在结构定义中猜测body指向[我称之为Body ]的含义,但是否则应该没问题:

void
reposition(struct Node *node)
{
    struct Node *referenceNode = node;
    struct Body *body;
    int numberOfBodies = getAmountOfBodies(node->body);
    float xValues[numberOfBodies];
    float yValues[numberOfBodies];
    int index = 0;

    body = node->body;
    while (body->next != NULL) {
        struct Forces *forces = getForce(body, node);

        // NOT SURE IF DIVIDING BY BODY MASS, OR NODE MASS
        float xVelocity = body->pos_x + (kDeltaTime * forces->x) / body->mass;
        float yVelocity = body->pos_y + (kDeltaTime * forces->y) / body->mass;

        xValues[index] = body->pos_x + xVelocity * kDeltaTime;
        yValues[index] = body->pos_y + yVelocity * kDeltaTime;
        body->v_x = xVelocity;
        body->v_y = yVelocity;
        index++;

        printf("RED: %d | GREEN: %d | BLUE: %d | xVelocity: %f | yVelocity: %f \n",
            body->red, body->green, body->blue,
            xVelocity, yVelocity);

        // NOTE/BUGFIX: here we're only advancing the pointer but _not_
        // permanently changing the node's body pointer
        body = body->next;
    }

    node = referenceNode;
    body = node->body;

    printf("\n");
    printf("RED: %d | GREEN: %d | BLUE: %d | xVelocity: %f | yVelocity: %f \n",
        body->red, body->green, body->blue,
        xValues[0], yValues[0]);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM