php.图形计算器(面向对象)

index.php页面:

<html>

   <head>

      <title>图形计算器(面向对象)</title>

      <meta http-equiv="Content-Type" content="text/html;charset=utf-8">

   </head>

   <body>

        <center>

             <h1>图形(面积 && 周长)计算器)<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

               error_reporting(E_ALL & ~E_NOTICE);

               function _autoload($className)

               {

                       include strtolower($className).".class.php";

                     }

                echo new Form();

                if(isset($_POST["sub"])){

                echo new Result();

                  }

         ?>

  </body>

</html>


form.class.php页面:

<?php

     class Form{

             private $action;

             private $shape;

             function _construct($action="index.php"){

                     $this->action = $action;

                     $this->shape = isset($_REQUEST["action"]) ? $_REQUEST["action"]:"rect";

              }

             function _toString(){

                      $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.='请选择一个形状';
                       }
                       $form.='<input type="submit" name="sub" value="计算">';
                      $form.='</form>';
                      return $form;
     }
              private function getRect(){
                             $input='<b>请输入|矩形|的长和宽:</b><p>';
                             $input.='宽度:<input type="text" name="width" value="'.$_POST['width'].'"><br>';
                             $input.='高度:<input type="test" name="height" value="'.$_POST['height'].'"><br>';
                             $input.='<input type="hidden" name="action" value="rect">';
                             return $input;
}
              private function getTriangle(){
                            $input='<b>请输入|三角形|的三边:</b><p>';
                            $input='<b>请输入|三角形|的三边:</b><p>';
                            $input.='第一边:<input type="text" name="side1" value="'.$_POST['side1'].'"><br>';
                            $input.='第二边:<input type="test" name="side2" value="'.$_POST['side2'].'"><br>';
                            $input.='第三边:<input type="test" name="side3" value="'.$_POST['side3'].'"><br>';
                            $input.='<input type="hidden" name="action" value="triangle">';
                            return $input;
}
              private function getCircle(){
                           $input='<b>请输入|圆形|的半径:</b><p>';
                           $input.='半径:<input type="text" name="radius" value="'.$_POST['radius'].'"><br>';
                           $input.='<input type="hidden" name="action" value="circle">';
                           return $input;
}
}
?>
shape.php页面:

<?php
     abstract class shape{
            public $shapeName;
            abstract function area();
            abstract function perimeter();

             protected function validate($value,$message="形状"){
                      if($value == "" ||!is_numeric($value)||$value < 0){
                           echo '<font color="red">'.$message.'必须为非负值的数>字,并且不能为空</font><br>';
                           return false;
       }              else{
                           return true;
                          }
            }

     }

?>

Rect.php页面<?php

    class 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;}

                     $this->width=$_POST["width"];

                     $this->height=$_POST["height"];

             }

             function area(){

                     return $this->width*$this->height;

             }

             function perimeter(){

                     return 2*($this->width+$this->height);

            }
     }
?>
Triangle.php页面:
<?php
     class Triangle extends Shape{
             private $side1=0;
             private $side2=0;
             private $side3=0;

             function _construct(){
                  if($this->validate($_POST['side1'],'三角形的第一边')){
                        $this->side1=$_POST["side1"];
                  if($this->validate($_POST['side2'],'三角形的第一
边')){
                        $this->side2=$_POST["side2"];
                  if($this->validate($_POST['side3'],'三角形的第一
边')){
                        $this->side3=$_POST["side3"];

                  }
                  $this->side1=$_POST["side1"];
                  $this->side2=$_POST["side2"];
                  $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;}
             }
     }
?>
Circle.php界面:
<?php
     class Circle extends Shape{
             private $radius=0;

             function _construct(){
                    $this->shapeName="圆形";
                    if($this->validate($_POST['radius'],'圆的半径')){
                        $this->radius.$_POST["radius"];
                    }else{exit;}
                    $this->radius=$_POST["radius"];
             }
             function area(){
                     return pi()*$this->radius*$this->radius;
             }
             function perimeter(){
                     return 2*pi()*$this->radius;
             }
     }

?>

原文地址:https://www.cnblogs.com/xiaoyueer/p/2208090.html