1

演算子 (加算、減算、乗算、除算)、再グループ化 (運ぶかどうか)、および列 (1 つまたは 2 つ) の 3 つのパラメーターに基づいてランダムな数学的問題を生成する必要があるサイトがあります。私はこれを行う方法について頭を悩ませようとしてきましたが、私が思いついたものには何らかの形で欠陥があります.

これが私が現在取り組んでいるものです:

function create_problem($operator, $regrouping, $columns){
    $top = rand(1,9);
    $bottom = rand(1,9);

    if($operator == "+"){
        if($columns == 1 && $regrouping === false){
            $result = array(
                'top' => $top,
                'bottom' => $bottom,
                'formula' => "$top.$operator.$bottom"
            );
        }
        if($columns == 2){
            if($regrouping === false){
                if($top+$bottom > 9){
                    $diff = ($top+$bottom)-10;
                    $top = $diff+rand(1, 3);    
                }
                $result = array(
                    'top' => $top,
                    'bottom' => $bottom,
                    'formula' => "$top.$operator.$bottom"
                );
            }else{
                if($top+$bottom < 10){
                    $top = rand(1, $bottom+1);
                }
            }
        }
    }
    return $result;
}

誰かがこれに対処した場合、または誰かが指針を持っている場合、私は最も感謝しています!

4

1 に答える 1

2

この関数は、最初に再グループ化をチェックし、共通列の数値の合計が 10 未満 (キャリーなしの場合) または 10 以上 (キャリーの場合) になるように乱数を生成します。

function create_problem($operator, $regrouping, $columns){
$x1 = '';
$x2 = '';
$y1 = '';
$y2 = '';
if ($regrouping){
    if ($columns == 2){
    $x1 = rand(1,9);
    $x2 = rand(0,9);
    $y1 = rand(9-$x1,9);
    $y2 = rand(10-$x2,9);
    } else {
    $x1 = rand(1,9);
    $y1 = rand(9-$x1,9);
    }
} else {
    if ($columns == 2){
    $x1 = rand(1,8);
    $x2 = rand(0,8);
    $y1 = rand(1,9-$x1);
    $y2 = rand(0,9-$x2);
    } else {
    $x1 = rand(1,8);
    $y1 = rand(1,9-$x1);
    }
}

return $x1.$x2.$operator.$y1.$y2;
}

元..

31 +54

..どこで 3=$x1 1=$x2 5=$y1 4=$y2

于 2012-12-09T06:37:23.510 に答える