PHP實(shí)現(xiàn)簡(jiǎn)易圖形計(jì)算器
本文實(shí)例為大家分享了PHP實(shí)現(xiàn)簡(jiǎn)易圖形計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
主函數(shù):index.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>圖形計(jì)算器</title>
<style>
* {
margin: 0px;
padding: 0px;
}
#contains {
width: 500px;
margin: 20px auto;
background: #0C0;
text-align: center;
}
h1 {
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;
}
</style>
</head>
<body>
<div id="contains">
<h1>簡(jiǎn)易圖形計(jì)算器</h1>
<a href='index.php?action=rect'>矩形</a> |
<a href='index.php?action=triangle'>三角形</a>|
<a href='index.php?action=cirle'>圓形</a>
<hr>
<?php
ini_set("display_errors", "On"); //開啟錯(cuò)誤調(diào)試
//設(shè)置錯(cuò)誤報(bào)告的級(jí)別,除了無關(guān)緊要的'注意',其他的報(bào)告都輸出
error_reporting(E_ALL & ~E_NOTICE);
function __autoload($classname) { //魔術(shù)方法 自動(dòng)加載類
include strtolower($classname).".class.php"; //將類名轉(zhuǎn)化成小寫
}
// 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 "<div id='footer'>";
if ($shape->yan($_POST)) {
echo "<b>".$shape->name."的周長(zhǎng)".$shape->zhou()."</b>"."<br>";
echo "<br>";
echo "<b>".$shape->name."的面積".$shape->area()."</b>"."<br>";
}else {
echo "<b>錯(cuò)誤:$shape->error</b>";
}
echo "</div>";
}
} else {
echo "請(qǐng)選擇一個(gè)圖形";
}
?>
</div>
</body>
</html>
先定義一個(gè)抽象類
<?php
abstract class Shape {
private $name;
private $error;
abstract function area();
abstract function zhou();
abstract function view($arr);
abstract function yan($arr);
}
?>
矩形類的編寫
<?php
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 action='index.php?action=rect' method='post'>";
$form .= "請(qǐng)輸入".$arr['name']."的寬度:<input type='text' name='width' value='".$_POST['width']."'/><br>";
$form .= "<br>";
$form .= "請(qǐng)輸入".$arr['name']."的長(zhǎng)度:<input type='text' name='height' value='".$_POST['height']."'/><br>";
$form .= "<br>";
$form .= "<input type='submit' name='sub' value='提交'/> ";
$form .= "<input type='reset' name='ret' value='重置'/>";
$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 .= "寬不是數(shù)字;";
$bz = false;
}
}
if ($arr['height']< 0) {
$this->error .= "寬度小于0;";
$bz = false;
} else {
if (!is_numeric($arr['height'])) {
$this->error .= "高不是數(shù)字;";
$bz = false;
}
}
return $bz;
}
}
?>
三角形類:
<?php
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 action='index.php?action=triangle' method='post'>";
$form .= "請(qǐng)輸入".$arr['name']."的第一條邊:<input type='text' name='bian1' value='".$_POST['bian1']."'/><br>";
$form .= "<br>";
$form .= "請(qǐng)輸入".$arr['name']."的第二條邊:<input type='text' name='bian2' value='".$_POST['bian2']."'/><br>";
$form .= "<br>";
$form .= "請(qǐng)輸入".$arr['name']."的第三條邊:<input type='text' name='bian3' value='".$_POST['bian3']."'/><br>";
$form .= "<br>";
$form .= "<input type='submit' name='sub' value='提交'/> ";
$form .= "<input type='reset' name='ret' value='重置'/>";
$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 .= "第一條邊不是數(shù)字;";
$bz = false;
}
}
if ($arr['bian2']< 0) {
$this->error .= "第二條邊小0;";
$bz = false;
} else {
if (!is_numeric($arr['bian2'])) {
$this->error .= "第二條邊不是數(shù)字;";
$bz = false;
}
}
if ($arr['bian2']< 0) {
$this->error .= "第三條邊小于0;";
$bz = false;
} else {
if (!is_numeric($arr['bian2'])) {
$this->error .= "第三條邊不是數(shù)字;";
$bz = false;
}
}
if (($this->bian1+$this->bian2) < $this->bian3 ||($this->bian1+$this->bian3) < $this->bian2 ||($this->bian2+$this->bian3) < $this->bian1) {
$this->error .= "三條邊不能構(gòu)成三角形";
$bz = false;
}
return $bz;
}
}
?>
其他的類只要按照上面的格式改下就很好寫出來了。


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PHP實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器
- php實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
- PHP實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- PHP實(shí)現(xiàn)的簡(jiǎn)單在線計(jì)算器功能示例
- php編程實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)頁版計(jì)算器功能示例
- PHP房貸計(jì)算器實(shí)例代碼,等額本息,等額本金
- PHP實(shí)現(xiàn)的簡(jiǎn)單三角形、矩形周長(zhǎng)面積計(jì)算器分享
- 用php簡(jiǎn)單實(shí)現(xiàn)加減乘除計(jì)算器
- php學(xué)習(xí)之簡(jiǎn)單計(jì)算器實(shí)現(xiàn)代碼
- PHP實(shí)現(xiàn)計(jì)算器小功能
相關(guān)文章
PHP 在數(shù)組中搜索給定的簡(jiǎn)單實(shí)例 array_search 函數(shù)
下面小編就為大家?guī)硪黄狿HP 在數(shù)組中搜索給定的簡(jiǎn)單實(shí)例 array_search 函數(shù)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
使用array_map簡(jiǎn)單搞定PHP刪除文件、刪除目錄
這篇文章主要介紹了使用array_map簡(jiǎn)單搞定PHP刪除文件、刪除目錄的相關(guān)資料,需要的朋友可以參考下2014-10-10
淺析PHP中call user func()函數(shù)及如何使用call user func調(diào)用自定義函數(shù)
使用call_user_func函數(shù),通過傳入字符串函數(shù),可以調(diào)用自定義函數(shù),并且支持引用。該函數(shù)允許用戶調(diào)用直接寫的函數(shù)并傳入一定的參數(shù),下面總結(jié)下這個(gè)函數(shù)的使用方法,需要的朋友參考下2015-11-11
PHP用正則匹配form表單中所有元素的類型和屬性值實(shí)例代碼
這篇文章主要介紹了PHP用正則匹配form表單中所有元素的類型和屬性值的方法,文中給出了完整的實(shí)例代碼,大家可以直接參考學(xué)習(xí),下面來一起看看吧。2017-02-02

