C

时间:2018-11-29 10:15:36

标签: javascript c algorithm

我正在尝试做的事情:

我看到了this算法,它是用javascript编写的,我正在尝试用C语言做Packing Blocks into a Fixed Rectangle。 在下面显示的代码中,我从.txt中读取数据。这部分没有问题,我只是对struct Blocks进行了指针排列,并对其进行了排序。 之后,我就像文章中的代码一样执行相同的逻辑,这就是发生错误的地方。

我的代码:

#include "stdio.h"
#include "stdlib.h"

typedef struct Block
{
    struct Node* fit;
    int width;
    int height;
    int x;
    int y;
    int id;
} Block;

typedef struct Node {
    struct Node* down;
    struct Node* right;
    int used;   
    int width;
    int height;
    int x;
    int y;
} Node;

Node *findNode(Node *root, int w, int h);
Node *splitNode(Node **root, int w, int h);

int main()
{
    FILE *file;
    char line[80];
    Block **blocks; 
    int totalBoards, boardWidth, boardHeight, totalBlocks;
    int i = 1, j;

    Node *root = malloc(sizeof(Node));
    root->x = 0;
    root->y = 0;
    root->used = 0;
    root->id = 0;

    fopen_s(&file, "blocks.txt", "r");

    // Reading the file
    while (fgets(line, 80, file)) {
        if (i == 1) {
            sscanf_s(line, "%d\n", &totalBoards);
        } else if (i == 2) {
            sscanf_s(line, "%d\n", &boardWidth);
            root->width = boardWidth;
        } else if (i == 3) {
            sscanf_s(line, "%d\n", &boardHeight);
            root->height = boardHeight;
        } else if (i == 4) {
            sscanf_s(line, "%d\n", &totalBlocks);
            blocks = malloc(totalBlocks * sizeof(Block *));

        } else {            
            int w, h;
            blocks[i - 5] = malloc(sizeof(Block));
            sscanf_s(line, "%d %d", &w, &h);
            blocks[i - 5]->width = w;
            blocks[i - 5]->height = h;
            blocks[i - 5]->id = i - 5;
        }

        i++;
    }

    //Bubble sort
    for (i = 0; i < totalBlocks; i++) {

        for (j = 0; j < totalBlocks - i - 1; j++)

            if (blocks[j]->height < blocks[j + 1]->height) {                
                Block *b = blocks[j];
                blocks[j] = blocks[j + 1];
                blocks[j + 1] = b;
            }
    }

    // THE IMPORTANT PART
    // The logic used by the algorithm
    // fit function
    for (i = 0; i < totalBlocks; i++) {
        Block *block = blocks[i];   
        Node *node;
        if (node = findNode(root, block->width, block->height)) {
            block->fit = splitNode(&node, block->width, block->height);
        }
    }

    //Print the blocks
    for (i = 0; i < totalBlocks; i++) {
        Block *block = blocks[i];
        if (block->fit) {           
            printf("x %d y %d\n", blocks[i]->fit->x, blocks[i]->fit->y);
        }
    }

    return 0;
}

Node *findNode(Node *root, int w, int h) {
    printf("%d", root->id);
    if (root->used == 1) {
        //Error Here
        return findNode(root->down, w, h) || findNode(root->right, w, h);
    }
    else if ((w <= root->width) && (h <= root->height)) {
        return root;
    }
    else {
        return NULL;
    }
}

Node *splitNode(Node **root, int w, int h) {

    (*root)->used = 1;
    (*root)->down = malloc(sizeof(Node));
    (*root)->down->right = malloc(sizeof(Node));
    (*root)->down->down = malloc(sizeof(Node));
    (*root)->down->x = (*root)->x;
    (*root)->down->y = (*root)->y + h;
    (*root)->down->width = (*root)->width;
    (*root)->down->height = (*root)->height - h;
    (*root)->down->used = 0;
    (*root)->down->id = idCount;
    idCount++;


    (*root)->right = malloc(sizeof(Node));
    (*root)->right->right = malloc(sizeof(Node));
    (*root)->right->down = malloc(sizeof(Node));
    (*root)->right->x = (*root)->x + w;
    (*root)->right->y = (*root)->y;
    (*root)->right->width = (*root)->width - w;
    (*root)->right->height = (*root)->height;
    (*root)->right->used = 0;
    (*root)->right->id = idCount;
    idCount++;

    return *root;
}

错误:

在这一部分中,从Node返回的findNode出了错

if (node = findNode(root, block->width, block->height)) {
    block->fit = splitNode(&node, block->width, block->height);
}

返回时
return findNode(root->down, w, h) || findNode(root->right, w, h);

因为我在node中使用变量splitNode

block->fit = splitNode(&node, block->width, block->height);

node的所有属性均为NULL,这会导致错误。

问题:

在此article的代码中,它返回了以下内容

return this.findNode(root.right, w, h) || this.findNode(root.down, w, h);

在C语言中我将返回

return findNode(root->right, w, h) || findNode(root->down, w, h);

我认为错误在这里。

解决方案?

我认为return ___ || ___在两种语言中的表现都不一样。所以我有两个问题:

  1. return ___ || ___在两种语言中都使用相同的语言?如果不是,则有什么区别,以及javascript中该代码的C代码等效项是什么?
  2. 我的代码有什么问题?为什么会发生该错误,而不像javascript代码那样返回正确的节点?

1 个答案:

答案 0 :(得分:2)

您认为||在JavaScript和C中具有不同的语义是正确的。

在JavaScript中,如果a || b是“真”,则a返回a,如果ba,则返回falsy

在C中,a || b是布尔表达式。它将始终返回truefalse而不是原始表达式。

例如,5 || 0在JavaScript中是5,而在C语言中是1

用{p>代替return findNode(root->right, w, h) || findNode(root->down, w, h);

Node *answer = findNode(root->right, w, h);
if (answer) return answer;
else return findNode(root->down, w, h);

应该工作。