Simple triangle and rectangular perimeter calculator implemented by PHP _ php instance

Source: Internet
Author: User
This article mainly introduces the simple triangle and rectangular perimeter area calculator implemented by PHP. The implementation in this article is relatively simple and provides code file download, if you need it, you can use PHP object-oriented knowledge to design a graphical calculator and abstract class knowledge. This calculator can calculate the circumference and area of a triangle and the circumference and area of a rectangle. This graphic calculator has four pages: 1. PHP graphical calculator home page index. php; 2. shape abstract class shape. class. php; 3. triangle calculation class triangle. class. php; 4. rect for Rectangle calculation. class. php.

PHP graphics calculator code click to download: PHP graphics calculator. Zip

The Code is as follows:

PHP graphical calculator Home Page:

            Simple graphic calculator        
                  
 
  
Simple graphical calculator rectangle | triangle
          
<? Php error_reporting (E_ALL &~ E_NOTICE); // sets the class file function _ autoload ($ classname) {include strtolower ($ classname) required to automatically load this program ). ". class. php ";} // determines whether the user has selected to click a shape link if (! Empty ($ _ GET ['action']) {// Step 1: Create a shape object $ classname = ucfirst ($ _ GET ['action']); $ shape = new $ classname ($ _ POST); // Step 2: Call the interface view () $ shape-> view () in the shape object; // Step 3: whether the user submitted the form if (isset ($ _ POST ['dosubmit ']) {// Step 4: Check whether the user output data is correct, if ($ shape-> yan ($ _ POST) {// calculate the circumference and area of the image echo $ shape-> name. "perimeter :". $ shape-> zhou ()."
"; Echo $ shape-> name." area: ". $ shape-> area ()."
";}}// If the user does not click the link, the main program is accessed by default.} else {echo" select a graph to calculate!
";}?>

Abstract class of shape:

Abstract class Shape {// Shape name public $ name; // Shape calculation area method abstract function area (); // Shape calculation perimeter method abstract function zhou (); // shape graphical form interface abstract function view (); // shape verification method abstract function yan ($ arr );}

Triangle computing files:

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 = "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 edge:
  
  
'; $ Form. = $ this-> name.' second edge:
'; $ Form. = $ this-> name.' Third edge:
'; $ Form. ='
'; $ Form. =''; Echo $ form;} function yan ($ arr) {$ bj = true; if ($ arr ['bian1'] <0) {echo "the first side cannot be smaller than 0!
"; $ Bj = false;} if ($ arr ['bian2'] <0) {echo" the second side cannot be less than 0!
"; $ Bj = false;} if ($ arr ['bian3'] <0) {echo" the third side 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 "the sum of the two sides must be greater than the third side "; $ bj = false;} return $ bj ;}}

Rectangular computing files:

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 2 * ($ this-> width + $ this-> height);} function view () {$ form =''; $ Form. = $ this-> name.' width:
'; $ Form. = $ this-> name.' height:
'; $ 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.