1 <html>
2 <head>
3 <title>圖形計(jì)算器開(kāi)發(fā)</title>
4 <meta http-equiv="Content-type" content="text/html;charset=utf-8">
5 </head>
6
7 <body>
8 <center>
9 <h1>圖形(周長(zhǎng)&面積)計(jì)算器</h1>
10
11 <a href="index.php?action=rect">矩形</a>
12 <a href="index.php?action=triangle">三角形</a>
13 <a href="index.php?action=circle">圓形</a>
14 </center>
15 <?php
16 error_reporting(E_ALL & ~E_NOTICE);
17 function __autoload($className){
18 include strtolower($className).".class.php";
19 }
20 echo new Form("index.php");
21 if(isset($_POST["sub"])){
22 echo new Result();
23 }
24 ?>
25 </body>
26 </html>
\\form.class.php
1 <?php
2 class Form{
3 private $action;
4 private $shape;
5 function __construct($action=""){
6 $this->action = $action;
7 $this->shape = isset($_GET["action"])?$_GET["action"]:"rect";
8 }
9
10 function __toString(){
11 $form='<form action="'.$this->action.'?action='.$this->shape.'" method="post">';
12 $shape="get".ucfirst($this->shape);
13 $form .=$this->$shape();
14 $form .='<br><input type="submit" name="sub" value="計(jì)算"><br>';
15 $form .='</form>';
16 return $form;
17 }
18
19 private function getRect(){
20 $input = '<b>請(qǐng)輸入|矩形|的寬度和高度:</b><p>';
21 $input .= '寬度:<input type="text" name="width" value="'.$_POST["width"].'"><br>';
22 $input .= '高度:<input type="text" name="height" value="'.$_POST["height"].'"><br>';
23 return $input;
24 }
25
26 private function getTriangle(){
27 $input = '<b>請(qǐng)輸入|三角形|的三條邊:</b><p>';
28 $input .= '第一條邊:<input type="text" name="side1" value="'.$_POST["side1"].'"><br>';
29 $input .= '第二條邊:<input type="text" name="side2" value="'.$_POST["side2"].'"><br>';
30 $input .= '第三條邊:<input type="text" name="side3" value="'.$_POST["side3"].'"><br>';
31 return $input;
32 }
33
34 private function getCircle(){
35 $input = '<b>請(qǐng)輸入|圓形|的半徑:</b><p>';
36 $input .= '半徑:<input type="text" name="radius" value="'.$_POST["radius"].'"><br>';
37 return $input;
38 }
39 }
40 ?>
\\shape.class.php
1 <?php
2 abstract class Shape{
3 public $shapeName;
4 abstract function area();
5 abstract function perimeter();
6 protected function validate($value,$message = '輸入的值'){
7 if($value=="" || !is_numeric($value) || $value<0){
8 $message=$this->shapeName.$message;
9 echo '<font color="red">'.$message.'必須為非負(fù)值的數(shù)字,并且不能為空</font><br>';
10 return false;
11 }
12 else{
13 return true;
14 }
15 }
16 }
17 ?>
\\result.class.php
1 <?php
2 class Result{
3 private $shape = null;
4 function __construct(){
5 $this->shape = new $_GET['action']();
6 }
7
8 function __toString(){
9 $result = $this->shape->shapeName.'的周長(zhǎng):'.round($this->shape->perimeter(),2).'<br>';
10 $result .= $this->shape->shapeName.'的面積:'.round($this->shape->area(),2).'<br>';
11 return $result;
12 }
13 }
14 ?>
rect.class.php
1 <?php
2 class Rect extends Shape{
3 private $width = 0;
4 private $height = 0;
5 function __construct(){
6 $this->shapeName = "矩形";
7 if($this->validate($_POST["width"],"寬度") & $this->validate($_POST["height"],"高度")){
8 $this->width = $_POST["width"];
9 $this->height = $_POST["height"];
10 }
11 }
12
13 function area(){
14 return $this->width*$this->height;
15 }
16
17 function perimeter(){
18 return 2*($this->width+$this->height);
19 }
20 }
21 ?>
\\triangle.class.php
1 <?php
2 class Triangle extends Shape{
3 private $side1 = 0;
4 private $side2 = 0;
5 private $side3 = 0;
6 function __construct(){
7 $this->shapeName = "三角形";
8 if($this->validate($_POST["side1"],"第一條邊") &
9 $this->validate($_POST["side2"],"第二條邊") &
10 $this->validate($_POST["side3"],"第三條邊")){
11 if($this->validateSum($_POST["side1"],$_POST["side2"],$_POST["side3"])){
12 $this->side1 = $_POST["side1"];
13 $this->side2 = $_POST["side2"];
14 $this->side3 = $_POST["side3"];
15 }
16 else{
17 echo '<font color="red">三角形的兩邊之和要大于第三邊</font><br>';
18 }
19 }
20 }
21
22 function area(){
23 $s = ($this->side1+$this->side2+$this->side3)/2;
24 return sqrt($s*($s-$this->side1)*($s-$this->side2)*($s-$this->side3));
25 }
26
27 function perimeter(){
28 return $this->side1+$this->side2+$this->side3;
29 }
30
31 private function validateSum($s1,$s2,$s3){
32 if((($s1+$s2)>$s3) && (($s1+$s3)>$s2) && (($s2+$s3)>$s1)){
33 return true;
34 }
35 else{
36 return false;
37 }
38 }
39 }
40 ?>
\\circle.class.php
1 <?php
2 class Circle extends Shape{
3 private $radius = 0;
4 function __construct(){
5 $this->shapeName = "圖形";
6 if($this->validate($_POST['radius'],'半徑')){
7 $this->radius = $_POST['radius'];
8 }
9 }
10
11 function area(){
12 return pi()*$this->radius*$this->radius;
13 }
14
15 function perimeter(){
16 return 2*pi()*$this->radius;
17 }
18 }
19 ?>
第20行的代碼是什么意思?為什么傳入index.php這個(gè)參數(shù)。
還有form.class.php第7行的$_GET是哪來(lái)的,看了整個(gè)代碼沒(méi)看見(jiàn)哪里聲明用get傳遞數(shù)據(jù)。
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
北大課工場(chǎng)是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國(guó)家深化產(chǎn)教融合/校企合作的政策,積極推進(jìn)“中國(guó)制造2025”,實(shí)現(xiàn)中華民族偉大復(fù)興的升級(jí)產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國(guó)職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。