PHP图形计算器(计算三角形矩形周长面积)

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

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

代码分别如下:

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

<html>

    <head>

        <title>简单的图形计算器</title>

        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

    </head>

    <body>

        <center>

            <h1>简单的图形计算器</h1>

            <a href="index.php?action=rect">矩形</a> ||

            <a href="index.php?action=triangle">三角形</a>

        </center>

        <hr><br>

    <?php

            error_reporting(E_ALL & ~E_NOTICE);

            //设置自动加载这个程序需要的类文件

            function __autoload($classname){

                include strtolower($classname).".class.php";

            }

            //判断用户是否有选择单击一个形状链接

            if(!empty($_GET[‘action‘])) {

                //第一步:创建形状的对象

                $classname = ucfirst($_GET[‘action‘]);

                $shape=new $classname($_POST);

                //第二步:调用形状的对象中的界面view()

                $shape -> view();

                //第三步:用户是否提交了对应图形界面的表单

                if(isset($_POST[‘dosubmit‘])) {

                    //第四步:查看用户输出的数据是否正确, 失败则提示

                    if($shape->yan($_POST)) {

                        //计算图形的周长和面积

                        echo $shape->name."的周长为:".$shape->zhou()."<br>";

                        echo $shape->name."的面积为:".$shape->area()."<br>";

                    }

                }

            //如果用户没有单击链接, 则是默认访问这个主程序

            }else {

                echo "请选择一个要计算的图形!<br>";

            }

        ?>

    </body>

</html>

形状的抽象类:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

abstract class  Shape{

    //形状的名称

    public $name;

    //形状的计算面积方法

    abstract function area();

    //形状的计算周长的方法

    abstract function zhou();

    //形状的图形表单界面

    abstract function view();

    //形状的验证方法

    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

56

57

58

59

60

61

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 = "三角形";

    }

    function area() {

        $p =    ($this->bian1 + $this->bian2 + $this->bian3)/2;

        return sqrt($p*($p-$this->bian1)*($p-$this->bian2)*($p-$this->bian3));

    }

    function zhou() {

        return $this->bian1 + $this->bian2 + $this->bian3;

    }

    function view() {

        $form = ‘<form action="index.php?action=triangle" method="post">‘;

        $form .= $this->name.‘第一个边:<input type="text" name="bian1" value="‘.$_POST[‘bian1‘].‘" /><br>‘;

        $form .= $this->name.‘第二个边:<input type="text" name="bian2" value="‘.$_POST[‘bian2‘].‘" /><br>‘;

        $form .= $this->name.‘第三个边:<input type="text" name="bian3" value="‘.$_POST[‘bian3‘].‘" /><br>‘;

        $form .= ‘<input type="submit" name="dosubmit" value="计算"><br>‘;

        $form .=‘<form>‘;

        echo $form;

    }

    function yan($arr) {

        $bj = true;

        if($arr[‘bian1‘] < 0) {

            echo "第一个边不能小于0!<br>";

            $bj = false;

        }

        if($arr[‘bian2‘] < 0) {

            echo "第二个边不能小于0!<br>";

            $bj = false;

        }

        if($arr[‘bian3‘] < 0) {

            echo "第三个边不能小于0!<br>";

            $bj = false;

        }

        if(($arr[‘bian1‘]+$arr[‘bian2‘] < $arr[‘bian3‘]) || ($arr[‘bian1‘] + $arr[‘bian3‘] < $arr[‘bian2‘]) || ($arr[‘bian2‘]+$arr[‘bian3‘] < $arr[‘bian1‘])) {

            echo "两边之和必须大于第三个边";

            $bj = false;

        }

        return $bj;

    }

}

