PHP实现简易图形计算器

吾爱主题 阅读:123 2021-10-25 14:09:00 评论:0

本文实例为大家分享了PHP实现简易图形计算器的具体代码,供大家参考,具体内容如下

主函数:index.php

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 <!doctype html> < html > < head > < meta charset = "utf-8" > < title >图形计算器</ title > < style >   * {   margin: 0px;   padding: 0px;   }   #contains {    width: 500px;   margin: 20px auto;   background: #0C0;   text-align: center;     }   h1 {    width: 500px;   height: 60px;   }   a {    font-size: 20px;   text-decoration: none;   }     #footer {    width: 300px;   background: #fff;   margin: 0 auto;   padding: 5px 10px;   border-radius: 150px;   } </ style > </ head >   < body >   < div id = "contains" >    < h1 >简易图形计算器</ h1 >   < a href = 'index.php?action=rect' >矩形</ a > |   < a href = 'index.php?action=triangle' >三角形</ a >|   < a href = 'index.php?action=cirle' >圆形</ a >   < hr >     <? php    ini_set("display_errors", "On"); //开启错误调试   //设置错误报告的级别,除了无关紧要的'注意',其他的报告都输出   error_reporting(E_ALL & ~E_NOTICE);     function __autoload($classname) { //魔术方法 自动加载类    include strtolower($classname).".class.php"; //将类名转化成小写   }   // include "shape.class.php";   // include "rect.class.php";    if (!empty($_GET['action'])) {   // echo "传送成功";    $ classname = ucfirst ($_GET['action']);    $ shape = new $classname($_POST);    $shape->view($_POST);      if (isset($_POST['sub'])) {    echo "< div id = 'footer' >";    if ($shape->yan($_POST)) {         echo "< b >".$shape->name."的周长".$shape->zhou()."</ b >"."< br >";     echo "< br >";     echo "< b >".$shape->name."的面积".$shape->area()."</ b >"."< br >";    }else {     echo "< b >错误:$shape->error</ b >";    }    echo "</ div >";    }      } else {    echo "请选择一个图形";   }     ?>     </ div > </ body > </ html >

先定义一个抽象类

?
1 2 3 4 5 6 7 8 9 10 <?php   abstract class Shape {   private $name ;   private $error ;   abstract function area();   abstract function zhou();   abstract function view( $arr );   abstract function yan( $arr );   } ?>

矩形类的编写

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 <?php   class Rect extends Shape {   private $width ;   private $height ;     function __construct( $arr = array ()) {    if (! empty ( $arr )) {     $this ->width = $arr [ 'width' ];     $this ->height = $arr [ 'height' ];    }    $this ->name = "矩形" ;    $this ->error = '' ;   }    function area() {    return $this ->width * $this ->height;   }   function zhou() {    return ( $this ->width+ $this ->height) * 2;   }   function view( $arr ) {    $form .= "<form action='index.php?action=rect' method='post'>" ;    $form .= "请输入" . $arr [ 'name' ]. "的宽度:<input type='text' name='width' value='" . $_POST ['width ']."' /><br>";    $form .= "<br>" ;    $form .= "请输入" . $arr [ 'name' ]. "的长度:<input type='text' name='height' value='" . $_POST ['height ']."' /><br>";    $form .= "<br>" ;    $form .= "<input type='submit' name='sub' value='提交'/>  " ;    $form .= "<input type='reset' name='ret' value='重置'/>" ;    $form .= "</form>" ;    echo $form ;   }   function yan( $arr ) {    $bz = true;      if ( $arr [ 'width' ]< 0) {     $this ->error .= "宽度小于0;" ;    $bz = false;    } else {     if (! is_numeric ( $arr [ 'width' ])) {     $this ->error .= "宽不是数字;" ;     $bz = false;    }    }    if ( $arr [ 'height' ]< 0) {     $this ->error .= "宽度小于0;" ;    $bz = false;    } else {     if (! is_numeric ( $arr [ 'height' ])) {     $this ->error .= "高不是数字;" ;     $bz = false;    }    }    return $bz ;   }   } ?>

三角形类:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 <?php   class Triangle extends Shape {    private $bian1 ;   private $bian2 ;   private $bian3 ;      function __construct( $arr = array ()) {    if (! empty ( $arr )) {     $this ->bian1 = $arr [ 'bian1' ];     $this ->bian2 = $arr [ 'bian2' ];    $this ->bian3 = $arr [ 'bian3' ];    }    $this ->name = "三角形" ;    $this ->error = '' ;   }    function area() {    $p = ( $this ->bian1 + $this ->bian2 + $this ->bian3) / 2;   // p(p-a)(p-b)(p-c)    return sqrt( $p *( $p - $this ->bian1)*( $p - $this ->bian2)*( $p - $this ->bian3));   }   function zhou() {    return $this ->bian1+ $this ->bian2+ $this ->bian3;   }   function view( $arr ) {    $form .= "<form action='index.php?action=triangle' method='post'>" ;    $form .= "请输入" . $arr [ 'name' ]. "的第一条边:<input type='text' name='bian1' value='" . $_POST ['bian1 ']."' /><br>";    $form .= "<br>" ;    $form .= "请输入" . $arr [ 'name' ]. "的第二条边:<input type='text' name='bian2' value='" . $_POST ['bian2 ']."' /><br>";    $form .= "<br>" ;    $form .= "请输入" . $arr [ 'name' ]. "的第三条边:<input type='text' name='bian3' value='" . $_POST ['bian3 ']."' /><br>";    $form .= "<br>" ;    $form .= "<input type='submit' name='sub' value='提交'/>  " ;    $form .= "<input type='reset' name='ret' value='重置'/>" ;    $form .= "</form>" ;    echo $form ;   }   function yan( $arr ) {    $bz = true;      if ( $arr [ 'bian1' ]< 0) {     $this ->error .= "第一条边小于0;" ;    $bz = false;    } else {     if (! is_numeric ( $arr [ 'bian1' ])) {     $this ->error .= "第一条边不是数字;" ;     $bz = false;    }    }    if ( $arr [ 'bian2' ]< 0) {     $this ->error .= "第二条边小0;" ;    $bz = false;    } else {     if (! is_numeric ( $arr [ 'bian2' ])) {     $this ->error .= "第二条边不是数字;" ;     $bz = false;    }    }    if ( $arr [ 'bian2' ]< 0) {     $this ->error .= "第三条边小于0;" ;    $bz = false;    } else {     if (! is_numeric ( $arr [ 'bian2' ])) {     $this ->error .= "第三条边不是数字;" ;     $bz = false;    }    }    if (( $this ->bian1+ $this ->bian2) < $this ->bian3 ||( $this ->bian1+ $this ->bian3) < $this ->bian2 ||( $this ->bian2+ $this ->bian3) < $this ->bian1) {     $this ->error .= "三条边不能构成三角形" ;    $bz = false;    }    return $bz ;   }   } ?>

其他的类只要按照上面的格式改下就很好写出来了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/shofe11/article/details/37340181

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容