当前位置:Gxlcms > PHP教程 > PHP实现的简单三角形、矩形周长面积计算器分享_PHP

PHP实现的简单三角形、矩形周长面积计算器分享_PHP

时间:2021-07-01 10:21:17 帮助过:14人阅读

运用PHP面向对象的知识设计一个图形计算器,同时也运用到了抽象类知识,这个计算器可以计算三角形的周长和面积以及矩形的周长和面积。本图形计算器有4个页面:1.PHP图形计算器主页index.php; 2.形状的抽象类shape.class.php; 3三角形计算类triangle.class.php; 4.矩形计算类rect.class.php。

PHP图形计算器代码点击下载: php图形计算器.zip

代码分别如下:

PHP图形计算器主页:

  1. <title>简单的图形计算器</title>
  2. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  3. <center>
  4. <h2>简单的图形计算器</h2>
  5. 矩形 ||
  6. 三角形
  7. </center>
  8. <hr><br>
  9. <?php
  10. error_reporting(E_ALL & ~E_NOTICE);
  11. //设置自动加载这个程序需要的类文件
  12. function __autoload($classname){
  13. include strtolower($classname).".class.php";
  14. }
  15. //判断用户是否有选择单击一个形状链接
  16. if(!empty($_GET['action'])) {
  17. //第一步:创建形状的对象
  18. $classname = ucfirst($_GET['action']);
  19. $shape=new $classname($_POST);
  20. //第二步:调用形状的对象中的界面view()
  21. $shape -> view();
  22. //第三步:用户是否提交了对应图形界面的表单
  23. if(isset($_POST['dosubmit'])) {
  24. //第四步:查看用户
输出的数据是否正确, 失败则提示 if($shape->yan($_POST)) { //计算图形的周长和面积 echo $shape->name."的周长为:".$shape->zhou()."
"; echo $shape->name."的面积为:".$shape->area()."
"; } } //如果用户没有单击链接, 则是默认访问这个主程序 }else { echo "请选择一个要计算的图形!
"; } ?>

形状的抽象类:

  1. abstract class Shape{
  2. //形状的名称
  3. public $name;
  4. //形状的计算面积方法
  5. abstract function area();
  6. //形状的计算周长的方法
  7. abstract function zhou();
  8. //形状的图形表单界面
  9. abstract function view();
  10. //形状的验证方法
  11. abstract function yan($arr);
  12. }

三角形计算类文件:

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

矩形计算类文件:

  1. class Rect extends Shape {
  2. private $width;
  3. private $height;
  4. function __construct($arr=array()) {
  5. if(!empty($arr)) {
  6. $this->width = $arr['width'];
  7. $this->height = $arr['height'];
  8. }
  9. $this->name = "矩形";
  10. }
  11. function area() {
  12. return $this->width * $this->height;
  13. }
  14. function zhou() {
  15. return 2*($this->width + $this->height);
  16. }
  17. function view() {
  18. $form = '';
  19. $form .= $this->name.'的宽:<input type="text" name="width" value="'.$_POST['width'].'"><br>';
  20. $form .= $this->name.'的高:<input type="text" name="height" value="'.$_POST['height'].'"><br>';
  21. $form .= '<input type="submit" name="dosubmit" value="计算"><br>';
  22. $form .='';
  23. echo $form;
  24. }
  25. function yan($arr) {
  26. $bg = true;
  27. if($arr['width'] < 0) {
  28. echo $this->name."的宽不能小于0!<br>";
  29. $bg = false;
  30. }
  31. if($arr['height'] < 0) {
  32. echo $this->name."的高度不能小于0!<br>";
  33. $bg = false;
  34. }
  35. return $bg;
  36. }
  37. }

人气教程排行