龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

PHP实现的简单三角形、矩形周长面积计算器分享(2)

时间:2014-11-21 02:06来源:网络整理 作者:网络 点击:
分享到:
形状的抽象类: abstract class Shape{//形状的名称public $name;//形状的计算面积方法abstract function area();//形状的计算周长的方法abstract function zhou();//形状的图形表

形状的抽象类:

abstract class  Shape{
    //形状的名称
    public $name;
 
    //形状的计算面积方法
    abstract function area();
 
    //形状的计算周长的方法
    abstract function zhou();
 
    //形状的图形表单界面
    abstract function view();
    //形状的验证方法
    abstract function yan($arr);
 
}

三角形计算类文件:

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

矩形计算类文件:

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;
    }
 
}
精彩图集

赞助商链接