7.10.3 PHP图形计算器主程序的实现

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

        <hr><br>

        <?php
            //判断用户是否有选择单击一个形状链接
            if(!empty($_GET['action'])) {

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

        ?>
    </body>
</html>

rect.class.php

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

    function __construct($arr) {

        $this->width = $arr['width'];
        $this->height = $arr['height'];
        $this->name = $arr['name'];
    }
    
    function area() {
        return $this->width * $this->height;
    }

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

    function view() {
        $form = '<form action="index.php" method="post">';
        $form .= $this->name.'的宽:<input type="text" name="width" value="" /><br>';
        $form .= $this->name.'的高:<input type="text" name="height" value="" /><br>';
        $form .= '<input type="submit" name="dosubmit" value="计算"><br>';
        $form .='<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  {
    //形状的名称
    protected $name;

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

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

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

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容