1.使用動態頁面實現圖形計算器,能夠計算給定圖形的周長和麪積
2.可使用接口或抽象類做爲規範,再寫各子類的多態
3.動態頁面以下:php
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> #cal{ text-align: center; } </style> </head> <body> <div id="cal"> <h1>圖形計算器</h1> <a href="save.php?action=rect">矩形</a>|| <a href="save.php?action=triangle">三角形</a>|| <a href="save.php?action=cycle">圓形</a> <hr> </div> <?php include "get.php"; if(!empty($_GET['action'])){ $p=ucfirst($_GET['action']); $s=new $p($_POST); $s->input(); if(!empty($_POST)){ if($s->identify($_POST)){ echo $s->name."的周長爲:".$s->getPerimeter()."<br>"; echo $s->name."的面積爲:".$s->getArea()."<br>"; } } }else{ echo "請選擇圖形!<br>"; } ?> </body> </html>
4.接口與各子類以下:html
<?php interface Calculate { function input(); function getPerimeter(); function getArea(); function identify($arr); }; class Triangle implements Calculate { private $line1; private $line2; private $line3; public $name; function __construct($arr){ if(!empty($arr)){ $this->line1=$arr['line1']; $this->line2=$arr['line2']; $this->line3=$arr['line3']; $this->name="三角形"; } } function input(){ if(!empty($_POST)){ $line1=$_POST['line1']; $line2=$_POST['line2']; $line3=$_POST['line3']; }else{ $line1=null; $line2=null; $line3=null; } $form="<form action='save.php?action=triangle' method='post'>"; $form.="<label for='line1'>三角形第一條邊爲:</label><input type='text' id='line1' name='line1' value='".$line1."'/><br>"; $form.="<label for='line2'>三角形第二條邊爲:</label><input type='text' id='line2' name='line2' value='".$line2."'/><br>"; $form.="<label for='line3'>三角形第三條邊爲:</label><input type='text' id='line3' name='line3' value='".$line3."'/><br>"; $form.="<button type='submit' name='doSubmit'>計算</button><br>"; $form.="</form>"; echo $form; } function getPerimeter(){ return $this->line1+$this->line2+$this->line3; } function getArea(){ $p=$this->getPerimeter()/2; return sqrt($p*($p-$this->line1)*($p-$this->line2)*($p-$this->line3)); } function identify($arr){ $line1=$arr['line1']; $line2=$arr['line2']; $line3=$arr['line3']; $identifier=true; if($line1<0){ echo "第一條邊小於0<br>"; $identifier=false; } if($line2<0){ echo "第二條邊小於0<br>"; $identifier=false; } if($line3<0){ echo "第三條邊小於0<br>"; $identifier=false; } if(($line1+$line2<$line3)||($line1+$line3<$line2)||($line2+$line3<$line1)){ $identifier=false; echo "兩邊之和小於第三邊<br>"; } return $identifier; } } class Cycle implements Calculate { private $radius; public $name; function __construct($arr){ if(!empty($arr)){ $this->radius=$arr['radius']; $this->name="圓形"; } } function input(){ $form="<form action='save.php?action=cycle' method='post'>"; $form.="<label for='radius'>圓形的半徑爲:</label><input type='text' id='radius' name='radius'/><br>"; $form.="<button type='submit' name='doSubmit'>計算</button><br>"; $form.="</form>"; echo $form; } function getPerimeter(){ return 2*pi()*$this->radius; } function getArea(){ return pi()*$this->radius*$this->radius; } function identify($arr){ $identifier=true; if($arr['radius']<0){ echo "半徑不能小於0!<br>"; $identifier=false; } return $identifier; } } class Rect implements Calculate { private $height; private $width; public $name; function __construct($arr){ if(!empty($arr)){ $this->width=$arr['width']; $this->height=$arr['height']; $this->name="矩形"; } } function input(){ $form="<form action='save.php?action=rect' method='post'>"; $form.="<label for='width'>矩形的長度:</label><input type='text' id='width' name='width'/><br>"; $form.="<label for='height'>矩形的寬度:</label><input type='text' id='height' name='height'/><br>"; $form.="<button type='submit' name='doSubmit'>計算</button><br>"; $form.="</form>"; echo $form; } function getPerimeter(){ return 2*($this->width+$this->height); } function getArea(){ return $this->width*$this->height; } function identify($arr){ $identifier=true; if($arr['width']<0){ echo "寬度不能小於0!<br>"; $identifier=false; } if($arr['height']<0){ echo "高度不能小於0!<br>"; $identifier=false; } return $identifier; } } ?>