Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. <?php
  2.  
  3. interface Geometry {
  4. public function getValue ();
  5. public function getMass ();
  6. }
  7.  
  8. class Paral implements Geometry {
  9. protected $a;
  10. protected $b;
  11. protected $c;
  12. protected $density;
  13.  
  14. public function __construct($a, $b, $c, $density){
  15. $this->a = $a;
  16. $this->b = $b;
  17. $this->c = $c;
  18. $this->density = $density;
  19. }
  20.  
  21. public function getValue (){
  22. return $this->a*$this->b*$this->c;
  23. }
  24.  
  25. public function getMass (){
  26. return $this->getValue() * $this->density;
  27. }
  28.  
  29. public function __toString(){
  30. return (string)($this->getMass());
  31. }
  32. }
  33.  
  34. class Cube extends Paral implements Geometry {
  35. public function __construct($a, $density){
  36. $this->a = $a;
  37. $this->b = $a;
  38. $this->c = $a;
  39. $this->density = $density;
  40. }
  41. public function __toString(){
  42. return (string) parent::__toString();
  43. }
  44. }
  45.  
  46. class Sfera implements Geometry {
  47. private $r;
  48. private $density;
  49.  
  50. public function __construct($r, $density){
  51. $this->r = $r;
  52. $this->density = $density;
  53. }
  54.  
  55. public function getValue (){
  56. return 4/3*(3.14*$this->r*$this->r*$this->r);
  57. }
  58.  
  59. public function getMass (){
  60. return $this->getValue() * $this->density;
  61.  
  62. }
  63.  
  64. public function __toString(){
  65. return (string)($this->getMass());
  66. }
  67.  
  68. }
  69.  
  70. class Pyramid implements Geometry {
  71. private $a;
  72. private $b;
  73. private $c;
  74. private $h;
  75. private $density;
  76.  
  77. public function __construct($a, $b, $c, $h, $density){
  78. $this->a = $a;
  79. $this->b = $b;
  80. $this->c = $c;
  81. $this->h = $h;
  82. $this->density = $density;
  83. }
  84.  
  85. public function getValue (){
  86. $a = $this->a;
  87. $b = $this->b;
  88. $c = $this->c;
  89. $h = $this->h;
  90. $p = ($a+$b+$c)/2;
  91. $s = sqrt($p * ($p - $a) * ($p - $b)*($p - $c));
  92. return ($s*$h)/3;
  93. }
  94.  
  95. public function getMass (){
  96. return $this->getValue() * $this->density;
  97. }
  98.  
  99. public function __toString(){
  100. return (string)($this->getMass());
  101. }
  102. }
  103.  
  104.  
  105. $paral = new Paral(5,6,7,0.1);
  106. print "Обем и масса паралелепипеда:" . "\n";
  107. print $paral->getValue() . "\n";
  108. print $paral->getMass() . "\n";
  109. //print $paral;
  110.  
  111. $cube = new Cube(5,0.1);
  112. print "Обем и масса куба:" . "\n";
  113. print $cube->getValue() . "\n";
  114. print $cube->getMass() . "\n";
  115. //print $cube;
  116.  
  117. $sfera = new Sfera(3,0.1);
  118. print "Обем и масса сферы:" . "\n";
  119. print $sfera->getValue() . "\n";
  120. print $sfera->getMass() . "\n";
  121.  
  122. $pyramid = new Pyramid(2, 3, 4, 7, 0.1);
  123. print "Обем и масса пирамиды:" . "\n";
  124. print $pyramid->getValue() . "\n";
  125. print $pyramid->getMass() . "\n";
  126.  
  127. print "_______________________" . "\n" ;
  128. print "Рандомный выбор фигуры: \n";
  129.  
  130. $figures = array(); //Рандомный выбор фигуры
  131. $figuresMass = array();
  132. for ($i = 0; $i < 4; $i++){
  133. $figures[$i]=rand(0, 3);
  134. switch ($figures[$i]) {
  135. case 0:
  136. $figures[$i]=new Paral(5,6,7,0.1);
  137. $figuresMass[$i] = $figures[$i]->getMass();
  138. break;
  139. case 1:
  140. $figures[$i]=new Cube(5,0.1);
  141. $figuresMass[$i] = $figures[$i]->getMass();
  142. break;
  143. case 2:
  144. $figures[$i]=new Sfera(3,0.1);
  145. $figuresMass[$i] = $figures[$i]->getMass();
  146. break;
  147. case 3:
  148. $figures[$i]=new Pyramid(2, 3, 4, 7, 0.1);
  149. $figuresMass[$i] = $figures[$i]->getMass();
  150. break;
  151. };
  152. print $i . " => ";
  153. print ($figuresMass[$i]);
  154. print "\n" ;
  155. }
  156.  
  157. print "_______________________" . "\n" ;
  158. print "Сортированный массив: \n";
  159.  
  160. sort($figuresMass);
  161.  
  162. foreach ($figuresMass as $key => $value) {
  163. echo $key." => ".$value ."\n";
  164. }
  165.  
  166. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement