1.使用动态页面实现图形计算器,可以计算给定图形的周长和面积
2.可以使用接口或抽象类作为规范,再写各子类的多态
3.动态页面如下:
<!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.接口与各子类如下:
<?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;
}
}
?>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。