7.10.5 PHP图形计算器主程序完成检验

7.11.1 与类有关的魔术常量和函数

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> 
        </center>

        <hr><br>

    <?php
            error_reporting(E_ALL & ~E_NOTICE);
    
            //设置自动加载这个程序需要的类文件
            function __autoload($classname){
                include strtolower($classname).".class.php";
            }
    
            //判断用户是否有选择单击一个形状链接
            if(!empty($_GET['action'])) {
                //第一步:创建形状的对象
                $classname = ucfirst($_GET['action']);

                $shape=new $classname($_POST);
                //第二步:调用形状的对象中的界面view()
                $shape -> view();

                //第三步:用户是否提交了对应图形界面的表单
                if(isset($_POST['dosubmit'])) {
                    //第四步:查看用户输出的数据是否正确, 失败则提示
                    if($shape->yan($_POST)) {
                        //计算图形的周长和面积
                        echo $shape->name."的周长为:".$shape->zhou()."<br>";
                        echo $shape->name."的面积为:".$shape->area()."<br>";
                    }
                }
                

            //如果用户没有单击链接, 则是默认访问这个主程序
            }else {
                echo "请选择一个要计算的图形!<br>";
            
            }

        ?>
    </body>
</html>

rect.class.php

<?php
/*
 * 这个类是一个矩形的类, 这个类要按形状的规范去实现
 *
 */
class Rect extends Shape {
    private $width;
    private $height;

    function __construct($arr=array()) {

        if(!empty($arr)) {
            $this->width = $arr['width'];
            $this->height = $arr['height'];
        }
        $this->name = "矩形";
    }
    
    function area() {
        return $this->width * $this->height;
    }

    function zhou() {
        return 2*($this->width + $this->height);
    }

    function view() {
        $form = '<form action="index.php?action=rect" method="post">';
        $form .= $this->name.'的宽:<input type="text" name="width" value="'.$_POST['width'].'" /><br>';
        $form .= $this->name.'的高:<input type="text" name="height" value="'.$_POST['height'].'" /><br>';
        $form .= '<input type="submit" name="dosubmit" value="计算"><br>';
        $form .='<form>';
        echo $form;
    }

    function yan($arr) {
        $bg = true;
        if($arr['width'] < 0) {
            echo $this->name."的宽不能小于0!<br>";
            $bg = false;    
        }

        if($arr['height'] < 0) {
            echo $this->name."的高度不能小于0!<br>";
            $bg = false;
        }

        return $bg;
    }

}

shape.class.php

<?php
/*
 * 这是一个形状的抽象类
 *
 * 定义子类必须实现的一些方法
 *
 *
 */
abstract class  Shape{
    //形状的名称
    public $name;

    //形状的计算面积方法
    abstract function area();

    //形状的计算周长的方法
    abstract function zhou();

    //形状的图形表单界面
    abstract function view();
    //形状的验证方法
    abstract function yan($arr);

}

triangle.class.php

<?php
/*
 * 这是按形状的抽象类写的一个三角形的计算方式
 *
 */

class Triangle extends Shape {
    private $bian1;
    private $bian2;
    private $bian3;

    function __construct($arr = array()) {
        if(!empty($arr)) {
            $this->bian1 = $arr['bian1'];
            $this->bian2 = $arr['bian2'];
            $this->bian3 = $arr['bian3'];

        }

        $this->name = "三角形";
    }

    function area() {
        $p =    ($this->bian1 + $this->bian2 + $this->bian3)/2;

        return sqrt($p*($p-$this->bian1)*($p-$this->bian2)*($p-$this->bian3));
    }

    function zhou() {
        return $this->bian1 + $this->bian2 + $this->bian3;
    }

    function view() {
        $form = '<form action="index.php?action=triangle" method="post">';
        $form .= $this->name.'第一个边:<input type="text" name="bian1" value="'.$_POST['bian1'].'" /><br>';
        $form .= $this->name.'第二个边:<input type="text" name="bian2" value="'.$_POST['bian2'].'" /><br>';
        $form .= $this->name.'第三个边:<input type="text" name="bian3" value="'.$_POST['bian3'].'" /><br>';
        $form .= '<input type="submit" name="dosubmit" value="计算"><br>';
        $form .='<form>';
        echo $form;
    }

    function yan($arr) {
        $bj = true;
        if($arr['bian1'] < 0) {
            echo "第一个边不能小于0!<br>";
            $bj = false;
        }

        if($arr['bian2'] < 0) {
            echo "第二个边不能小于0!<br>";
            $bj = false;
        }

        if($arr['bian3'] < 0) {
            echo "第三个边不能小于0!<br>";
            $bj = false;
        }


        if(($arr['bian1']+$arr['bian2'] < $arr['bian3']) || ($arr['bian1'] + $arr['bian3'] < $arr['bian2']) || ($arr['bian2']+$arr['bian3'] < $arr['bian1'])) {
            echo "两边之和必须大于第三个边";
            $bj = false;
        }

        return $bj; 
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容