PHP implementation of simple triangles, rectangular perimeter area calculator sharing _php instance

Source: Internet
Author: User
Tags abstract

Using the object-oriented knowledge of PHP to design a graphics calculator, but also the use of abstract knowledge, the calculator can calculate the circumference and area of the triangle, as well as the circumference and area of the rectangle. This graphics Calculator has 4 pages: 1.    PHP Graphics Calculator Home index.php;    2. Abstract class shape.class.php of shape;    3 triangular calculation class triangle.class.php; 4. Rectangular calculation class rect.class.php.

PHP Graphics Calculator Code Click to download: PHP graphics calculator. zip

The code is as follows:

PHP Graphics Calculator Home:

 

Abstract class for shapes:

The abstract class shape{
//shape name is public
$name;
The calculated area method of the shape
abstract function areas ();
The method of calculating the circumference of a shape
abstract function Zhou ();
Shape graphic Form Interface
abstract function view ();
The validation method of the shape
abstract function yan ($arr);

Triangle Compute class File:

Class Triangle extends Shape {    private $bian 1;     private $bian 2;  &
Nbsp;  private $bian 3;       function __construct ($arr = Array ()) {         if (!empty ($arr)) {             $this->bian1
= $arr [' Bian1 '];
             $this->bian2 = $arr [' bian2 '];
             $this->bian3 = $arr [' bian3 '];           }           $
This->name = "triangle";    &NBSP}       function area () {      
   $p =    ($this->bian1 + $this->bian2 + $this->bian3)/2;           reTurn sqrt ($p * ($p-$this->bian1) * ($p-$this->bian2) * ($p-$this->bian3));    &NBSP}       function Zhou () {      
  return $this->bian1 + $this->bian2 + $this->bian3;    &NBSP}       function view () {      
   $form = ' <form action= ' index.php?action=triangle ' method= ' post ' > ';          $form. = $this->name. ' First side: <input type= "text" Name= "Bian1"
Value= "'. $_post[' Bian1 ']. '/><br> ';          $form. = $this->name. ' Second edge: <input type= "text" Name= "Bian2"
Value= "'. $_post[' bian2 ']. '/><br> ';          $form. = $this->name. ' Third side: <input type= "text" Name= "BIAN3"
Value= "'. $_post[' bian3 ']. '/><br> ';          $form. = ' <inpuT type= "submit" Name= "dosubmit" value= "calculated" ><br> ";
         $form. = ' <form> ';
        echo $form; &NBSP;&NBSP;&NBSP;&NBSP}       function Yan ($arr) {      
   $BJ = true;         if ($arr [' Bian1 '] < 0) {      
      echo "The first side can not be less than 0!<br>";
             $BJ = false; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}           if ($arr [' bian2 '] < 0) {            echo] The second edge cannot be less than 0!<br
> ";
             $BJ = false; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;   if ($arr [' bian3 '] < 0) {            
echo "Third edge cannot be less than 0!<br>";
             $BJ = false; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}           if ($ arr[' bian1 ']+ $arr [' bian2 '] < $arr [' bian3 ']) | | ($arr [' bian1 '] + $arr [' bian3 '] < $arr [' bian2 ']) | | ($arr [' bian2 ']+ $arr [' bian3 '] < $arr [' Bian1 ']) {            echo "The sum of both sides must be greater than the third side";    
          $BJ = false; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}           return $ 
bj &NBSP;&NBSP;&NBSP;&NBSP}}

rectangular Computed class file:

Class Rect extends Shape {    private $width;     private $height;   &NBSP ;    function __construct ($arr =array ()) {          if ( !empty ($arr)) {             $this->width = $arr ['
Width '];
             $this->height = $arr [' height ']; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}          $this->
Name = "Rectangle"; &NBSP;&NBSP;&NBSP;&NBSP}       function area () {      
  return $this->width * $this->height; &NBSP;&NBSP;&NBSP;&NBSP}       function Zhou () {      
  return 2* ($this->width + $this->height); &NBSP;&NBSP;&NBSP;&NBSP}       function view () {         $form = ' <form action= ' index.php?action=rect ' method= ' post ' > ';          $form. = $this->name. ' Wide: <input type= "text" name= "width"
Value= "'. $_post[' width ']. '/><br> ';          $form. = $this->name. ' High: <input type= "text" name= "height"
Value= "'. $_post[' height ']. '/><br> ';          $form. = ' <input type= "submit" Name= "Dosubmit" value= "calculation" >
<br> ';
         $form. = ' <form> ';
        echo $form; &NBSP;&NBSP;&NBSP;&NBSP}       function Yan ($arr) {      
   $BG = true;         if ($arr [' width '] < 0) {             echo $this->name. " The width cannot be less than 0!&Lt;br> ";              $BG = false;      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}           if ($arr [' Height '] < 0 {            echo $this->name. "
The height can not be less than 0!<br> ";
             $BG = false; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}           return $
Bg &NBSP;&NBSP;&NBSP;&NBSP}  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.