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 ...
随机推荐
- 关于firstChild,firstElementChild和children
<div> <p>123</p> </div> 在上面这段代码中,如果使用以下js代码 var oDiv=document.getElementByTa ...
- 建站随手记:about server stack
建站需要,随手记: Server Stack: ----------- 标准的mezzanine的Stack设置 前端:Nginx wsgi:gunicorn cms tool: mezzanine ...
- 【翻译】安卓新播放器EXOplayer介绍
http://developer.android.com/guide/topics/media/exoplayer.html 前言: Playing videos and music is a p ...
- Java—多线程编程
一个多线程程序包含两个或多个能并发运行的部分.程序的每一部分都称作一个线程,并且每个线程定义了一个独立的执行路径. 进程:一个进程包括由操作系统分配的内存空间,包含一个或多个线程.一个线程不能独立的存 ...
- Android开发--Android Studio配置
1.常见问题 emulator: You might want to adjust your AVD RAM size and/or HAXM configuration to run in fast ...
- PHP 文件包含总结 include require 命名空间 autoload spl_autoload_register 读取文件路径
总结: 1. include或require包含其他文件 使用./或者 ../,这里的当前路径和上一层路径,取决于运行脚本的路径,会存在如下问题. 在写PHP程序时,经常要用到include或requ ...
- 使用AXIS2客户端调用 WEBSERVICE
问题 在调用WEBSERVICE时,可以使用wsdl2java生成java代码,调用接口,这种方法在接口固定的情况下是一种不错的选择,如果需要动态调用接口,那么这样就行不通了. 解决办法 1.直接构建 ...
- ie浏览器兼容问题汇总
对兼容ie浏览器所遇到的问题及总结 互联快谈 2016-10-28 05:51 1,若直接给一个元素设置absolute定位.在浏览器缩放的时候.位置会错位.解决的方法是给外层的元素设置为relati ...
- 根据序列图像聚焦区域获取深度 Shape From Focus
最为超新新新新鸟...我也不知道第一篇文章应该写什么..所以,把自己最近正在研究的东西报一下吧, 研究的东西其实也不算深奥,就是对一个图像序列中的每张图像进行检测,发现每张图片的聚焦清晰区域,找到这个 ...
- 【mysql】关于循环插入数据 存储设计
要求插入的数据有一定的规律 新建实例列表 CREATE TABLE users ( userId ) NOT NULL, userName ) NOT NULL, Serves ) NOT NULL, ...