存档:
index.php
form.class.php
1 action = $action; 7 $this->shape = isset($_GET["action"])?$_GET["action"]:"rect"; 8 } 9 10 function __toString(){11 $form='';16 return $form;17 }18 19 private function getRect(){20 $input = '请输入|矩形|的宽度和高度:
';21 $input .= '宽度:
';22 $input .= '高度:';23 return $input;24 }25 26 private function getTriangle(){27 $input = '请输入|三角形|的三条边:';28 $input .= '第一条边:
';29 $input .= '第二条边:';30 $input .= '第三条边:';31 return $input;32 }33 34 private function getCircle(){35 $input = '请输入|圆形|的半径:';36 $input .= '半径:
';37 return $input;38 }39 }40 ?>shape.class.php
1 shapeName.$message; 9 echo ''.$message.'必须为非负值的数字,并且不能为空';10 return false;11 }12 else{13 return true;14 }15 }16 }17 ?>
result.class.php
1 shape = new $_GET['action'](); 6 } 7 8 function __toString(){ 9 $result = $this->shape->shapeName.'的周长:'.round($this->shape->perimeter(),2).'';10 $result .= $this->shape->shapeName.'的面积:'.round($this->shape->area(),2).'';11 return $result;12 }13 }14 ?>
rect.class.php
1 shapeName = "矩形"; 7 if($this->validate($_POST["width"],"宽度") & $this->validate($_POST["height"],"高度")){ 8 $this->width = $_POST["width"]; 9 $this->height = $_POST["height"];10 }11 }12 13 function area(){14 return $this->width*$this->height;15 }16 17 function perimeter(){18 return 2*($this->width+$this->height);19 }20 }21 ?>
triangle.class.php
1 shapeName = "三角形"; 8 if($this->validate($_POST["side1"],"第一条边") & 9 $this->validate($_POST["side2"],"第二条边") & 10 $this->validate($_POST["side3"],"第三条边")){11 if($this->validateSum($_POST["side1"],$_POST["side2"],$_POST["side3"])){12 $this->side1 = $_POST["side1"];13 $this->side2 = $_POST["side2"];14 $this->side3 = $_POST["side3"];15 }16 else{17 echo '三角形的两边之和要大于第三边';18 } 19 }20 }21 22 function area(){23 $s = ($this->side1+$this->side2+$this->side3)/2;24 return sqrt($s*($s-$this->side1)*($s-$this->side2)*($s-$this->side3));25 }26 27 function perimeter(){28 return $this->side1+$this->side2+$this->side3;29 }30 31 private function validateSum($s1,$s2,$s3){32 if((($s1+$s2)>$s3) && (($s1+$s3)>$s2) && (($s2+$s3)>$s1)){33 return true;34 }35 else{36 return false;37 }38 }39 }40 ?>
circle.class.php
1 shapeName = "图形"; 6 if($this->validate($_POST['radius'],'半径')){ 7 $this->radius = $_POST['radius']; 8 } 9 }10 11 function area(){12 return pi()*$this->radius*$this->radius;13 }14 15 function perimeter(){16 return 2*pi()*$this->radius;17 }18 }19 ?>
结果如下: