php实现图形计算器
存档:
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>
<?php
error_reporting(E_ALL & ~E_NOTICE);
function __autoload($className){
include strtolower($className).".class.php";
}
echo new Form("index.php");
if(isset($_POST["sub"])){
echo new Result();
}
?>
</body>
</html>
form.class.php
<?php
class Form{
private $action;
private $shape;
function __construct($action=""){
$this->action = $action;
$this->shape = isset($_GET["action"])?$_GET["action"]:"rect";
} function __toString(){
$form='<form action="'.$this->action.'?action='.$this->shape.'" method="post">';
$shape="get".ucfirst($this->shape);
$form .=$this->$shape();
$form .='<br><input type="submit" name="sub" value="计算"><br>';
$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>';
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>';
return $input;
} private function getCircle(){
$input = '<b>请输入|圆形|的半径:</b><p>';
$input .= '半径:<input type="text" name="radius" value="'.$_POST["radius"].'"><br>';
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){
$message=$this->shapeName.$message;
echo '<font color="red">'.$message.'必须为非负值的数字,并且不能为空</font><br>';
return false;
}
else{
return true;
}
}
}
?>
result.class.php
<?php
class Result{
private $shape = null;
function __construct(){
$this->shape = new $_GET['action']();
} function __toString(){
$result = $this->shape->shapeName.'的周长:'.round($this->shape->perimeter(),2).'<br>';
$result .= $this->shape->shapeName.'的面积:'.round($this->shape->area(),2).'<br>';
return $result;
}
}
?>
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"];
}
} 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"],"第三条边")){
if($this->validateSum($_POST["side1"],$_POST["side2"],$_POST["side3"])){
$this->side1 = $_POST["side1"];
$this->side2 = $_POST["side2"];
$this->side3 = $_POST["side3"];
}
else{
echo '<font color="red">三角形的两边之和要大于第三边</font><br>';
}
}
} 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($s1,$s2,$s3){
if((($s1+$s2)>$s3) && (($s1+$s3)>$s2) && (($s2+$s3)>$s1)){
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'];
}
} function area(){
return pi()*$this->radius*$this->radius;
} function perimeter(){
return 2*pi()*$this->radius;
}
}
?>
结果如下:



php实现图形计算器的更多相关文章
- PHP学习笔记06——面向对象版图形计算器
index.php 用于显示页面 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "h ...
- PHP.11-PHP实例(二)-面向对象实例(图形计算器)
面向对象实例(图形计算器) [PHP语法详解] 1.实现外观 #不同的动作,输出不同的表单 ###关于PHP中,无法使用localhost访问.php文件[http://www.360doc.com/ ...
- PHP图形计算器(计算三角形矩形周长面积)
运用PHP面向对象的知识设计一个图形计算器,同时也运用到了抽象类知识,这个计算器可以计算三角形的周长和面积以及矩形的周长和面积.本图形计算器有4个页面:1.PHP图形计算器主页index.php; ...
- php:兄弟连之面向对象版图形计算器1
曾经看细说PHP的时候就想做这个,可是一直没什么时间,这次总算忙里偷闲搞了代码量比較多的project. 首先,文档结构,都在一个文件夹下就好了,我的就例如以下. 一開始,进入index.php文件. ...
- [图形计算器]Desmos
一.图形计算器 var elt = document.getElementById('calculator'); var calculator = Desmos.GraphingCalculator( ...
- 图形计算器(geogebra[5.0.278.0])使用QQ浏览器打开下载
点击这里下载Geogebra图形计算器
- PHP面向对象实例(图形计算器)
效果:
- 【java图形计算器】 java awt swing组件应用
package package1; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swi ...
- php:兄弟连之面向对象版图形计算器2
上篇说到通过result.class.php来分流,因为三个类都继承了shape这个类,让我们来看一下,面向对象中的继承. shape.class.shape文件 <?php abstract ...
随机推荐
- Kali-linux使用Maltego收集信息
Maltego是一个开源的漏洞评估工具,它主要用于论证一个网络内单点故障的复杂性和严重性.该工具能够聚集来自内部和外部资源的信息,并且提供一个清晰的漏洞分析界面.本节将使用Kali Linux操作系统 ...
- 转:日志组件logback的介绍及配置使用方法
转自:http://blog.csdn.net/zgmzyr/article/details/8267072 一.logback的介绍 Logback是由log4j创始人设计的又一个开源日志组件.lo ...
- sum函数
>>> np.sum([0.5, 1.5]) 2.0 >>> np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32) 1 > ...
- SVN篇
启动SVN : svnserve -d -r svn 查看进程: ps -ef | grep svmserve -------------------------------------------- ...
- 【noip模拟赛 王强的疑惑】 题解
考试题. 是个DP. 50分可以通过子集枚举+线段覆盖(贪心)完成. 考试没时间写了一个子集枚举30分. #include <cstdio> #include <cstring> ...
- HDU 1007 Quoit Design(经典最近点对问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...
- vlc源码分析(三) 调用live555接收RTSP数据
首先了解RTSP/RTP/RTCP相关概念,尤其是了解RTP协议:RTP与RTCP协议介绍(转载). vlc使用模块加载机制调用live555,调用live555的文件是live555.cpp. 一. ...
- 【ZOJ 2996】(1+x)^n(二项式定理)
Please calculate the coefficient modulo 2 of x^i in (1+x)^n. Input For each case, there are two inte ...
- BZOJ 1941: [Sdoi2010]Hide and Seek(k-d Tree)
Time Limit: 16 Sec Memory Limit: 162 MBSubmit: 1712 Solved: 932[Submit][Status][Discuss] Descripti ...
- centos7添加新网卡实现双IP双网关
问题背景: 业务需要,针对业务需要不同地域的机构访问,所以需要在同一台机器上配置不同IP并配置不同网关,实现不用机构可以访问同一台服务器办理业务. 系统环境: centos linux7 网络环境: ...