无限循环,无法找出原因

时间:2015-12-07 22:55:23

标签: php algorithm infinite-loop

请考虑以下问题:http://adventofcode.com/day/7

我有以下代码:

<?php

function log_not($intArg) {
    $strArr = str_split(str_pad(decbin($intArg), 16, "0", STR_PAD_LEFT));

    for ($i = 0; $i < 16; $i++) { $strArr[$i] = ($strArr[$i] == "0") ? "1" : "0"; }

    return (bindec(implode($strArr)));
}

function traverseTree($tree, $startNode) {
    sleep(1);
    echo ($startNode."\n");

    if (is_numeric($startNode)) {
        return (int)$startNode;
    } else {        
        $thisNode = $tree[$startNode];

        if (is_array($thisNode)) {
            print_r($thisNode);
            echo ("\n");

            switch ($thisNode[0]) {

                case "NOT": 
                    return log_not(traverseTree($tree, $thisNode[1])); 
                    break;

                case "AND":
                    return ((int)traverseTree($tree, $thisNode[1]) & (int)traverseTree($tree, $thisNode[2])); 
                    break;

                case "OR":
                    return ((int)traverseTree($tree, $thisNode[1]) | (int)traverseTree($tree, $thisNode[2])); 
                    break;

                case "RSHIFT":
                    return ((int)traverseTree($tree, $thisNode[1]) >> (int)($thisNode[2])); 
                    break;

                case "LSHIFT":
                    return ((int)traverseTree($tree, $thisNode[1]) << (int)($thisNode[2])); 
                    break;

                default:
                    break;
            }
        } else {
            echo ($thisNode."\n\n");
            if (is_numeric($thisNode)) {
                //echo ("Number\n");
                return $thisNode;
            } else {
                //echo ("String\n");
                return (int)traverseTree($tree, $tree[$startNode]);
            }
        }
    }
}

$logicTree = [];
$myfile = fopen("day7_input - kopia.txt", "r") or die("Unable to open file!");

while(!feof($myfile)) {
    $line = trim(fgets($myfile));
    $lineArr = explode(" ", $line);

    if (count($lineArr) == 3) {
        $logicTree[$lineArr[2]] = $lineArr[0];
    } elseif (count($lineArr) == 4) {
        $logicTree[$lineArr[3]] = [$lineArr[0], $lineArr[1]];
    } else {
        $logicTree[$lineArr[4]] = [$lineArr[1], $lineArr[0], $lineArr[2]];
    }
}

fclose($myfile);

//print_r($logicTree);

echo (traverseTree($logicTree, "a"));

?>

出于某种原因,我在traverseTree() - 函数中得到了一个无限循环。我不知道为什么,而且我已经多次重写了这个功能。逻辑按位运算的帮助函数是因为我认为普通运算符可能有问题。谁能明白为什么?

我的输入可以在http://pub.bluie.se/day7_input.txt

找到

$startnode是一个数字(或格式为数字的字符串),在这种情况下,is_numeric应根据文档(http://php.net/manual/en/function.is-numeric.php)评估该数字。如果是数字,则该函数应返回该数字。

如果它不是数字,则它是一个字符串,在其中的情况是$tree中的一个键。例如,如果$startnodex,则$tree的关键字可以包含以下示例值:

x => 123        should return 123
x => y          should call traverseTree($tree, "y")
x => ["NOT", "y"]   should call log_not(traverseTree($tree, "y"))
x => ["NOT", "1"]   should call log_not(traverseTree($tree, "1"))
x => ["AND", "y", "z"]  should call (traverseTree($tree, "y") & traverseTree($tree, "z"))
x => ["AND", "1", "z"]  should call (traverseTree($tree, "1") & traverseTree($tree, "z"))
x => ["AND", "y", "1"]  should call (traverseTree($tree, "y") & traverseTree($tree, "1"))
x => ["OR", "y", "z"]   should call (traverseTree($tree, "y") | traverseTree($tree, "z"))
x => ["OR", "1", "z"]   should call (traverseTree($tree, "1") | traverseTree($tree, "z"))
x => ["OR", "y", "1"]   should call (traverseTree($tree, "y") | traverseTree($tree, "1"))
x => ["LSHIFT", "y", 1] should call (traverseTree($tree, "y") << 1)
x => ["LSHIFT", "1", 1] should call (traverseTree($tree, "1") << 1)
x => ["RSHIFT", "y", 1] should call (traverseTree($tree, "y") >> 1)
x => ["RSHIFT", "1", 1] should call (traverseTree($tree, "1") >> 1)

0 个答案:

没有答案