• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

按照书上的写了个计算图形面积的程序,但是不执行计算,请问哪里出错了

php 搞代码 4年前 (2022-01-23) 14次浏览 已收录 0个评论
文章目录[隐藏]

《细说php》第8章的最后一个例子。可以创建表单,但是点击计算之后出错误。请各位大侠指点迷津
接口:class__Shape.php
<?php
interface Shape{
function area();
function perimeter();
}
?>
矩形:class__Rect.php
<?php
class Rect implements Shape{
private $width;
private $length;

function __construct($size=””){
$this->width=$size[“width”];
$this->length=$size[“length”];
}
function area(){
return $this->length * $this->width;
}
function perimeter(){
return 2 * ($this->width + $this->length);
}
}
?>
三角形:class__Triangle.php
<?php
class Triangle implements Shape{
private $length1;
private $length2;
private $length3;

function __construct($size=””){
$this->length1 = $size[“length1”];
$this->length2 = $size[“length2”];
$this->length3 = $size[“length3”];
}
function area(){
$s = ($this->length1 + $this->length2 + $this->length3)/2;
return sqrt($s * ($s – $this->length1) * ($s – $this->length2) * ($s – $this->length3));
}
function perimeter(){
return $this->length1 + $this->length2 + $this->length3;
}
}
?>
圆形:class__Circle.php
<?php
class Circle implements Shape{
private $radius;

function __construce($size=””){
$this->radius = $size[“radius”];
}
function area(){
return pi() * $this->radius * $this->radius;
}
function perimeter(){
return 2* pi() * $this->radius;
}
}
?>
表格:class__Form.php
<?php
class Form{
private $formName;
private $request;
private $action;
private $method;
private $target;

function __construct($formName,$request,$action=”index.php”,$method=”get”,$target=”_self”){
$this->formName=$formName;
$this->request=$request;
$this->action=$action;
$this->method=$method;
$this->target=$target;
}
function __toString(){
$str=”

“;
$str.=”

“;来&源gao@dai!ma.com搞$代^码%网搞gaodaima代码
$str.=”

“;
break;
case 2:
$str.=”

“;
$str.=”

“;
$str.=”

“;
break;
case 3:
$str.=”

“;
break;
}
$str.=”

“;
$str.=”request[“action”].”>”;
$str.=”request[“action”].”>”;
$str.=”

“.$this->formName.”

“;
$str.=”action.”method=”.$this->method.”target=$this->target”;

switch ($this->request[“action”]){
case 1:
$str.=”

矩形长度:“;
$str.=”request[“length”].”>
矩形宽度:“;
$str.=”request[“width”].”>
三角形边长1:“;
$str.=”request[“length1″].”>
三角形边长2:“;
$str.=”request[“length2″].”>
三角形边长3:“;
$str.=”request[“length3″].”>
圆形半径:“;
$str.=”request[“radius”].”>

“;

return $str;
}
}
?>
控制程序:index.php

图形计算器

<body>
<?php
function __autoload($className){
include (‘class__’.ucfirst($className).’.php’);
}
?>

图形周长和面积计算器


矩形||
三角形||
圆形

<?php
switch ($_REQUEST[“action”]){
case 1:
$form=new Form(“矩形”,$_REQUEST,”index.php”);
echo $form;
break;
case 2:
$form=new Form(“三角形”,$_REQUEST,”index.php”);
echo $form;
break;
case 3:
$form=new Form(“圆形”,$_REQUEST,”index.php”);
echo $form;
break;
default:
echo “请选择一个图形
“;
}

if(isset($_REQUEST[“act”])){
switch($_REQUEST[“act”]){
case 1:
$shape=new Rect($_REQUEST);
break;
case 2:
$shape=new Triangle($_REQUEST);
break;
case 3:
$shape=new Circle($_REQUEST);
break;
}
echo “面积为:”.$shape->area().”
“;
echo “周长为:”.$shape->perimeter().”
“;
}
?>


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:按照书上的写了个计算图形面积的程序,但是不执行计算,请问哪里出错了

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址