国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

PHP實(shí)現(xiàn)的簡(jiǎn)單三角形、矩形周長(zhǎng)面積計(jì)算器分享

2024-05-04 23:27:35
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這篇文章主要介紹了PHP實(shí)現(xiàn)的簡(jiǎn)單三角形、矩形周長(zhǎng)面積計(jì)算器分享,本文的實(shí)現(xiàn)相對(duì)較簡(jiǎn)單,同時(shí)提供了代碼文件下載,需要的朋友可以參考下

運(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ì)算器主頁(yè):

 

 
  1. <html> 
  2. <head> 
  3. <title>簡(jiǎn)單的圖形計(jì)算器</title> 
  4. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
  5. </head> 
  6.  
  7. <body> 
  8. <center> 
  9. <h1>簡(jiǎn)單的圖形計(jì)算器</h1> 
  10.  
  11. <a href="index.php?action=rect">矩形</a> || 
  12. <a href="index.php?action=triangle">三角形</a>  
  13. </center> 
  14.  
  15. <hr><br> 
  16.  
  17. <?php 
  18. error_reporting(E_ALL & ~E_NOTICE); 
  19.  
  20. //設(shè)置自動(dòng)加載這個(gè)程序需要的類(lèi)文件 
  21. function __autoload($classname){ 
  22. include strtolower($classname).".class.php"
  23.  
  24. //判斷用戶(hù)是否有選擇單擊一個(gè)形狀鏈接 
  25. if(!emptyempty($_GET['action'])) { 
  26. //第一步:創(chuàng)建形狀的對(duì)象 
  27. $classname = ucfirst($_GET['action']); 
  28.  
  29. $shape=new $classname($_POST); 
  30. //第二步:調(diào)用形狀的對(duì)象中的界面view() 
  31. $shape -> view(); 
  32.  
  33. //第三步:用戶(hù)是否提交了對(duì)應(yīng)圖形界面的表單 
  34. if(isset($_POST['dosubmit'])) { 
  35. //第四步:查看用戶(hù)輸出的數(shù)據(jù)是否正確, 失敗則提示 
  36. if($shape->yan($_POST)) { 
  37. //計(jì)算圖形的周長(zhǎng)和面積 
  38. echo $shape->name."的周長(zhǎng)為:".$shape->zhou()."<br>"
  39. echo $shape->name."的面積為:".$shape->area()."<br>"
  40.  
  41. //如果用戶(hù)沒(méi)有單擊鏈接, 則是默認(rèn)訪(fǎng)問(wèn)這個(gè)主程序 
  42. }else { 
  43. echo "請(qǐng)選擇一個(gè)要計(jì)算的圖形!<br>"
  44.  
  45.  
  46. ?> 
  47. </body> 
  48. </html> 

形狀的抽象類(lèi):

 

  1. abstract class Shape{ 
  2. //形狀的名稱(chēng) 
  3. public $name
  4.  
  5. //形狀的計(jì)算面積方法 
  6. abstract function area(); 
  7.  
  8. //形狀的計(jì)算周長(zhǎng)的方法 
  9. abstract function zhou(); 
  10.  
  11. //形狀的圖形表單界面 
  12. abstract function view(); 
  13. //形狀的驗(yàn)證方法 
  14. abstract function yan($arr); 
  15.  

三角形計(jì)算類(lèi)文件:

 

  1. class Triangle extends Shape { 
  2. private $bian1
  3. private $bian2
  4. private $bian3
  5.  
  6. function __construct($arr = array()) { 
  7. if(!emptyempty($arr)) { 
  8. $this->bian1 = $arr['bian1']; 
  9. $this->bian2 = $arr['bian2']; 
  10. $this->bian3 = $arr['bian3']; 
  11.  
  12.  
  13. $this->name = "三角形"
  14.  
  15. function area() { 
  16. $p = ($this->bian1 + $this->bian2 + $this->bian3)/2; 
  17.  
  18. return sqrt($p*($p-$this->bian1)*($p-$this->bian2)*($p-$this->bian3)); 
  19.  
  20. function zhou() { 
  21. return $this->bian1 + $this->bian2 + $this->bian3; 
  22.  
  23. function view() { 
  24. $form = '<form action="index.php?action=triangle" method="post">'
  25. $form .= $this->name.'第一個(gè)邊:<input type="text" name="bian1" value="'.$_POST['bian1'].'" /><br>'
  26. $form .= $this->name.'第二個(gè)邊:<input type="text" name="bian2" value="'.$_POST['bian2'].'" /><br>'
  27. $form .= $this->name.'第三個(gè)邊:<input type="text" name="bian3" value="'.$_POST['bian3'].'" /><br>'
  28. $form .= '<input type="submit" name="dosubmit" value="計(jì)算"><br>'
  29. $form .='<form>'
  30. echo $form
  31.  
  32. function yan($arr) { 
  33. $bj = true; 
  34. if($arr['bian1'] < 0) { 
  35. echo "第一個(gè)邊不能小于0!<br>"
  36. $bj = false; 
  37.  
  38. if($arr['bian2'] < 0) { 
  39. echo "第二個(gè)邊不能小于0!<br>"
  40. $bj = false; 
  41.  
  42. if($arr['bian3'] < 0) { 
  43. echo "第三個(gè)邊不能小于0!<br>"
  44. $bj = false; 
  45.  
  46. if(($arr['bian1']+$arr['bian2'] < $arr['bian3']) || ($arr['bian1'] + $arr['bian3'] < $arr['bian2']) || ($arr['bian2']+$arr['bian3'] < $arr['bian1'])) { 
  47. echo "兩邊之和必須大于第三個(gè)邊"
  48. $bj = false; 
  49.  
  50. return $bj;  

矩形計(jì)算類(lèi)文件:

 

  1. class Rect extends Shape { 
  2. private $width
  3. private $height
  4.  
  5. function __construct($arr=array()) { 
  6.  
  7. if(!emptyempty($arr)) { 
  8. $this->width = $arr['width']; 
  9. $this->height = $arr['height']; 
  10. $this->name = "矩形"
  11.  
  12. function area() { 
  13. return $this->width * $this->height; 
  14.  
  15. function zhou() { 
  16. return 2*($this->width + $this->height); 
  17.  
  18. function view() { 
  19. $form = '<form action="index.php?action=rect" method="post">'
  20. $form .= $this->name.'的寬:<input type="text" name="width" value="'.$_POST['width'].'" /><br>'
  21. $form .= $this->name.'的高:<input type="text" name="height" value="'.$_POST['height'].'" /><br>'
  22. $form .= '<input type="submit" name="dosubmit" value="計(jì)算"><br>'
  23. $form .='<form>'
  24. echo $form
  25.  
  26. function yan($arr) { 
  27. $bg = true; 
  28. if($arr['width'] < 0) { 
  29. echo $this->name."的寬不能小于0!<br>"
  30. $bg = false;  
  31.  
  32. if($arr['height'] < 0) { 
  33. echo $this->name."的高度不能小于0!<br>"
  34. $bg = false; 
  35.  
  36. return $bg
  37.  
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 于田县| 庐江县| 道孚县| 彰化市| 都昌县| 宁陕县| 祥云县| 闸北区| 本溪市| 台中市| 古浪县| 富民县| 文登市| 迁安市| 弋阳县| 盘锦市| 盐源县| 深泽县| 涟源市| 多伦县| 灵寿县| 略阳县| 喀什市| 海口市| 达拉特旗| 新民市| 赞皇县| 隆昌县| 大英县| 铁岭市| 布拖县| 莆田市| 玉树县| 贺兰县| 商水县| 邻水| 高陵县| 邮箱| 肃南| 互助| 蓬溪县|