7.10.3 PHP圖形計算器主程序的實現(xiàn)
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>
</center>
<hr><br>
<?php
//判斷用戶是否有選擇單擊一個形狀鏈接
if(!empty($_GET['action'])) {
echo $_GET['action'];
//如果用戶沒有單擊鏈接, 則是默認訪問這個主程序
}else {
echo "請選擇一個要計算的圖形!<br>";
}
?>
</body>
</html>
rect.class.php
<?php
/*
* 這個類是一個矩形的類, 這個類要按形狀的規(guī)范去實現(xiàn)
*
*/
class Rect extends Shape {
private $width;
private $height;
function __construct($arr) {
$this->width = $arr['width'];
$this->height = $arr['height'];
$this->name = $arr['name'];
}
function area() {
return $this->width * $this->height;
}
function zhou() {
return 2*($this->width + $this->height);
}
function view() {
$form = '<form action="index.php" method="post">';
$form .= $this->name.'的寬:<input type="text" name="width" value="" /><br>';
$form .= $this->name.'的高:<input type="text" name="height" value="" /><br>';
$form .= '<input type="submit" name="dosubmit" value="計算"><br>';
$form .='<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;
}
}
shape.class.php
<?php
/*
* 這是一個形狀的抽象類
*
* 定義子類必須實現(xiàn)的一些方法
*
*
*/
abstract class {
//形狀的名稱
protected $name;
//形狀的計算面積方法
abstract function area();
//形狀的計算周長的方法
abstract function zhou();
//形狀的圖形表單界面
abstract function view();
//形狀的驗證方法
abstract function yan();
}