矩形计算类文件:


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

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 = "矩形";

    }

    function area() {

        return $this->width * $this->height;

    }

    function zhou() {

        return 2*($this->width + $this->height);

    }

    function view() {

        $form = ‘<form action="index.php?action=rect" method="post">‘;

        $form .= $this->name.‘的宽:<input type="text" name="width" value="‘.$_POST[‘width‘].‘" /><br>‘;

        $form .= $this->name.‘的高:<input type="text" name="height" value="‘.$_POST[‘height‘].‘" /><br>‘;

        $form .= ‘<input type="submit" name="dosubmit" value="计算"><br>‘;

        $form .=‘<form>‘;

        echo $form;

    }

    function yan($arr) {

        $bg = true;

        if($arr[‘width‘] < 0) {

            echo $this->name."的宽不能小于0!<br>";

            $bg = false;   

        }

        if($arr[‘height‘] < 0) {

            echo $this->name."的高度不能小于0!<br>";

            $bg = false;

        }

        return $bg;

    }

}

 

>> 本文固定链接: http://php.ncong.com/yuanma/tuxing_jisuanqi.html

>> 转载请注明: 恩聪php 2014年09月01日 于 恩聪PHP学习教程 发表

时间: 2024-08-08 05:37:12

PHP图形计算器(计算三角形矩形周长面积)的相关文章

创建一个三角形类并且通过成员函数计算三角形的周长和面积《1》

首先定义一个三角形类 class Triangle//三角形类 { public: double getA(void);//得到a的值 double getB(void);//得到b的值 double getC(void);//得到c的值 void setA(double x);//设置a的值 void setB(double y);//设置b的值 void setC(double z);//设置c的值 bool isTriangle(void);//取三边的值 double Perimeter

创建一个三角形类并且使用成员函数计算三角形的周长和面积《2》

首先创建一个三角形类 class Triangle//三角形类 { public: void Setabc(double x, double y, double z);//置三边的值,注意要能成三角形 void Getabc(double *x, double *y, double *z);//取三边的值 double Perimeter(void);//计算三角形的周长 double Area(void);//计算并返回三角形的面积 private: double a, b, c; //三边为

计算三角形的周长以及面积

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace S { class Program { static void Main() { Program p = new Program(); p.a(); } public void a() { Double[] a=new Double[6]; for (int i = 0; i < 6; i++) { if(

计算三角形的周长

private double shang; private double xia; private double gao; public double Zhouchang(double a,double b, double c) { shang=a; xia=b; gao=c; return a+b+c; } Sanjiao sj=new Sanjiao(); System.out.println("三角形的周长为:"+sj.Zhouchang(30.5,20,27.4));

25.按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有

package zhongqiuzuoye; public class Rect { public double width; public double height; Rect(double width,double height) //带有两个参数的构造方法,用于将width和height属性初化; { this.width=width; this.height=height; } Rect() //不带参数的构造方法,将矩形初始化为宽和高都为10. { width=10; height=

Java入门:基础算法之计算三角形面积

本部分介绍如何计算三角形面积. /** * @author: 理工云课堂 * @description: 程序计算三角形的面积.三角形的底和高由用户输入 */ import java.util.Scanner; class AreaTriangleDemo { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the width

使用多态求矩形的面积和周长以及圆形的面积和周长

//使用多态求矩形的面积和周长以及圆形的面积我周长 Shape shape = new Circle(5); //new Square(5,6); double area = shape.GetArea(); double perimeter = shape.GetPerimeter(); Console.WriteLine(" 这个形状的面积是{0},周长是{1}",area,perimeter); Console.ReadKey(); } public abstract class

利用向量积(叉积)计算三角形的面积和多边形的面积

利用向量积(叉积)计算三角形的面积和多边形的面积: 向量的数量积和向量积: (1)  向量的数量积   (1)  向量的向量积 两个向量a和b的叉积(向量积)可以被定义为: 在这里θ表示两向量之间的角夹角(0° ≤ θ ≤ 180°),它位于这两个矢量 所定义的平面上. 向量积的模(长度)可以解释成以a和b为邻边的平行四边形的面积.求三角形ABC的面积,根据向量积的意义,得到: a=axi+ayj+azk; b=bxi+byj+bzk; a×b=(aybz-azby)i+(azbx-axbz)j

(三角剖分)三角形和矩形的面积交 2016湖南省赛

1 // (三角剖分)三角形和矩形的面积交 2016湖南省赛 2 3 #include <iostream> 4 #include <cstdio> 5 #include <cstring> 6 #include <stack> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std; 11 12 const i