PHP Implementation of Simple Triangle Rectangular Perimeter Area Calculator Sharing
- 2021-08-05 09:17:21
- OfStack
Using PHP object-oriented knowledge to design a graphic calculator, but also using abstract knowledge, this calculator can calculate the perimeter and area of triangular and rectangular perimeter and area. This graphical calculator has 4 pages: 1. PHP graphical calculator home page index. php; 2. Abstract classes of shapes shape. class. php; 33 angular calculation class triangle. class. php;; 4. Rectangular calculation class rect. class. php.
PHP Graphic Calculator Code Click Download: php Graphic Calculator. zip
The codes are as follows:
PHP Graphic Calculator Home Page:
<html>
<head>
<title> Simple graphic calculator </title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<center>
<h1> Simple graphic calculator </h1>
<a href="index.php?action=rect"> Rectangle </a> ||
<a href="index.php?action=triangle">3 Angle </a>
</center>
<hr><br>
<?php
error_reporting(E_ALL & ~E_NOTICE);
// Set the class files required to automatically load this program
function __autoload($classname){
include strtolower($classname).".class.php";
}
// Determine whether the user has selected to click 1 Shape links
if(!empty($_GET['action'])) {
// No. 1 1 Step: Create an object for the shape
$classname = ucfirst($_GET['action']);
$shape=new $classname($_POST);
// No. 1 2 Step: Invoke the interface in the object of the shape view()
$shape -> view();
// No. 1 3 Step: Does the user submit the form corresponding to the graphical interface
if(isset($_POST['dosubmit'])) {
// No. 1 4 Step: Check whether the data output by the user is correct , Prompt for failure
if($shape->yan($_POST)) {
// Calculate the perimeter and area of a figure
echo $shape->name." The perimeter of is: ".$shape->zhou()."<br>";
echo $shape->name." The area of is: ".$shape->area()."<br>";
}
}
// If the user does not click the link, The main program is accessed by default
}else {
echo " Please select 1 Graphics to be calculated !<br>";
}
?>
</body>
</html>
Abstract classes for shapes:
abstract class Shape{
// Name of the shape
public $name;
// Method for calculating area of shape
abstract function area();
// Method for calculating perimeter of shape
abstract function zhou();
// Graphical form interface of shape
abstract function view();
// Verification method of shape
abstract function yan($arr);
}
3 angular calculation class file:
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 = "3 Angle ";
}
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.' No. 1 1 A side :<input type="text" name="bian1" value="'.$_POST['bian1'].'" /><br>';
$form .= $this->name.' No. 1 2 A side :<input type="text" name="bian2" value="'.$_POST['bian2'].'" /><br>';
$form .= $this->name.' No. 1 3 A side :<input type="text" name="bian3" value="'.$_POST['bian3'].'" /><br>';
$form .= '<input type="submit" name="dosubmit" value=" Calculation "><br>';
$form .='<form>';
echo $form;
}
function yan($arr) {
$bj = true;
if($arr['bian1'] < 0) {
echo " No. 1 1 Edge cannot be less than 0!<br>";
$bj = false;
}
if($arr['bian2'] < 0) {
echo " No. 1 2 Edge cannot be less than 0!<br>";
$bj = false;
}
if($arr['bian3'] < 0) {
echo " No. 1 3 Edge cannot be less than 0!<br>";
$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 both sides must be greater than the first 3 A side ";
$bj = false;
}
return $bj;
}
}
Rectangular 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 2*($this->width + $this->height);
}
function view() {
$form = '<form action="index.php?action=rect" method="post">';
$form .= $this->name.' Width of :<input type="text" name="width" value="'.$_POST['width'].'" /><br>';
$form .= $this->name.' The height of :<input type="text" name="height" value="'.$_POST['height'].'" /><br>';
$form .= '<input type="submit" name="dosubmit" value=" Calculation "><br>';
$form .='<form>';
echo $form;
}
function yan($arr) {
$bg = true;
if($arr['width'] < 0) {
echo $this->name." The width of cannot be less than 0!<br>";
$bg = false;
}
if($arr['height'] < 0) {
echo $this->name." The height of cannot be less than 0!<br>";
$bg = false;
}
return $bg;
}
}