PHP implementation of simple triangle, rectangular perimeter area calculator Share _php instances

Source: Internet
Author: User
Using the PHP object-oriented knowledge to design a graphing calculator, but also the use of abstract knowledge, the calculator can calculate the circumference and area of the triangle and the circumference and area of the rectangle. This graphing calculator has 4 pages: 1. PHP Graphing Calculator homepage index.php; 2. Abstract type of shape shape.class.php; 3 triangle calculation class triangle.class.php; 4. Rectangle calculation class rect.class.php.

PHP Graphing Calculator Code click to download: Php graphing calculator. zip

The code is as follows:

PHP Graphing Calculator Home page:

            <title>A simple graphing Calculator</title>        
 
  
                  
 
              
 
   

A simple graphing Calculator

Rectangle | | Triangle
<?php error_reporting (E_all & ~e_notice); Set the class file function __autoload ($classname) {include strtolower ($classname) required to load this program automatically. Class.php "; }//Determine if the user has the option to click a shape link if (!empty ($_get[' action ')) {//First step: Create the shape's object $cl Assname = Ucfirst ($_get[' action '); $shape =new $classname ($_post); Step two: Call the interface in the shape's Object view () $shape view (); The third step: whether the user submitted the corresponding graphical interface of the form if (Isset ($_post[' Dosubmit ')) {//Fourth step: Check whether the user output data is correct, failure prompts if ($shape->yan ($_post)) {//calculates the perimeter and area of the graph Echo $shape->name. "The perimeter is:". $shape->zhou (). "
"; echo $shape->name. " The area is: ". $shape->area ()."
"; }}//If the user does not click the link, it is the default access to the main program}else {echo "Please select a graph to calculate!"
"; }?>

Abstract classes of shapes:

Abstract class  shape{    //Shape name public    $name;     The computed area method of the shape is the    abstract function areas ();     The method of calculating the perimeter of the shape is    abstract function Zhou ();     Shape graphical Form Interface    abstract function view ();    The method of validating the shape is    abstract function yan ($arr);}

Triangle Calculation class file:

Class Triangle extends Shape {private $bian 1;    Private $bian 2;     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";         } 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. = $this->name. ' First side:
  
  
'; $form. = $this->name. ' Second side:
'; $form. = $this->name. ' Third side:
'; $form. = '
'; $form. = ''; Echo $form; } function Yan ($arr) {$bj = true; if ($arr [' Bian1 '] < 0) {echo ' first edge cannot be less than 0!
"; $BJ = false; } if ($arr [' bian2 '] < 0) {echo ' second edge cannot be less than 0!
"; $BJ = false; if ($arr [' bian3 '] < 0) {echo ' third edge cannot be less than 0!
"; $BJ = false; if ($arr [' Bian1 ']+ $arr [' bian2 '] < $arr [' bian3 ']) | | ($arr [' bian1 '] + $arr [' bian3 '] < $arr [' bian2 ']) | | ($arr [' bian2 ']+ $arr [' bian3 '] < $arr [' Bian1 ']) {echo "must be greater than the third edge on both sides"; $BJ = false; } return $BJ; }}

Rectangle Calculation class File:

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 = "Rectangle";    } function area () {return $this->width * $this->height;    } function Zhou () {return ($this->width + $this->height); } function View () {$form = ''; $form. = $this->name. ' Width:
'; $form. = $this->name. ' High:
'; $form. = '
'; $form. = ''; Echo $form; } function Yan ($arr) {$BG = true; if ($arr [' width '] < 0) {echo $this->name. The width cannot be less than 0!
"; $BG = false; if ($arr [' height '] < 0) {echo $this->name. The height cannot be less than 0!
"; $BG = false; } return $BG; } }
  • 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.