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>||
<a href="index.php?action=circle">圆形</a>
</center>
<hr><br>
<?php
//设置自动加载这个程序所需要的类文件
error_reporting(@notice);
function __autoload($classname){
include strtolower($classname).".class.php";
}
//如果没有点击图形链接
if(!empty($_GET['action'])){
//1.创建一个形状对象;
$classname=ucfirst($_GET['action']);
$shape=new $classname($_POST);
//2.调用形状类中的view表单界面;
$shape->view();
//3.用户是否提交了对应图形界面的表单
if(isset($_POST['sub'])){
//4.查看用户输入的数据是否正确(在形状类中已经定义过)
if($shape->validate()){
//5.计算出图形的周长和面积
echo "{$shape->name}的周长为:".$shape->circum();
echo "<br>";
echo "{$shape->name}的面积为:".$shape->area();
}
}
}
//如果没有点击图形链接,则提示
else{
echo "您没有选择要输出的图形,请选择要一个要输出的图形!";
}
?>
</body>
</html>
2.<!-- 这是一个定义图形的抽象类,方便后续图形类文件的扩展,形成多态特性!文件名shape.class.php-->
<?php
//定义这个图形的形状所需要的属性和验证方法
abstract class Shape{
//图形的名称
public $name;
//计算图形周长的方法;
abstract function circum();
//计算图形面积的方法;
abstract function area();
//验证图形输入数值的方法;
abstract function validate();
//图形表单界面的方法;
abstract function view();
}
?>
<!--定义一个矩形的类,类文件名rect.class.php-->
<?php
class Rect extends shape{
//定义矩形的宽、高属性
private $width;
private $height;
//构造方法初始化矩形的属性,用数组作为参数接收矩形宽、高的数值
function __construct($arr=array()){
if(!empty($arr)){
$this->width=$_POST['width'];
$this->height=$_POST['height'];
}
$this->name="矩形";
}
//计算矩形周长的方法;
function circum(){
return 2*($this->width+$this->height);
}
//计算矩形面积的方法;
function area(){
return $this->width*$this->height;
}
//验证矩形输入数值的方法;
function validate(){
$bj=true;
if(!is_numeric($this->width)){
echo "输入不合法,矩形的宽度必须为数字!";
$bj=false;
}
if($this->width<=0){
echo "矩形的宽不能小于等于0";
$bj=false;
}
if(!is_numeric($this->height)){
echo "输入不合法,矩形的高度必须为数字!";
$bj=false;
}
if($this->height<=0){
echo "矩形的高不能小于等于0";
$bj=false;
}
return $bj;
}
//矩形表单界面的方法;
function view(){
$form="<form action='index.php?action=rect' method='post'><br>";
$form.='矩形的宽为:<input type="text" name="width" value="'.$_POST['width'].'">'."cm"."<br>";
$form.='矩形的高为:<input type="text" name="height" value="'.$_POST['height'].'">'."cm"."<br>";
$form.="<input type='submit' name='sub' view='计算'><br>";
$form.="</form>";
echo $form;
}
}
?>
<!--三角形的class类文件triangle.class.php-->
<?php
//定义这个三角形的形状所需要的属性和验证方法
class Triangle extends Shape{
//三角形的三边边长值
private $length1;
private $length2;
private $length3;
function __construct($arr=array()){
if(!empty($arr)){
$this->length1=$_POST['length1'];
$this->length2=$_POST['length2'];
$this->length3=$_POST['length3'];
}
$this->name="三角形";
}
//计算三角形周长的方法;
function circum(){
return $this->length1+$this->length2+$this->length3;
}
//计算三角形面积的方法;
function area(){
$p=($this->length1+$this->length2+$this->length3)/2;
$area=sqrt($p*($p-$this->length1)*($p-$this->length2)*($p-$this->length3));
return $area;
}
//验证三角形输入数值的方法;
function validate(){
$bj=true;
if(!is_numeric($this->length1)){
echo "第一条边长输入不合法,必须为数字!!<br>";
$bj=false;
}
if(is_numeric($this->length1)&&($this->length1<=0)){
echo "三角形的边长不能小于等于0!<br>";
$bj=false;
}
if(!is_numeric($this->length2)){
echo "第二条边长输入不合法,必须为数字!!<br>";
$bj=false;
}
if(is_numeric($this->length2)&&($this->length2<=0)){
echo "三角形的边长不能小于等于0!<br>";
$bj=false;
}
if(!is_numeric($this->length3)){
echo "第三条边长输入不合法,必须为数字!!<br>";
$bj=false;
}
if(is_numeric($this->length3)&&($this->length3<=0)){
echo "三角形的边长不能小于等于0!<br>";
$bj=false;
}
if($this->length1>0&&$this->length2>0&&$this->length3>0){
if(($this->length1+$this->length2<=$this->length3)||($this->length1+$this->length3<=$this->length2)
||($this->length2+$this->length3<=$this->length1)){
echo "您所输入的不是三角形,三角形的两边之和必须大于第三边!<br>";
$bj=false;
}
}
return $bj;
}
//三角形表单界面的方法;
function view(){
$form='<form action="index.php?action=triangle" method="post">';
$form.='三角形的第一条边为:<input type="text" name="length1" value="'.$_POST['length1'].'">'.'cm'.'<br>';
$form.='三角形的第二条边为:<input type="text" name="length2" value="'.$_POST['length2'].'">'.'cm'.'<br>';
$form.='三角形的第三条边为:<input type="text" name="length3" value="'.$_POST['length3'].'">'.'cm'.'<br>';
$form.='<input type="submit" name="sub" value="计算" ><br>';
echo $form;
}
}
?>
<!--圆形的类circle.class.php-->
<?php
class Circle extends Shape{
private $radius;
const PI=3.14;
function __construct($arr=array()){
if(!empty($arr)){
$this->radius=$_POST['radius'];
}
$this->name="圆形";
}
//计算圆形周长的方法;
function circum(){
//PI为常量,方法中调用常量的值必须通过类来调用,此处使用self来代替;
return 2*(self::PI)*$this->radius;
}
//计算圆形面积的方法;
function area(){
return (self::PI)*$this->radius*$this->radius;
}
//验证圆形输入数值的方法;
function validate(){
$bj=true;
if(!is_numeric($this->radius)){
echo "您的输入不合法,必须为数字!";
$bj=false;
}
if(is_numeric($this->radius)&&($this->radius<=0)){
echo "圆的半径不能小于等于0!";
$bj=false;
}
return $bj;
}
//图形表单界面的方法;
function view(){
$form='<form action="index.php?action=circle" method="post">';
$form.='输入圆的半径为:<input type="text" name="radius">'.'cm'.'<br>';
$form.='<input type="submit" name="sub" value="计算"><br>';
echo $form;
}
}
?>
原文链接:https://blog.csdn.net/living_ren/article/details/75206245
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~