PHP實(shí)現(xiàn)的簡(jiǎn)單三角形、矩形周長(zhǎng)面積計(jì)算器分享
運(yùn)用PHP面向?qū)ο蟮闹R(shí)設(shè)計(jì)一個(gè)圖形計(jì)算器,同時(shí)也運(yùn)用到了抽象類(lèi)知識(shí),這個(gè)計(jì)算器可以計(jì)算三角形的周長(zhǎng)和面積以及矩形的周長(zhǎng)和面積。本圖形計(jì)算器有4個(gè)頁(yè)面:1.PHP圖形計(jì)算器主頁(yè)index.php; 2.形狀的抽象類(lèi)shape.class.php; 3三角形計(jì)算類(lèi)triangle.class.php; 4.矩形計(jì)算類(lèi)rect.class.php。
PHP圖形計(jì)算器代碼點(diǎn)擊下載: php圖形計(jì)算器.zip
代碼分別如下:
PHP圖形計(jì)算器主頁(yè):
<html>
<head>
<title>簡(jiǎn)單的圖形計(jì)算器</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<center>
<h1>簡(jiǎn)單的圖形計(jì)算器</h1>
<a href="index.php?action=rect">矩形</a> ||
<a href="index.php?action=triangle">三角形</a>
</center>
<hr><br>
<?php
error_reporting(E_ALL & ~E_NOTICE);
//設(shè)置自動(dòng)加載這個(gè)程序需要的類(lèi)文件
function __autoload($classname){
include strtolower($classname).".class.php";
}
//判斷用戶(hù)是否有選擇單擊一個(gè)形狀鏈接
if(!empty($_GET['action'])) {
//第一步:創(chuàng)建形狀的對(duì)象
$classname = ucfirst($_GET['action']);
$shape=new $classname($_POST);
//第二步:調(diào)用形狀的對(duì)象中的界面view()
$shape -> view();
//第三步:用戶(hù)是否提交了對(duì)應(yīng)圖形界面的表單
if(isset($_POST['dosubmit'])) {
//第四步:查看用戶(hù)輸出的數(shù)據(jù)是否正確, 失敗則提示
if($shape->yan($_POST)) {
//計(jì)算圖形的周長(zhǎng)和面積
echo $shape->name."的周長(zhǎng)為:".$shape->zhou()."<br>";
echo $shape->name."的面積為:".$shape->area()."<br>";
}
}
//如果用戶(hù)沒(méi)有單擊鏈接, 則是默認(rèn)訪(fǎng)問(wèn)這個(gè)主程序
}else {
echo "請(qǐng)選擇一個(gè)要計(jì)算的圖形!<br>";
}
?>
</body>
</html>
形狀的抽象類(lèi):
abstract class Shape{
//形狀的名稱(chēng)
public $name;
//形狀的計(jì)算面積方法
abstract function area();
//形狀的計(jì)算周長(zhǎng)的方法
abstract function zhou();
//形狀的圖形表單界面
abstract function view();
//形狀的驗(yàn)證方法
abstract function yan($arr);
}
三角形計(jì)算類(lèi)文件:
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 = "三角形";
}
function area() {
$p = ($this->bian1 + $this->bian2 + $this->bian3)/2;
return sqrt($p*($p-$this->bian1)*($p-$this->bian2)*($p-$this->bian3));
}
function zhou() {
return $this->bian1 + $this->bian2 + $this->bian3;
}
function view() {
$form = '<form action="index.php?action=triangle" method="post">';
$form .= $this->name.'第一個(gè)邊:<input type="text" name="bian1" value="'.$_POST['bian1'].'" /><br>';
$form .= $this->name.'第二個(gè)邊:<input type="text" name="bian2" value="'.$_POST['bian2'].'" /><br>';
$form .= $this->name.'第三個(gè)邊:<input type="text" name="bian3" value="'.$_POST['bian3'].'" /><br>';
$form .= '<input type="submit" name="dosubmit" value="計(jì)算"><br>';
$form .='<form>';
echo $form;
}
function yan($arr) {
$bj = true;
if($arr['bian1'] < 0) {
echo "第一個(gè)邊不能小于0!<br>";
$bj = false;
}
if($arr['bian2'] < 0) {
echo "第二個(gè)邊不能小于0!<br>";
$bj = false;
}
if($arr['bian3'] < 0) {
echo "第三個(gè)邊不能小于0!<br>";
$bj = false;
}
if(($arr['bian1']+$arr['bian2'] < $arr['bian3']) || ($arr['bian1'] + $arr['bian3'] < $arr['bian2']) || ($arr['bian2']+$arr['bian3'] < $arr['bian1'])) {
echo "兩邊之和必須大于第三個(gè)邊";
$bj = false;
}
return $bj;
}
}
矩形計(jì)算類(lèi)文件:
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 = "矩形";
}
function area() {
return $this->width * $this->height;
}
function zhou() {
return 2*($this->width + $this->height);
}
function view() {
$form = '<form action="index.php?action=rect" method="post">';
$form .= $this->name.'的寬:<input type="text" name="width" value="'.$_POST['width'].'" /><br>';
$form .= $this->name.'的高:<input type="text" name="height" value="'.$_POST['height'].'" /><br>';
$form .= '<input type="submit" name="dosubmit" value="計(jì)算"><br>';
$form .='<form>';
echo $form;
}
function yan($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;
}
}
- 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)單在線(xiàn)計(jì)算器功能示例
- php編程實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)頁(yè)版計(jì)算器功能示例
- PHP房貸計(jì)算器實(shí)例代碼,等額本息,等額本金
- 用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í)現(xiàn)通過(guò)照片獲取ip地址
在本教程中,我們將學(xué)習(xí)如何用一張照片來(lái)盜取ip地址。我的想法是通過(guò)修改.htaccess文件,將jpg文件當(dāng)作php文件來(lái)解析。感興趣的朋友一起學(xué)習(xí)吧2016-04-04
PHP實(shí)現(xiàn)自動(dòng)加載機(jī)制
這篇文章主要介紹了PHP實(shí)現(xiàn)自動(dòng)加載機(jī)制,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
關(guān)于PHP內(nèi)置的字符串處理函數(shù)詳解
下面小編就為大家?guī)?lái)一篇關(guān)于PHP內(nèi)置的字符串處理函數(shù)詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
Zend Framework教程之前端控制器Zend_Controller_Front用法詳解
這篇文章主要介紹了Zend Framework教程之前端控制器Zend_Controller_Front用法,詳細(xì)分析了前端控制器Zend_Controller_Front的功能,使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03
php網(wǎng)站判斷用戶(hù)是否是手機(jī)訪(fǎng)問(wèn)的方法
PHP網(wǎng)站判斷用戶(hù)是否用手機(jī)訪(fǎng)問(wèn),如果是手機(jī)的話(huà),就跳轉(zhuǎn)到指定的手機(jī)友好頁(yè)面。2013-11-11
Zend Framework框架中實(shí)現(xiàn)Ajax的方法示例
這篇文章主要介紹了Zend Framework框架中實(shí)現(xiàn)Ajax的方法,結(jié)合實(shí)例形式詳細(xì)分析了Zend Framework框架中實(shí)現(xiàn)ajax功能的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
PHP 結(jié)合 Boostrap 結(jié)合 js 實(shí)現(xiàn)學(xué)生列表刪除編輯及搜索功能
這篇文章主要介紹了PHP 結(jié)合 Boostrap 結(jié)合 js 實(shí)現(xiàn)學(xué)生列表刪除編輯以及搜索功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05

