【问题标题】:Dynamically Creating Mathematical Expressions动态创建数学表达式
【发布时间】:2012-12-09 06:22:07
【问题描述】:

我有一个网站需要根据 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;
}

如果有人处理过这个问题,或者如果有人有任何指示,我将不胜感激!

【问题讨论】:

    标签: php math dynamic


    【解决方案1】:

    此函数首先检查重组,并生成随机数,使公共列数的总和小于 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

    【讨论】:

    • 一个简单的测试运行看起来不错,但有两个小问题。 1) create_problem("-", false, 2); 返回68-3-5 的输出,而不是两列表达式。 2) create_problem("-", true, 2); 返回 67-40 不需要重新组合。否则看起来很棒!
    • 现在重试编辑。第一个错误是错字,第二个错误是逻辑问题。
    • -1,您似乎忘记解释这是什么以及它的工作原理。代码转储不是答案。
    • 很好用,贾斯汀。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    相关资源
    最近更新 更多