Ну, как-то так
<?php
class file
{
private $file;
public function __construct($file_name)
{
$this->file = fopen($file_name, 'r');
}
public function read_file()
{
if($this->file){
while (!feof($this->file))
{
$file_str[] = fgets($this->file, 999);
}
}
else
{
echo "Ошибка открытия файла";
}
return $file_str;
}
public function decrypt_data()
{
foreach($this->read_file() as $str){
$parameters[] = explode(",",trim($str));
}
return $parameters;
}
}
abstract class figure
{
abstract public function P();
abstract public function S();
}
class Square extends figure
{
public $a;
public $type = __CLASS__;
public function __construct($a)
{
$this->a = $a;
}
public function P()
{
return 4*$this->a;
}
public function S()
{
return pow($this->a,2);
}
}
class Circle extends figure
{
public $r;
public $type = __CLASS__;
public function __construct($r)
{
$this->r = $r;
}
public function P()
{
return 2*pi()*$this->r;
}
public function S()
{
return pi() * pow($this->r,2);
}
}
class Triangle extends figure
{
private $a;
private $b;
private $c;
public $type = __CLASS__;
public function __construct($a,$b,$c)
{
$this->a = $a;
$this->b = $b;
$this->c = $c;
}
public function P()
{
$P = $this->a + $this->b + $this->c;
return $P;
}
public function H(){
$p = $this->P()/2;
return (2/$this->a)*sqrt($p * ($p-$this->a) * ($p-$this->b) * ($p - $this->c));
}
public function S()
{
return 0.5 * $this->a *$this->H();
}
}
$file = new file("Z:/home/localhost/www/test_for_job/figure.txt");
$file_date = $file->decrypt_data();
foreach($file_date as $str){
switch(strlen($str[0]))
{
case 7 : $figure[] = new square($str[1]); break;
case 4 : $figure[] = new circle($str[1]); break;
case 11 : $figure[] = new triangle($str[1],$str[2],$str[3]); break;
}
}
foreach($figure as $obj)
{
if ($obj instanceof Figure) {
echo $obj->type.", площадь = ".round($obj->S(),2).", периметр = ".round($obj->P(),2)."</br>";
}
}
Исходные данные:
Квадрат,22
Круг,10
Треугольник,20,10,15
Квадрат,10
Круг,100
Результат:
Square, площадь = 484, периметр = 88
Circle, площадь = 314.16, периметр = 62.83
Triangle, площадь = 72.62, периметр = 45
Square, площадь = 100, периметр = 40
Circle, площадь = 31415.93, периметр = 628.32