在php计算器中正确输出

时间:2019-02-09 06:42:35

标签: php calculator calc

我的PHP计算器需要帮助。

我当前的输出:

enter image description here

我想要的输出:

But I need that

所以我需要直列,输出正确的顺序等

我的代码:

function calc($operator, $num)
{
    if ($num < 0) {
        return 'Number must be more than zero';
    }
    $operatorsMap = ['+', '-', '*', '/'];
    if (!in_array($operator, $operatorsMap)) {
        return 'You have entered a bad operator';
    }
    for ($rows = 0; $rows <= $num; $rows++) {
        echo "\n|";
        for ($columns = 0; $columns <= $num; $columns++) {
            switch ($operator) {
                case '+':
                    echo round($columns + $rows, 2);
                    echo "|";
                    break;
                case '-':
                    echo round($columns - $rows, 2);
                    echo "|";
                    break;
                case '*':
                    echo round($columns * $rows, 2);
                    echo "|";
                    break;
                case '/':
                    echo round($columns / $rows, 2);
                    echo "|";
                    break;
            }
        }
    }   
}

0 个答案:

没有答案