利用PHP怎么對(duì)三角形和矩形的周長面積進(jìn)行計(jì)算-創(chuàng)新互聯(lián)

利用PHP怎么對(duì)三角形和矩形的周長面積進(jìn)行計(jì)算?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的淮安網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

運(yùn)用PHP面向?qū)ο蟮闹R(shí)設(shè)計(jì)一個(gè)圖形計(jì)算器,同時(shí)也運(yùn)用到了抽象類知識(shí),這個(gè)計(jì)算器可以計(jì)算三角形的周長和面積以及矩形的周長和面積。本圖形計(jì)算器有4個(gè)頁面:1.PHP圖形計(jì)算器主頁index.php;    2.形狀的抽象類shape.class.php;    3三角形計(jì)算類triangle.class.php;    4.矩形計(jì)算類rect.class.php。

PHP圖形計(jì)算器代碼點(diǎn)擊下載:   php圖形計(jì)算器.zip

代碼分別如下:

PHP圖形計(jì)算器主頁:

<html>
    <head>
        <title>簡單的圖形計(jì)算器</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    </head>
 
    <body>
        <center>
            <h2>簡單的圖形計(jì)算器</h2>
 
            <a href="index.php?action=rect">矩形</a> ||
            <a href="index.php?action=triangle">三角形</a> 
        </center>
 
        <hr><br>
 
    <?php
            error_reporting(E_ALL & ~E_NOTICE);
 
            //設(shè)置自動(dòng)加載這個(gè)程序需要的類文件
            function __autoload($classname){
                include strtolower($classname).".class.php";
            }
 
            //判斷用戶是否有選擇單擊一個(gè)形狀鏈接
            if(!empty($_GET['action'])) {
                //第一步:創(chuàng)建形狀的對(duì)象
                $classname = ucfirst($_GET['action']);
 
                $shape=new $classname($_POST);
                //第二步:調(diào)用形狀的對(duì)象中的界面view()
                $shape -> view();
 
                //第三步:用戶是否提交了對(duì)應(yīng)圖形界面的表單
                if(isset($_POST['dosubmit'])) {
                    //第四步:查看用戶輸出的數(shù)據(jù)是否正確, 失敗則提示
                    if($shape->yan($_POST)) {
                        //計(jì)算圖形的周長和面積
                        echo $shape->name."的周長為:".$shape->zhou()."<br>";
                        echo $shape->name."的面積為:".$shape->area()."<br>";
                    }
                }
 
            //如果用戶沒有單擊鏈接, 則是默認(rèn)訪問這個(gè)主程序
            }else {
                echo "請(qǐng)選擇一個(gè)要計(jì)算的圖形!<br>";
 
            }
 
        ?>
    </body>
</html>

形狀的抽象類:

abstract class  Shape{
    //形狀的名稱
    public $name;
 
    //形狀的計(jì)算面積方法
    abstract function area();
 
    //形狀的計(jì)算周長的方法
    abstract function zhou();
 
    //形狀的圖形表單界面
    abstract function view();
    //形狀的驗(yàn)證方法
    abstract function yan($arr);
 
}

三角形計(jì)算類文件:

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.'第一個(gè)邊:<input type="text" name="bian1" value="'.$_POST['bian1'].'" /><br>';
        $form .= $this->name.'第二個(gè)邊:<input type="text" name="bian2" value="'.$_POST['bian2'].'" /><br>';
        $form .= $this->name.'第三個(gè)邊:<input type="text" name="bian3" value="'.$_POST['bian3'].'" /><br>';
        $form .= '<input type="submit" name="dosubmit" value="計(jì)算"><br>';
        $form .='<form>';
        echo $form;
    }
 
    function yan($arr) {
        $bj = true;
        if($arr['bian1'] < 0) {
            echo "第一個(gè)邊不能小于0!<br>";
            $bj = false;
        }
 
        if($arr['bian2'] < 0) {
            echo "第二個(gè)邊不能小于0!<br>";
            $bj = false;
        }
 
        if($arr['bian3'] < 0) {
            echo "第三個(gè)邊不能小于0!<br>";
            $bj = false;
        }
 
        if(($arr['bian1']+$arr['bian2'] < $arr['bian3']) || ($arr['bian1'] + $arr['bian3'] < $arr['bian2']) || ($arr['bian2']+$arr['bian3'] < $arr['bian1'])) {
            echo "兩邊之和必須大于第三個(gè)邊";
            $bj = false;
        }
 
        return $bj; 
    }
}

矩形計(jì)算類文件:


class Rect extends Shape {
    private $width;
    private $height;
 
    function __construct($arr=array()) {
 
        if(!empty($arr)) {
            $this->width = $arr['width'];
            $this->height = $arr['height'];
        }
        $this->name = "矩形";
    }
 
    function area() {
        return $this->width * $this->height;
    }
 
    function zhou() {
        return 2*($this->width + $this->height);
    }
 
    function view() {
        $form = '<form action="index.php?action=rect" method="post">';
        $form .= $this->name.'的寬:<input type="text" name="width" value="'.$_POST['width'].'" /><br>';
        $form .= $this->name.'的高:<input type="text" name="height" value="'.$_POST['height'].'" /><br>';
        $form .= '<input type="submit" name="dosubmit" value="計(jì)算"><br>';
        $form .='<form>';
        echo $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;
    }
 
}

看完上述內(nèi)容,你們掌握利用PHP怎么對(duì)三角形和矩形的周長面積進(jìn)行計(jì)算的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

分享文章:利用PHP怎么對(duì)三角形和矩形的周長面積進(jìn)行計(jì)算-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://muchs.cn/article16/ddhjgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)網(wǎng)站設(shè)計(jì)公司、定制網(wǎng)站網(wǎng)站導(dǎo)航、微信小程序、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)