PHP面向对象实例(图形计算器)
效果:
index.php
<!DOCTYPE html> <html> <head> <title>图形计算(使用面向对象技术开发)</title> <meta http-equiv="content" 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> <hr> <!--创建一条水平分隔线--> </center> <?php //错误报告处理 //error_reporting(E_ALL & ~E_NOTUCE); //自动加载需要的类文件 function __autoload($className){ //strtolower()函数,把类名转化为小写 include strtolower($className).".class.php"; } echo new Form(); if(isset($_POST["sub"])){ echo new Result(); } ?> </body> </html>
form.class.php
<?php class Form{ private $action; private $shape; //在PHP5中的构造方法 function __construct($action=""){ $this->action=$action; $this->shape=isset($_REQUEST["action"])?$_REQUEST["action"]:"rect"; } //在直接输出对象引用的时候自动调用 function __toString(){ $form='<form action="'.$this->action.'" method="post">'; //echo $this->shape; switch($this->shape){ case "rect": //要加到表单里面,要返回字符串 $form.=$this->getRect(); break; case "triangle": $form.=$this->getTriangle(); break; case "circle": $form.=$this->getCircle(); break; default: $form.='请选择一个形状<br>'; } $form.='<input type="submit" name="sub" value="计算">'; $form.='</form>'; return $form; } //得到矩形的方法 private function getRect(){ $input='<b>请输入矩形的宽度和高度:</b><p>'; $input.='宽度:<input type="text" name="width" value="'.$_POST["width"].'"><br>'; $input.='高度:<input type="text" name="height" value="'.$_POST["height"].'"><br>'; $input.='<input type="hidden" name="action" value="rect">'; return $input; } //得到三角形的方法 private function getTriangle(){ $input='<b>请输入三角形的三条边:</b><p>'; $input.='第一条边:<input type="text" name="side1" value="'.$_POST["side1"].'"><br>'; $input.='第二条边:<input type="text" name="side2" value="'.$_POST["side2"].'"><br>'; $input.='第三条边:<input type="text" name="side3" value="'.$_POST["side3"].'"><br>'; $input.='<input type="hidden" name="action" value="triangle">'; return $input; } //得到圆形的方法 private function getCircle(){ $input='<b>请输入圆形的半径:</b><p>'; $input.='半径:<input type="text" name="radius" value="'.$POST["radius"].'"><br>'; $input.='<input type="hidden" name="action" value="circle">'; return $input; } } ?>
shape.class.php
<?php abstract class Shape{ public $shapeName; abstract function area(); abstract function perimeter(); //验证 protected function validate($value,$message="形状"){ if($value==""||!is_numeric($value)||$value<0){ echo '<font color="red">'.$message.'必须为非负值的数字,并且不能为空!</font><br>'; return false; }else{ return true; } } } ?>
result.class.php
<?php class Result{ private $shape; function __construct(){ switch ($_POST['action']) { case 'rect': $this->shape=new Rect(); break; case 'triangle': $this->shape=new Triangle(); break; case 'circle': $this->shape=new Circle(); break; default: $this->shape=false; break; } } //在直接输出对象引用的时候自动调用 function __toString(){ if($this->shape){ $result=$this->shape->shapeName."的周长:".$this->shape->perimeter().'<br>'; $result.=$this->shape->shapeName."的面积:".$this->shape->area().'<br>'; return $result; }else{ return "没有这个形状<br>"; } } } ?>
Rect.class.php
<?php class Rect extends Shape{ private $width=0; private $height=0; function __construct(){ $this->shapeName="矩形"; if($this->validate($_POST["width"],'矩形的宽') & $this->validate($_POST["height"],"矩形的长")){ $this->width=$_POST["width"]; $this->height=$_POST["height"]; }else{ exit; } } //面积 function area(){ return $this->width*$this->height; } //周长 function perimeter(){ return 2*($this->width+$this->height); } } ?>
Triangle.class.php
<?php class Triangle extends Shape{ private $side1=0; private $side2=0; private $side3=0; function __construct(){ $this->shapeName="三角形"; if($this->validate($_POST["side1"],'三角形的第一条边') & $this->validate($_POST["side2"],"三角形的第二条边") & $this->validate($_POST["side3"],"三角形的第三条边")){ $this->side1=$_POST["side1"]; $this->side2=$_POST["side2"]; $this->side3=$_POST["side3"]; if(!$this->validateSum()){ echo '<font color="red">三角形的两边之和必须大于第三边!</font><br>'; exit; } }else{ exit; } } //海伦公式 function area(){ $s=($this->side1+$this->side2+$this->side3)/2; return sqrt($s*($s-$this->side1)*($s-$this->side2)*($s-$this->side3)); } function perimeter(){ return $this->side1+$this->side2+$this->side3; } //验证两边之和大于第三边 private function validateSum(){ $condition1=($this->side1 + $this->side2)> $this->side3; $condition2=($this->side1 + $this->side3)>$this->side2; $condition3=($this->side2 + $this->side3)>$this->side1; if($condition1 & $condition2 & $condition3){ return true; }else{ return false; } } } ?>
Circle.class.php
<?php class Circle extends Shape{ private $radius=0; function __construct(){ $this->shapeName="圆形"; if($this->validate($_POST["radius"],'圆的半径')){ $this->radius=$_POST["radius"]; }else{ exit; } } function area(){ return pi()*$this->radius*$this->radius; } function perimeter(){ return 2*pi()*$this->radius; } } ?>
PHP面向对象实例(图形计算器)的更多相关文章
- php:兄弟连之面向对象版图形计算器1
曾经看细说PHP的时候就想做这个,可是一直没什么时间,这次总算忙里偷闲搞了代码量比較多的project. 首先,文档结构,都在一个文件夹下就好了,我的就例如以下. 一開始,进入index.php文件. ...
- PHP学习笔记06——面向对象版图形计算器
index.php 用于显示页面 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "h ...
- php:兄弟连之面向对象版图形计算器2
上篇说到通过result.class.php来分流,因为三个类都继承了shape这个类,让我们来看一下,面向对象中的继承. shape.class.shape文件 <?php abstract ...
- PHP.11-PHP实例(二)-面向对象实例(图形计算器)
面向对象实例(图形计算器) [PHP语法详解] 1.实现外观 #不同的动作,输出不同的表单 ###关于PHP中,无法使用localhost访问.php文件[http://www.360doc.com/ ...
- PHP图形计算器(计算三角形矩形周长面积)
运用PHP面向对象的知识设计一个图形计算器,同时也运用到了抽象类知识,这个计算器可以计算三角形的周长和面积以及矩形的周长和面积.本图形计算器有4个页面:1.PHP图形计算器主页index.php; ...
- php实现图形计算器
存档: index.php <html> <head> <title>图形计算器开发</title> <meta http-equiv=" ...
- [图形计算器]Desmos
一.图形计算器 var elt = document.getElementById('calculator'); var calculator = Desmos.GraphingCalculator( ...
- 图形计算器(geogebra[5.0.278.0])使用QQ浏览器打开下载
点击这里下载Geogebra图形计算器
- php:的图形计算器的面向对象的版本武器2
通过自带部分result.class.php分流,由于这三个类继承shape这个类,让我们来看看,面向对象的继承. shape.class.shape档 <?php abstract class ...
随机推荐
- spring设置全局异常处理器
1.spring设置全局异常,它的原理是向上捕获 spring.xml配置 <!--自定义全局异常处理器--> <bean id="globalExceptionResol ...
- JavaScipt 数据交互
标准的w3c直接提供了XMLHttpRequest方法,我们主要站在设计的角度来理解,如何设计出低耦合高内聚的代码jquery对Ajax的处理主要体现在对浏览器兼容,数据的处理过滤以及各种事件的封装上 ...
- ABAP面试问题及侧重点
ABAP面试 1.简单的Report包括哪些东西 2.Dialog 逻辑流以及相应的处理内容 3.用过的几种增强方式:怎么找增强 4.接口和函数的使用,一般遇到自己不会的函数怎么处理 5.关联查询:I ...
- 查看APK中MD5签名的方法
(需下载jdk) 1. 先将apk文件重命名为zip文件 2. 解压zip,其中的META-INF/CERT.RSA文件即MD5签名文件 3. cmd下打开黑窗口,敲入如下命令: keytool -p ...
- linux php 安装 memcache 扩展
1. memcached依赖于libevent,需要先安装libevent. tar zxvf libevent-2.0.21-stable.tar.gz cd libevent-2.0.21-sta ...
- js 10秒倒计时 功能
请等待<span id=</span>秒 <script type="text/javascript"> function run(){ var s ...
- mysql 查询当天、本周,本月,上一个月的数据
今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 近7天 DAY) <= date(时间字段名) 近30天 DAY) & ...
- daydayup2 codeforces143D
这题很考察分析 题意:让你构造n*m的矩阵,使得不存在两个点的距离的平方为5 1若n=1 ,答案为m 2若m=1,答案为n 3若n=2 则这样 110011001100.. 110011001100 ...
- 2014 NOIP 赛前自我整理提醒。
空谈WA,实干AC. 所以作为一个就要上战场的OIer ,实干当然是最重要,但刷题不在多,要点牢记是关键,虽然本渣没记住多少,但还是列几点值得注意的小点. 1.战场上容不得失误. 对于每日都要敲键盘的 ...
- python对XML的解析
原文:http://blog.csdn.net/yueguanghaidao/article/details/7265246 python有三种方法解析XML,SAX,DOM,以及ElementTre ...