7.10.4 PHP圖形計算器主程序的步驟及代

7.10.4 PHP圖形計算器主程序的步驟及代

index.php

<html>
    <head>
        <title>簡單的圖形計算器</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    </head>

    <body>
        <center>
            <h1>簡單的圖形計算器</h1>

            <a href="index.php?action=rect">矩形</a> ||
            <a href="index.php?action=triangle">三角形</a> 
        </center>

        <hr><br>

        <?php
            //判斷用戶是否有選擇單擊一個形狀鏈接
            if(!empty($_GET['action'])) {

                echo $_GET['action'];
            //如果用戶沒有單擊鏈接, 則是默認(rèn)訪問這個主程序
            }else {
                echo "請選擇一個要計算的圖形!<br>";
            
            }

        ?>
    </body>
</html>

rect.class.php

<?php
/*
 * 這個類是一個矩形的類, 這個類要按形狀的規(guī)范去實現(xiàn)
 *
 */
class Rect extends Shape {
    private $width;
    private $height;

    function __construct($arr) {

        $this->width = $arr['width'];
        $this->height = $arr['height'];
        $this->name = $arr['name'];
    }
    
    function area() {
        return $this->width * $this->height;
    }

    function zhou() {
        return 2*($this->width + $this->height);
    }

    function view() {
        $form = '<form action="index.php" method="post">';
        $form .= $this->name.'的寬:<input type="text" name="width" value="" /><br>';
        $form .= $this->name.'的高:<input type="text" name="height" value="" /><br>';
        $form .= '<input type="submit" name="dosubmit" value="計算"><br>';
        $form .='<form>';
    }

    function yan($arr) {
        $bg = true;
        if($arr['width'] < 0) {
            echo $this->name."的寬不能為0!<br>";
            $bg = false;    
        }

        if($arr['height'] < 0) {
            echo $this->name."的高度不能小于0!<br>";
            $bg = false;
        }

        return $bg;
    }

}

shape.class.php

<?php
/*
 * 這是一個形狀的抽象類
 *
 * 定義子類必須實現(xiàn)的一些方法
 *
 *
 */
abstract class  {
    //形狀的名稱
    protected $name;

    //形狀的計算面積方法
    abstract function area();

    //形狀的計算周長的方法
    abstract function zhou();

    //形狀的圖形表單界面
    abstract function view();
    //形狀的驗證方法
    abstract function yan($arr);

}

triangle.class.php

<?php
/*
 * 這是按形狀的抽象類寫的一個三角形的計算方式
 *
 */

class Triangle extends Shape {
    private $bian1;
    private $bian2;
    private $bian3;

    function __construct($arr = array()) {
        if(!empty($arr)) {
            $this->bian1 = $arr['bian1'];
            $this->bian2 = $arr['bian2'];
            $this->bian3 = $arr['bian3'];

        }

        $this->name = "三角形";
    }

    function area() {
        $p =    ($this->bian1 + $this->bian2 + $this->bian3)/2;

        return sqrt($p*($p-$this->bian1)*($p-$this->bian2)*($p-$this->bian3));
    }

    function zhou() {
        return $this->bian1 + $this->bian2 + $this->bian3;
    }

    function view() {
        $form = '<form action="index.php?action=triangle" method="post">';
        $form .= $this->name.'第一個邊:<input type="text" name="bian1" value="'.$_POST['bian1'].'" /><br>';
        $form .= $this->name.'第二個邊:<input type="text" name="bian2" value="'.$_POST['bian2'].'" /><br>';
        $form .= $this->name.'第三個邊:<input type="text" name="bian3" value="'.$_POST['bian3'].'" /><br>';
        $form .= '<input type="submit" name="dosubmit" value="計算"><br>';
        $form .='<form>';
        echo $form;
    }

    function yan($arr) {
        $bj = true;
        if($arr['bian1'] < 0) {
            echo "第一個邊不能小于0!<br>";
            $bj = false;
        }

        if($arr['bian2'] < 0) {
            echo "第二個邊不能小于0!<br>";
            $bj = false;
        }

        if($arr['bian3'] < 0) {
            echo "第三個邊不能小于0!<br>";
            $bj = false;
        }


        if(($arr['bian1']+$arr['bian2'] < $arr['bian3']) || ($arr['bian1'] + $arr['bian3'] < $arr['bian2']) || ($arr['bian2']+$arr['bian3'] < $arr['bian1'])) {
            echo "兩邊之和必須大于第三個邊";
            $bj = false;
        }

        return $bj; 
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容