国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > PHP > 正文

php面向?qū)ο缶幊叹毩?xí):計(jì)算矩形、三角形、圓形的周長(zhǎng)和面積

2020-03-22 18:48:19
字體:
供稿:網(wǎng)友


剛剛學(xué)完phphtml' target='_blank'>面向?qū)ο?/u>的編程,參考著高洛峰老師的php教程學(xué)習(xí)了這個(gè)實(shí)例。

效果圖片:

三角形
矩形
圓形

以下是實(shí)現(xiàn)代碼:index.php
<html><head>    <title>圖形計(jì)算(使用面向?qū)ο箝_發(fā)技術(shù))</title>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>    <center>        <h1>圖形(周長(zhǎng)&面積) 計(jì)算器</h1>        <a href="index.php?action=rect">矩形</a>        <a href="index.php?action=triangle">三角形</a>        <a href="index.php?action=circle">圓形</a>        <hr>    </center>    <?php  //php代碼部分        error_reporting(E_ALL & ~E_NOTICE);//提示錯(cuò)誤的等級(jí)    // __autoload是php中的魔術(shù)方法,在用到類的時(shí)候自動(dòng)調(diào)用        function __autoload($className){            //自動(dòng)導(dǎo)入這個(gè)類            include strtolower($className).".class.php";        }        //輸出表單,form類中有魔術(shù)方法__toString,因此可以直接輸出類的對(duì)象引用,就是輸出對(duì)象返回的字符串        echo new Form();        if(isset($_POST["sub"])){            //輸出結(jié)果            echo new Result();//直接輸出對(duì)象的引用表示        }    ?></body></html>
form.class.php_這是表單類_
<?php    //根據(jù)index中提交的不同action的值返回不同的表單    class Form{        private $action;        private $shape;        //構(gòu)造方法        function __construct($action=""){            $this->action = $action;            $this->shape=isset($_REQUEST["action"])?$_REQUEST["action"]:"rect";        }        function __toString()        {            // TODO: Implement __toString() method.            $form='<form action="'.$this->action.'"  method="post">';            switch($this->shape){                case "rect":                    $form.=$this->getRect();                    break;                case "triangle":                    $form.=$this->getTriangle();                    break;                case "circle":                    $form.=$this->getCircle();                    break;                default:                    $form.='請(qǐng)選擇一個(gè)形狀<br>';            }            $form.='<input type="submit" name="sub" value="計(jì)算">';            $form.='</form>';            return $form;        }        private function getRect(){            $input='<b>請(qǐng)輸入 | 矩形 | 的寬度和高度:</b><p>';            $input.='寬度:<input type="text" name="width" value="'.$_POST['width'].'"><br>';            $input.='高度:<input type="text" name="height" value="'.$_POST['height'].'"><br>';            $input.='<input type="hidden" name="action" value="rect">';            return $input;        }        private function getTriangle(){            $input='<b>請(qǐng)輸入 | 三角形 | 的三條邊:</b><p>';            $input.='第一邊: <input type="text" name="side1" value="'.$_POST['side1'].'" ><br>';            $input.='第二邊: <input type="text" name="side2" value="'.$_POST['side2'].'" ><br>';            $input.='第三邊: <input type="text" name="side3" value="'.$_POST['side3'].'" ><br>';            $input.='<input type="hidden" name="action" value="triangle">';            return $input;        }        private function getCircle(){            $input='<b>請(qǐng)輸入 | 圓形 | 的半徑:</b><p>';            $input.='半徑: <input type="text" name="radius" value="'.$_POST['radius'].'" ><br>';            $input.='<input type="hidden" name="action" value="circle">';            return $input;        }    }/** * Created by PhpStorm. * User: user * Date: 2018/4/15 * Time: 16:26 * */
shape.class.php 這是一個(gè)抽象類,用來定義規(guī)范的
<?phpabstract class Shape {    public $shapeName;    //規(guī)范circle、triangle、rect中必須有area()、perimeter()方法    abstract function area();    abstract function perimeter();    public function setShapeName($shapeName)    {        $this->shapeName = $shapeName;        return $this;    }    //判斷輸入的數(shù)字是否為大于0的有效數(shù)字    protected function validate($value, $message="形狀"){        if($value == "" || !is_numeric($value) || $value < 0 ){            echo '<font color="red"> '.$message.' 必須為非負(fù)值的數(shù)字,并且不能為空 </font><br>';            return false;        } else {            return true;        }    }}/** * Created by PhpStorm. * User: user * Date: 2018/4/15 * Time: 16:42 */
circle.class.php_就是計(jì)算周長(zhǎng)和面積的公式了_
<?phpclass Circle extends Shape {    private $radius=0;    function __construct(){        $this->shapeName="圓形";        if($this->validate($_POST['radius'], '圓的半徑')){            $this->radius=$_POST["radius"];        }else{            exit;        }    }    function area(){        return pi()*$this->radius*$this->radius;    }    function perimeter(){        return 2*pi()*$this->radius;    }}/** * Created by PhpStorm. * User: user * Date: 2018/4/15 * Time: 17:06 */
rect.class.php
<?phpclass Rect extends Shape{    private $width=0;    private $height=0;    function __construct()    {        $this->shapeName="矩形";        if($this->validate($_POST["width"],'矩形的寬度') & $this->validate($_POST["height"],'矩形的高度'))        {            $this->width=$_POST["width"];            $this->height=$_POST["height"];        }        else{            exit;        }    }    function area(){        return $this->width*$this->height;    }    function perimeter()    {        return 2 * ($this->width + $this->height);    }}/** * Created by PhpStorm. * User: user * Date: 2018/4/15 * Time: 17:02 */
triangle.class.php
<?phpclass Triangle extends Shape{    private $side1=0;    private $side2=0;    private $side3=0;    function __construct(){        $this->shapeName="三角形";        if($this->validate($_POST['side1'], '三角形的第一個(gè)邊')){            $this->side1=$_POST["side1"];        }        if($this->validate($_POST['side2'], '三角形的第二個(gè)邊')){            $this->side2=$_POST["side2"];        }        if($this->validate($_POST['side3'], '三角形的第三個(gè)邊')){            $this->side3=$_POST["side3"];        }        if(!$this->validateSum()){            echo '<font color="red">三角形的兩邊之和必須大于第三邊</font>';            exit;        }    }    function area(){        $s=( $this->side1+$this->side2+$this->side3 )/2;        return sqrt( $s * ($s - $this->side1) * ($s - $this->side2) * ($s - $this->side3) );    }    function perimeter(){        return $this->side1+$this->side2+$this->side3;    }    private function validateSum()    {        $condition1 = ($this->side1 + $this->side2) > $this->side3;        $condition2 = ($this->side1 + $this->side3) > $this->side2;        $condition3 = ($this->side2 + $this->side3) > $this->side1;        if ($condition1 && $condition2 && $condition3) {            return true;        } else {            return false;        }    }}/** * Created by PhpStorm. * User: user * Date: 2018/4/15 * Time: 17:04 */
result.class.php_這里是返回計(jì)算結(jié)果類_
<?php    class Result{        private $shape;        function __construct()        {            switch($_POST['action']){                case 'rect':                    $this->shape=new Rect();                    break;                case 'triangle':                    $this->shape=new Triangle();                    break;                case 'circle':                    $this->shape=new Circle();                    break;                default:                    $this->shape=false;            }        }        /**         * @return string         */        function __toString()        {            // TODO: Implement __toString() method.            if($this->shape){                $result=$this->shape->shapeName.'的周長(zhǎng):'.$this->shape->perimeter().'<br>';                $result.=$this->shape->shapeName.'的面積:'.$this->shape->area().'<br>';                return $result;            }else{                return '沒有這個(gè)形狀';            }        }    }/** * Created by PhpStorm. * User: user * Date: 2018/4/15 * Time: 16:47 */

相關(guān)推薦:

PHP面向?qū)ο?靜態(tài)延遲綁定static::

以上就是php面向?qū)ο缶幊叹毩?xí):計(jì)算矩形、三角形、圓形的周長(zhǎng)和面積 的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注 其它相關(guān)文章!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 伊吾县| 丹寨县| 扎鲁特旗| 京山县| 庐江县| 平江县| 三穗县| 和平区| 遵义县| 阳春市| 建始县| 舒城县| 子长县| 新密市| 静海县| 克什克腾旗| 田林县| 岫岩| 闽侯县| 铜川市| 阿城市| 芒康县| 靖边县| 凯里市| 平谷区| 西安市| 开封县| 鄂尔多斯市| 繁昌县| 宜城市| 迭部县| 益阳市| 丰镇市| 定西市| 东兴市| 鄂托克前旗| 普宁市| 蒲江县| 丽江市| 县级市| 游戏|