利用PHP编写一个简易的图形计算器
发布时间:2020-11-06 16:37:12
来源:亿速云
阅读:75
作者:Leah
利用PHP编写一个简易的图形计算器?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
主函数:index.php
图形计算器* {
margin: 0px;
padding: 0px;
}
#contains {
width: 500px;
margin: 20px auto;
background: #0C0;
text-align: center;
}
h2 {
width: 500px;
height: 60px;
}
a {
font-size: 20px;
text-decoration: none;
}
#footer {
width: 300px;
background: #fff;
margin: 0 auto;
padding: 5px 10px;
border-radius: 150px;
}
简易图形计算器
矩形 |
三角形|
ini_set("display_errors", "On"); //开启错误调试
//设置错误报告的级别,除了无关紧要的'注意',其他的报告都输出
error_reporting(E_ALL & ~E_NOTICE);
function __autoload($classname) { //魔术方法 自动加载类
include strtolower($classname).".class.php"; //将类名转化成小写
}
// include "shape.class.php";
// include "rect.class.php";
if (!empty($_GET['action'])) {
// echo "传送成功";
$classname = ucfirst($_GET['action']);
$shape = new $classname($_POST);
$shape->view($_POST);
if (isset($_POST['sub'])) {
echo "
";}
} else {
echo "请选择一个图形";
}
?>
先定义一个抽象类
abstract class Shape {
private $name;
private $error;
abstract function area();
abstract function zhou();
abstract function view($arr);
abstract function yan($arr);
}
?>
矩形类的编写
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 = "矩形";
$this->error = '';
}
function area() {
return $this->width * $this->height;
}
function zhou() {
return ($this->width+$this->height) * 2;
}
function view($arr) {
$form .= "
";$form .= "请输入".$arr['name']."的宽度:
";
$form .= "
";
$form .= "请输入".$arr['name']."的长度:
";
$form .= "
";
$form .= " ";
$form .= "";
$form .= "
";echo $form;
}
function yan($arr) {
$bz = true;
if ($arr['width']< 0) {
$this->error .= "宽度小于0;";
$bz = false;
} else {
if (!is_numeric($arr['width'])) {
$this->error .= "宽不是数字;";
$bz = false;
}
}
if ($arr['height']< 0) {
$this->error .= "宽度小于0;";
$bz = false;
} else {
if (!is_numeric($arr['height'])) {
$this->error .= "高不是数字;";
$bz = false;
}
}
return $bz;
}
}
?>
三角形类:
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 = "三角形";
$this->error = '';
}
function area() {
$p = ($this->bian1 + $this->bian2 + $this->bian3) / 2;
// p(p-a)(p-b)(p-c)
return sqrt($p*($p-$this->bian1)*($p-$this->bian2)*($p-$this->bian3));
}
function zhou() {
return $this->bian1+$this->bian2+$this->bian3;
}
function view($arr) {
$form .= "
";$form .= "请输入".$arr['name']."的第一条边:
";
$form .= "
";
$form .= "请输入".$arr['name']."的第二条边:
";
$form .= "
";
$form .= "请输入".$arr['name']."的第三条边:
";
$form .= "
";
$form .= " ";
$form .= "";
$form .= "
";echo $form;
}
function yan($arr) {
$bz = true;
if ($arr['bian1']< 0) {
$this->error .= "第一条边小于0;";
$bz = false;
} else {
if (!is_numeric($arr['bian1'])) {
$this->error .= "第一条边不是数字;";
$bz = false;
}
}
if ($arr['bian2']< 0) {
$this->error .= "第二条边小0;";
$bz = false;
} else {
if (!is_numeric($arr['bian2'])) {
$this->error .= "第二条边不是数字;";
$bz = false;
}
}
if ($arr['bian2']< 0) {
$this->error .= "第三条边小于0;";
$bz = false;
} else {
if (!is_numeric($arr['bian2'])) {
$this->error .= "第三条边不是数字;";
$bz = false;
}
}
if (($this->bian1+$this->bian2) < $this->bian3 ||($this->bian1+$this->bian3) < $this->bian2 ||($this->bian2+$this->bian3) < $this->bian1) {
$this->error .= "三条边不能构成三角形";
$bz = false;
}
return $bz;
}
}
?>
其他的类只要按照上面的格式改下就很好写出来了。
关于利用PHP编写一个简易的图形计算器问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。