运用PHP面向对象的知识设计一个图形计算器,同时也运用到了抽象类知识,这个计算器可以计算三角形的周长和面积以及矩形的周长和面积。本图形计算器有4个页面:1.PHP图形计算器主页index.php; 2.形状的抽象类shape.class.php; 3三角形计算类triangle.class.php; 4.矩形计算类rect.class.php。
PHP图形计算器代码点击下载: php图形计算器.zip
代码分别如下:
PHP图形计算器主页:
<html>
<head>
<title>简单的图形计算器</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<center>
<h1>简单的图形计算器</h1>
<a href="index.php">矩形</a> ||
<a href="index.php">三角形</a>
</center>
<hr><br>
<".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>";
}
"htmlcode">
abstract class Shape{
//形状的名称
public $name;
//形状的计算面积方法
abstract function area();
//形状的计算周长的方法
abstract function zhou();
//形状的图形表单界面
abstract function view();
//形状的验证方法
abstract function yan($arr);
}
三角形计算类文件:
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" 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;
}
}
矩形计算类文件:
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" 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;
}
}
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
内蒙古资源网 Copyright www.nmgbbs.com
暂无“PHP实现的简单三角形、矩形周长面积计算器分享”评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。