PHP数学毕达哥拉斯

时间:2011-04-30 23:36:28

标签: php math pythagorean

好吧,我编写了这个毕达哥拉斯解算器类的东西,我想知道我可以改进它的方法,还是让它更有效率?

<?php
    $sides = array('1' => 'Hypotenuse',
        '2' => 'Adjacent',
        '3' => 'Opposite');

    function createSideDropdown($name) {
        global $sides;
        $option = "<select name='".$name."'>";
            if(!empty($sides)) {
                foreach($sides as $id => $sideDesc) {
                    $option .= "<option value='".$id."'>".$sideDesc."</option>";
                }
            } else {
                die("Error fetching sides!");
            }
        $option .= "</select>";
        echo $option;
    }

    try {
        if(!empty($_POST['submit'])) {
            if(empty($_POST['val1']) || empty($_POST['val2'])) {
                throw new Exception("Please enter an integer in both boxes.");
            }
            if(!is_numeric($_POST['val1']) || !is_numeric($_POST['val2'])) {
                throw new Exception("One of the numbers you entered is not a valid integer.");
            }

            $val1 = $_POST['val1'];
            $val2 = $_POST['val2'];
            $val1numtype = $_POST['val1type'];
            $val2numtype = $_POST['val2type'];
            $val1nametype = $sides[$val1numtype];
            $val2nametype = $sides[$val2numtype];

                if($val1numtype == $val2numtype) {
                    throw new Exception("The two sides of the triangle must be different");
                }

                if($val1nametype == "Hypotenuse" || $val2nametype == "hypotenuse") {
                    // work out a small side
                    $bignum = max($val1, $val2);
                    $smallnum = min($val1, $val2);
                    $sqTotal = ($bignum * $bignum) - ($smallnum * $smallnum);
                    $total = sqrt($sqTotal);
                    echo $bignum."&sup2; - ".$smallnum."&sup2; = ".$sqTotal."<br />
                    &radic;".$sqTotal." = ".$total.$_POST['mes'];
                } else {
                    // work out the hypotenuse
                    $sq1 = $val1 * $val1;
                    $sq2 = $val2 * $val2;
                    $sqTotal = $sq1 + $sq2;
                    $total = sqrt($sqTotal);
                    echo $val1."&sup2; + ".$val2."&sup2; = ".$sqTotal."<br />
                    &radic;".$sqTotal." = ".$total.$_POST['mes'];
                }       
            echo "<br /><br />"; // Seperate the working out from the input
        }
    } catch(Exception $e) {
        echo $e->getMessage()."<br/><br/>";
    }


?>
<form method='POST' action='index.php'>
Value 1: <input type='text' name='val1' /> 
<?php createSideDropdown("val1type"); ?>
<br /><br />

Value 2: <input type='text' name='val2' /> 
<?php createSideDropdown("val2type"); ?>
<br />
<select name="mes">
<option name="mm">mm</option>
<option name="cm">cm</option>
<option name="m">m</option>
<option name="cm">km</option>
</select>
<br /> 

<input type="submit" name="submit" />
</form>
?>

1 个答案:

答案 0 :(得分:1)

嗯,你当然可以做的一件事是:

HTML本身和PHP本身 - 将它分开。然后,我会继续看看其余部分。

此外,您可以使用JavaScript执行例外 - 我的意思是,使用JavaScript来解析文本字段并使用JavaScript编写错误。而不是总是提交表单。 - 你仍然应该解析PHP中的字段。

然后,从中创建一个类并对其进行适当的记录,例如

/**
 * This method does bla
 * @param Int a
 */

不要使用全局变量 - 可以使用类属性来完成。