运用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学习教程 发表

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

  1. python3 练手实例1 计算三角形周长和面积

    def j(): a,b,c=map(float,input('请输入三角形三条边的长度,用空格隔开:').split()) if a>0 and b>0 and c>0 and a ...

  2. Java练习 SDUT-3339_计算长方形的周长和面积(类和对象)

    计算长方形的周长和面积(类和对象) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 设计一个长方形类Rect,计算长方形 ...

  3. 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( ...

  4. OpenJudge计算概论-计算三角形面积【海伦公式】

    /*============================================== 计算三角形面积 总时间限制: 1000ms 内存限制: 65536kB 描述 平面上有一个三角形,它的 ...

  5. 计算概论(A)/基础编程练习2(8题)/3:计算三角形面积

    #include<stdio.h> #include<math.h> int main() { // 声明三角形的三个顶点坐标和面积 float x1, y1, x2, y2, ...

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

    本部分介绍如何计算三角形面积. /** * @author: 理工云课堂 * @description: 程序计算三角形的面积.三角形的底和高由用户输入 */ import java.util.Sca ...

  7. 大一C语言学习笔记(11)---编程篇--写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积,要求 0 bug;

    考核内容: 写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积: 答案: #include<stdio.h ...

  8. PHP面向对象实例(图形计算器)

    效果:

  9. php:兄弟连之面向对象版图形计算器1

    曾经看细说PHP的时候就想做这个,可是一直没什么时间,这次总算忙里偷闲搞了代码量比較多的project. 首先,文档结构,都在一个文件夹下就好了,我的就例如以下. 一開始,进入index.php文件. ...

随机推荐

  1. 【模拟】Codeforces 699A Launch of Collider

    题目链接: http://codeforces.com/problemset/problem/699/A 题目大意: 给N个点,向左或向右运动,速度均为1,问最早什么时候有两个点相撞.无解输出-1 题 ...

  2. 【数学相关、规律】Codeforces 696B Puzzles

    题目链接: http://codeforces.com/problemset/problem/696/B 题目大意: 给一棵树,从根节点开始递归,time=1,每次递归等概率随机访问这个节点的子节点, ...

  3. C#:ref和out的联系及区别

    一:ref 关键字使参数按引用传递. 其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中.若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字. ...

  4. HDOJ(HDU) 2201 熊猫阿波的故事(概率问题)

    Problem Description 凡看过功夫熊猫这部电影的人都会对影片中那只憨憨的熊猫阿波留下相当深的印象,胖胖的熊猫阿波自从打败了凶狠强悍的雪豹泰龙以后,在和平谷的地位是越来越高,成为谷中第一 ...

  5. 浅谈层次化的AI架构

    原文地址:http://www.aisharing.com/archives/86/comment-page-1 记得在以前的一篇文章中谈到了一种类似于双缓冲的AI结构,最近在整理一些东西的时候,发现 ...

  6. SRM 390(1-250pt)

    DIV1 250pt 题意:给定整数n和k,问最少需要多少个n连接在一起形成的新整数t,使得t是k的倍数.如果不能形成倍数,输出-1.k <= 10^5,n <= 10^9. 解法:不断增 ...

  7. SRM 508(2-1000pt)

    DIV2 1000pt 题意:给定整数n和r,求有多少个这样的数列,a1,a2...an,使得a1 + a2 +...+an = a1|a2|a3|...|an,(按位或).输出这样数列的个数mod  ...

  8. Linux下profile environment bashrc的区别

        先将export LANG=zh_CN加入/etc/profile ,退出系统重新登录,登录提示显示英文.将/etc/profile 中的export LANG=zh_CN删除,将LNAG=z ...

  9. Linux 输出重定向>和>>的区别是什么

    > 是定向输出到文件,如果文件不存在,就创建文件:如果文件存在,就将其清空:一般我们备份清理日志文件的时候,就是这种方法:先备份日志,再用`>`,将日志文件清空(文件大小变成0字节): & ...

  10. jiaocheng https://github.com/CarpenterLee/JCFInternals

    https://github.com/CarpenterLee/JCFInternals