PHP面向对象实例(图形计算器)

Posted on 2016-04-02 17:04  Y-HKL  阅读(586)  评论(0编辑  收藏  举报

效果:

index.php

<!DOCTYPE html>
<html>
<head>
    <title>图形计算(使用面向对象技术开发)</title>
    <meta http-equiv="content" 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_NOTUCE);
        //自动加载需要的类文件
        function __autoload($className){
            //strtolower()函数,把类名转化为小写
            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;

    //在PHP5中的构造方法
    function __construct($action=""){
        $this->action=$action;
        $this->shape=isset($_REQUEST["action"])?$_REQUEST["action"]:"rect";
    }

    //在直接输出对象引用的时候自动调用
    function __toString(){
        $form='<form action="'.$this->action.'" method="post">';
        //echo $this->shape;
        switch($this->shape){
            case "rect":
                //要加到表单里面,要返回字符串
                $form.=$this->getRect();
                break;
            case "triangle":
                $form.=$this->getTriangle();
                break;
            case "circle":
                $form.=$this->getCircle();
                break;
            default:
                $form.='请选择一个形状<br>';
        }
        $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="text" name="height" value="'.$_POST["height"].'"><br>';
        $input.='<input type="hidden" name="action" value="rect">';
        return $input;
    }
    //得到三角形的方法
    private function getTriangle(){
        $input='<b>请输入三角形的三条边:</b><p>';
        $input.='第一条边:<input type="text" name="side1" value="'.$_POST["side1"].'"><br>';
        $input.='第二条边:<input type="text" name="side2" value="'.$_POST["side2"].'"><br>';
        $input.='第三条边:<input type="text" 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.class.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;
        }
    }
}
?>

 

 

result.class.php

<?php
class Result{
    private $shape;

    function __construct(){
        switch ($_POST['action']) {
            case 'rect':
                $this->shape=new Rect();
                break;
            case 'triangle':
                $this->shape=new Triangle();
                break;
            case 'circle':
                $this->shape=new Circle();
                break;
            default:
                $this->shape=false;
                break;
        }
    }

    //在直接输出对象引用的时候自动调用
    function __toString(){
        if($this->shape){
            $result=$this->shape->shapeName."的周长:".$this->shape->perimeter().'<br>';
            $result.=$this->shape->shapeName."的面积:".$this->shape->area().'<br>';
            return $result;
        }else{
            return "没有这个形状<br>";
        }
    }
}
?>

 

 

 

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

    }
    //面积
    function area(){
        return $this->width*$this->height;
    }
    //周长
    function perimeter(){
        return 2*($this->width+$this->height);
    }
}
?>

 

 

Triangle.class.php

<?php
class Triangle extends Shape{
    private $side1=0;
    private $side2=0;
    private $side3=0;

    function __construct(){
        $this->shapeName="三角形";

        if($this->validate($_POST["side1"],'三角形的第一条边') & $this->validate($_POST["side2"],"三角形的第二条边") & $this->validate($_POST["side3"],"三角形的第三条边")){
            $this->side1=$_POST["side1"];
            $this->side2=$_POST["side2"];
            $this->side3=$_POST["side3"];
            if(!$this->validateSum()){
                echo '<font color="red">三角形的两边之和必须大于第三边!</font><br>';
                exit;
            }
        }else{
            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.class.php

<?php
class Circle extends Shape{
    private $radius=0;

    function __construct(){
        $this->shapeName="圆形";

        if($this->validate($_POST["radius"],'圆的半径')){
            $this->radius=$_POST["radius"];
        }else{
            exit;
        }
    }

    function area(){
        return pi()*$this->radius*$this->radius;
    }

    function perimeter(){
        return 2*pi()*$this->radius;
    }
}
?>