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

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

index.php


    
        简单的图形计算器
        
    

    
        

简单的图形计算器

矩形 || 三角形


view(); //第三步:用户是否提交了对应图形界面的表单 if(isset($_POST['dosubmit'])) { //第四步:查看用户输出的数据是否正确, 失败则提示 if($shape->yan($_POST)) { //计算图形的周长和面积 echo $shape->name."的周长为:".$shape->zhou()."
"; echo $shape->name."的面积为:".$shape->area()."
"; } } //如果用户没有单击链接, 则是默认访问这个主程序 }else { echo "请选择一个要计算的图形!
"; } ?>

rect.class.php

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 .= $this->name.'的宽:
'; $form .= $this->name.'的高:
'; $form .= '
'; $form .=''; echo $form; } function yan($arr) { $bg = true; if($arr['width'] < 0) { echo $this->name."的宽不能小于0!
"; $bg = false; } if($arr['height'] < 0) { echo $this->name."的高度不能小于0!
"; $bg = false; } return $bg; } }

shape.class.php

triangle.class.php

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 .= $this->name.'第一个边:
'; $form .= $this->name.'第二个边:
'; $form .= $this->name.'第三个边:
'; $form .= '
'; $form .=''; echo $form; } function yan($arr) { $bj = true; if($arr['bian1'] < 0) { echo "第一个边不能小于0!
"; $bj = false; } if($arr['bian2'] < 0) { echo "第二个边不能小于0!
"; $bj = false; } if($arr['bian3'] < 0) { echo "第三个边不能小于0!
"; $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; } }

你可能感兴趣的:(7.10.5 PHP图形计算器主程序完成检验)