首页 技术 正文
技术 2022年11月6日
0 收藏 412 点赞 1,115 浏览 2506 个字

前面的话

  本文用面向对象的技术来实现一个简单的图形面积计算器

图形类

//rect.class.php
<?php
abstract class Shape{
public $name;
abstract function area();
abstract function view();
abstract function test($arr);
}
?>

主界面

//index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.box{
width: 800px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="box">
<h1>图形计算器</h1>
<div>
<a href="index.php?action=rect" rel="external nofollow" >矩形</a>
<a href="index.php?action=triangle" rel="external nofollow" >三角形</a>
</div>
</div>
<?php
error_reporting(E_ALL & ~E_NOTICE);
function __autoload($classname){
include strtolower($classname).".class.php";
}
if(!empty($_GET['action'])) {
$classname = ucfirst($_GET['action']);
$shape=new $classname($_POST);
$shape->view();
if(isset($_POST['dosubmit'])) {
if($shape->test($_POST)) {
echo $shape->name."的面积为:".$shape->area()."<br>";
}
}
}else{
echo "请选择一个要计算的图形!<br>";
}
?>
</body>
</html>

矩形类

//rect.class.php
<?php
class Rect extends Shape{
private $width;
private $height;
function __construct($arr=[]){
if(!empty($arr)){
$this->width = $arr['width'];
$this->height = $arr['height'];
}
$this->name = "矩形";
}
function area() {
return $this->width * $this->height;
}
function view() {
$form = '<form action="index.php?action=rect" method="post">';
$form .=$this->name.'的宽:<input name="width" value=""/><br>';
$form .=$this->name.'的高:<input name="height" value=""/><br>';
$form .='<input type="submit" name="dosubmit" value="计算"><br>';
$form .='</form>';
echo $form;
}
function test($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;
}
}
?>

三角形类

//triangle.class.php
<?php
class Triangle extends Shape{
private $b1;
private $b2;
private $b3;
function __construct($arr=[]){
if(!empty($arr)){
$this->b1 = $arr['b1'];
$this->b2 = $arr['b2'];
$this->b3 = $arr['b3'];
}
$this->name = "三角形";
}
function area() {
$p = ($this->b1 + $this->b2 + $this->b3)/2;
return sqrt($p*($p-$this->b1)*($p-$this->b2)*($p-$this->b3));
}
function view() {
$form = '<form action="index.php?action=triangle" method="post">';
$form .=$this->name.'第一个边的宽:<input name="b1" value=""/><br>';
$form .=$this->name.'第二个边的宽:<input name="b2" value=""/><br>';
$form .=$this->name.'第三个边的宽:<input name="b3" value=""/><br>';
$form .='<input type="submit" name="dosubmit" value="计算"><br>';
$form .='</form>';
echo $form;
}
function test($arr) {
$bg = true;
if($arr['b1'] < 0) {
echo "第一个边的宽不能小于0!<br>";
$bg = false;
}
if($arr['b2'] < 0) {
echo "第二个边的宽不能小于0!<br>";
$bg = false;
}
if($arr['b3'] < 0) {
echo "第三个边的宽不能小于0!<br>";
$bg = false;
}
if(($arr['b1'] + $arr['b2'] < $arr['b3'])||($arr['b1'] + $arr['b3'] < $arr['b2'])||($arr['b3'] + $arr['b2'] < $arr['b1'])){
echo '两边之和不能小于第三边<br>';
$bg = false;
}
return $bg;
}
}
?>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896