Spicke

Recional

Sep 27th, 2017
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1. <?
  2.     error_reporting(1);
  3.  
  4.     class RationalChislo {
  5.         public $a = 0;
  6.         public $b = 0;
  7.  
  8.         public function __construct ($a, $b){
  9.             if(is_numeric($a)) {
  10.                 $this->a = $a;
  11.             }
  12.             if(is_numeric($b) && ($b != 0)) {
  13.                 $this->b = $b;
  14.             }
  15.         }
  16.     }
  17.     class RationalCalculator {
  18.         public function add($x, $y) {
  19.             if(is_a($x, 'RationalChislo') && is_a($y, 'RationalChislo')){
  20.                 $Temp = new RationalChislo();
  21.                 if( $x->b == $y->b )
  22.                 {
  23.                     $Temp->a = $x->a + $y->a;
  24.                     $Temp->b = $x->b;
  25.                 }
  26.                 else
  27.                 {
  28.                     $Temp->b = $x->b * $y->b;
  29.                     $Temp->a = ($Temp->b / $x->b * $x->a ) + ($Temp->b / $y->b * $y->a );
  30.                 }
  31.                 $t1 = $Temp->a;
  32.                 $t2 = $Temp->b;
  33.                 $nod = Nod($t1, $t2);
  34.                 $Temp->a = $Temp->a / $nod;
  35.                 $Temp->b = $Temp->b / $nod;
  36.                 return $Temp;
  37.             }
  38.             else return null;
  39.         }
  40.         public function sub($x, $y) {
  41.             if(is_a($x, 'RationalChislo') && is_a($y, 'RationalChislo')) {
  42.                 $Temp = new RationalChislo();
  43.                 if( $x->b == $y->b )
  44.                 {
  45.                     $Temp->a = $x->a - $y->a;
  46.                     $Temp->b = $x->b;
  47.                 }
  48.                 else
  49.                 {
  50.                     $Temp->b = $x->b * $y->b;
  51.                     $Temp->a = ($Temp->b / $x->b * $x->a ) - ($Temp->b / $y->b * $y->a );
  52.                 }
  53.                 return $Temp;
  54.             }
  55.             else return null;
  56.         }
  57.  
  58.         public function mult($x, $y) {
  59.             if(is_a($x, 'RationalChislo') && is_a($y, 'RationalChislo')){
  60.                 $Temp = new RationalChislo();
  61.                 $Temp->a = $x->a * $y->a;
  62.                 $Temp->b = $x->b * $y->b;
  63.                 return $Temp;
  64.             }
  65.             else return null;
  66.         }
  67.  
  68.         public function div($x, $y) {
  69.             if(is_a($x, 'RationalChislo') && is_a($y, 'RationalChislo')){
  70.                 $Temp = new RationalChislo();
  71.                 $Temp->a = $x->a * $y->b;
  72.                 $Temp->b = $x->b * $y->a;
  73.                 return $Temp;
  74.             }
  75.             else return null;
  76.         }
  77.  
  78.         public function Nod($x, $y) {
  79.             while ($x != $y) {
  80.                 if ($x > $y) $x =  $x - $y;
  81.                 else $y = $y - $x;
  82.             }
  83.             return $y;
  84.         }
  85.  
  86.         /*public function reduce($q){
  87.             $nod = NOD($q->a,$->$b);
  88.             $q->a = $q->a/$nod;
  89.             $q->b = $q->b/$nod;
  90.             return $q;
  91.         }*/  
  92.     }
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment