Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- error_reporting(1);
- class RationalChislo {
- public $a = 0;
- public $b = 0;
- public function __construct ($a, $b){
- if(is_numeric($a)) {
- $this->a = $a;
- }
- if(is_numeric($b) && ($b != 0)) {
- $this->b = $b;
- }
- }
- }
- class RationalCalculator {
- public function add($x, $y) {
- if(is_a($x, 'RationalChislo') && is_a($y, 'RationalChislo')){
- $Temp = new RationalChislo();
- if( $x->b == $y->b )
- {
- $Temp->a = $x->a + $y->a;
- $Temp->b = $x->b;
- }
- else
- {
- $Temp->b = $x->b * $y->b;
- $Temp->a = ($Temp->b / $x->b * $x->a ) + ($Temp->b / $y->b * $y->a );
- }
- $t1 = $Temp->a;
- $t2 = $Temp->b;
- $nod = Nod($t1, $t2);
- $Temp->a = $Temp->a / $nod;
- $Temp->b = $Temp->b / $nod;
- return $Temp;
- }
- else return null;
- }
- public function sub($x, $y) {
- if(is_a($x, 'RationalChislo') && is_a($y, 'RationalChislo')) {
- $Temp = new RationalChislo();
- if( $x->b == $y->b )
- {
- $Temp->a = $x->a - $y->a;
- $Temp->b = $x->b;
- }
- else
- {
- $Temp->b = $x->b * $y->b;
- $Temp->a = ($Temp->b / $x->b * $x->a ) - ($Temp->b / $y->b * $y->a );
- }
- return $Temp;
- }
- else return null;
- }
- public function mult($x, $y) {
- if(is_a($x, 'RationalChislo') && is_a($y, 'RationalChislo')){
- $Temp = new RationalChislo();
- $Temp->a = $x->a * $y->a;
- $Temp->b = $x->b * $y->b;
- return $Temp;
- }
- else return null;
- }
- public function div($x, $y) {
- if(is_a($x, 'RationalChislo') && is_a($y, 'RationalChislo')){
- $Temp = new RationalChislo();
- $Temp->a = $x->a * $y->b;
- $Temp->b = $x->b * $y->a;
- return $Temp;
- }
- else return null;
- }
- public function Nod($x, $y) {
- while ($x != $y) {
- if ($x > $y) $x = $x - $y;
- else $y = $y - $x;
- }
- return $y;
- }
- /*public function reduce($q){
- $nod = NOD($q->a,$->$b);
- $q->a = $q->a/$nod;
- $q->b = $q->b/$nod;
- return $q;
- }*/
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment