1 <html>
2 <head>
3 <title>圖形計算器開發(fā)</title>
4 <meta http-equiv="Content-type" content="text/html;charset=utf-8">
5 </head>
6
7 <body>
8 <center>
9 <h1>圖形(周長&面積)計算器</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="計算"><br>';
15 $form .='</form>';
16 return $form;
17 }
18
19 private function getRect(){
20 $input = '<b>請輸入|矩形|的寬度和高度:</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>請輸入|三角形|的三條邊:</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>請輸入|圓形|的半徑:</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.'必須為非負值的數字,并且不能為空</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.'的周長:'.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這個參數。
還有form.class.php第7行的$_GET是哪來的,看了整個代碼沒看見哪里聲明用get傳遞數據。
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數據專業(yè)的國家
北大青鳥中博軟件學院創(chuàng)立于2003年,作為華東區(qū)著名互聯網學院和江蘇省首批服務外包人才培訓基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術與教育服務機構,發(fā)展為教育服務業(yè)的綜合性企業(yè)集團,成為集合面授教學培訓、網
達內教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構,是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經理從事移動互聯網管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經理職務負責iOS教學及管理工作。
浪潮集團項目經理。精通Java與.NET 技術, 熟練的跨平臺面向對象開發(fā)經驗,技術功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網頁制作和網頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經驗。曾經歷任德國Software AG 技術顧問,美國Dachieve 系統(tǒng)架構師,美國AngelEngineers Inc. 系統(tǒng)架構師。