1 <html>
2 <head>
3 <title>图形计算器开发</title>
4 <meta http-equiv="Content-type" content="text/html;charset=utf-8">
5 </head>
6
7 <body>
8 <center>
9 <h1>图形(周长&面积)计算器</h1>
10
11 <a href="index.php?action=rect">矩形</a>
12 <a href="index.php?action=triangle">三角形</a>
13 <a href="index.php?action=circle">圆形</a>
14 </center>
15 <?php
16 error_reporting(E_ALL & ~E_NOTICE);
17 function __autoload($className){
18 include strtolower($className).".class.php";
19 }
20 echo new Form("index.php");
21 if(isset($_POST["sub"])){
22 echo new Result();
23 }
24 ?>
25 </body>
26 </html>
\\form.class.php
1 <?php
2 class Form{
3 private $action;
4 private $shape;
5 function __construct($action=""){
6 $this->action = $action;
7 $this->shape = isset($_GET["action"])?$_GET["action"]:"rect";
8 }
9
10 function __toString(){
11 $form='<form action="'.$this->action.'?action='.$this->shape.'" method="post">';
12 $shape="get".ucfirst($this->shape);
13 $form .=$this->$shape();
14 $form .='<br><input type="submit" name="sub" value="计算"><br>';
15 $form .='</form>';
16 return $form;
17 }
18
19 private function getRect(){
20 $input = '<b>请输入|矩形|的宽度和高度:</b><p>';
21 $input .= '宽度:<input type="text" name="width" value="'.$_POST["width"].'"><br>';
22 $input .= '高度:<input type="text" name="height" value="'.$_POST["height"].'"><br>';
23 return $input;
24 }
25
26 private function getTriangle(){
27 $input = '<b>请输入|三角形|的三条边:</b><p>';
28 $input .= '第一条边:<input type="text" name="side1" value="'.$_POST["side1"].'"><br>';
29 $input .= '第二条边:<input type="text" name="side2" value="'.$_POST["side2"].'"><br>';
30 $input .= '第三条边:<input type="text" name="side3" value="'.$_POST["side3"].'"><br>';
31 return $input;
32 }
33
34 private function getCircle(){
35 $input = '<b>请输入|圆形|的半径:</b><p>';
36 $input .= '半径:<input type="text" name="radius" value="'.$_POST["radius"].'"><br>';
37 return $input;
38 }
39 }
40 ?>
\\shape.class.php
1 <?php
2 abstract class Shape{
3 public $shapeName;
4 abstract function area();
5 abstract function perimeter();
6 protected function validate($value,$message = '输入的值'){
7 if($value=="" || !is_numeric($value) || $value<0){
8 $message=$this->shapeName.$message;
9 echo '<font color="red">'.$message.'必须为非负值的数字,并且不能为空</font><br>';
10 return false;
11 }
12 else{
13 return true;
14 }
15 }
16 }
17 ?>
\\result.class.php
1 <?php
2 class Result{
3 private $shape = null;
4 function __construct(){
5 $this->shape = new $_GET['action']();
6 }
7
8 function __toString(){
9 $result = $this->shape->shapeName.'的周长:'.round($this->shape->perimeter(),2).'<br>';
10 $result .= $this->shape->shapeName.'的面积:'.round($this->shape->area(),2).'<br>';
11 return $result;
12 }
13 }
14 ?>
rect.class.php
1 <?php
2 class Rect extends Shape{
3 private $width = 0;
4 private $height = 0;
5 function __construct(){
6 $this->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 <?php
2 class Triangle extends Shape{
3 private $side1 = 0;
4 private $side2 = 0;
5 private $side3 = 0;
6 function __construct(){
7 $this->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 '<font color="red">三角形的两边之和要大于第三边</font><br>';
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 <?php
2 class Circle extends Shape{
3 private $radius = 0;
4 function __construct(){
5 $this->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 ?>
第20行的代码是什么意思?为什么传入index.php这个参数。还有form.class.php第7行的$_GET是哪来的,看了整个代码没看见哪里声明用get传递数据。
- 3 回答
- 0 关注
- 509 浏览
添加回答
举报
0/150
提交
取消