razionale.php

<?php

class razionale {
    var 
$num;
    var 
$den;
    
    Function 
razionale ($num 0$den 1)
    {
        
$this->num $num;
        
$this->den $den;
    }
    
    Function 
gcd($x$y)
    {
        
$a abs($x);
        
$b abs($y);
  
        if (
$b $a) {
            
$tmp $a;
            
$a $b;
            
$b $tmp;
        }
        for(;;) {
            if (
$b == 0)
                return 
$a;
            else if (
$b == 1)
                return 
$b;
            else {
                
$tmp $b;
                
$b $a $b;
                
$a $tmp;
            }
        }
    }

    Function 
normalize ()
    {
        
$s $this->sign_int($this->den);
        if (
$s == 0)
            
printf ("Zero denominator.");
        else if (
$s 0) {
            
$this->den = -$this->den;
            
$this->num = -$this->num;
        }

        
$g $this->gcd($this->num$this->den);
        if ( 
$g != 1) {
            
$this->num /= $g;
            
$this->den /= $g;
        }
        return 
$this;
    }
    
    Function 
sign_int ($i)
    {
        if (
$i>0)
            return 
1;
        if (
$i<0)
            return -
1;
        return 
0;
    }
    
    Function 
sign()
    {
        if (
$this->num 0)
            return 
1;
        if (
$this->num 0)
            return -
1;
        return 
0;
    }
    
    Function 
addfrac ($x$y)
    {
        
$somma = new razionale;
        if (
$x->num == 0) {
            
$this->num $y->num;
            
$this->den $y->den;
        } else if (
$y->num() == 0) {
            
$this->num $x->num;
            
$this->den $x->den;
        } else {
            
$somma->num $x->num*$y->den $x->den*$y->num;
            
$somma->den $x->den*$y->den;
            
$this $somma->normalize();
        }
    }
    
    Function 
subfrac ($x$y)
    {
        
$differenza = new razionale;
        if (
$x->num == 0) {
            
$this->num = -$y->num;
            
$this->den $y->den;
        } else if (
$y->num == 0) {
            
$this->num $x->num;
            
$this->den $x->den;
        } else {
            
$differenza->num $x->num*$y->den $x->den*$y->num;
            
$differenza->den $x->den*$y->den;
            
$this $differenza->normalize();
        }
    }
    
    Function 
mulfrac ($x$y)
    {
        
$prodotto = new razionale;
        if (
$x->num == || $y->num == 0) {
            
$this->num 0;
            
$this->den 1;
        } else if (
$x->value() == 1) {
            
$this->num $y->num;
            
$this->den $y->den;
        } else if (
$y->value() == 1) {
            
$this->num $x->num;
            
$this->den $x->den;
        } else if (
$x->value() == -1) {
            
$this->num = -$y->num;
            
$this->den $y->den;
        } else if (
$y->value() == -1) {
            
$this->num = -$x->num;
            
$this->den $x->den;
        } else {
            
$prodotto->num $x->num*$y->num;
            
$prodotto->den $x->den*$y->den;
            
$this $prodotto->normalize ();
        }
    }

    Function 
divfrac ($x$y)
    {
        
$quoziente = new razionale;
        if (
$x->num == 0) {
            
$this->num 0;
            
$this->den 1;
        } else if (
$y->num == 0) {
            
printf ("Division by zero");
        } else {
            
$quoziente->num $x->num*$y->den;
            
$quoziente->den $x->den*$y->num;
            
$this $quoziente->normalize ();
        }
    }

    Function 
negatefrac ()
    {
        
$this->num = -$this->num;
    }

    Function 
invertfrac ()
    {
        
$tmp $this->num;
        
$this->num $this->den;
        
$this->den $tmp;
        
$s $this->sign_int ($this->den);
        if (
$s == 0)
            exit (
1);
        else if (
$s 0) {
            
$this->den = -$this->den;
            
$this->num = -$this->num;
        }
    }
    
    Function 
comparefrac ($x$y)
    {
        
$this->subfrac ($x,$y);
        return 
$this->sign_int ($this->num);
    } 

    Function 
scanfrac ($x)
    {
        
// Se c'e' uno slash e' espresso direttamente come frazione
        
if (strchr($x"/")) {
            
$num_den split ("/"$xstrlen($x));
            
$this->num $num_den[0];
            
$this->den $num_den[1];
            
        } else if (
strchr($x".")) { // e' espresso come decimale
            // facciamo fare il lavoro al PHP
            
$float $x;
            
settype($float"double");
            
// quante cifre dopo la virgola?
            
$decimali strlen (strchr ($x".")) - 1;
            
$this->den pow (10$decimali);
            
$this->num $float $this->den;
        } else { 
// e' un intero
            
$this->num $x;
            
$this->den 1;
        }
    }
    
    Function 
fractoa ()
    {
        if (
$this->den == 1)
            
$str sprintf ("%d"$this->num);
        else
            
$str sprintf ("%d/%d"$this->num$this->den);
        return 
$str;
    }
    
    Function 
num ()
    {
        return 
$this->num;
    }
    
    Function 
den ()
    {
        return 
$this->den;
    }
    
    Function 
value ()
    {
        return 
$this->num/$this->den;
    }
    
    Function 
set ($num$den)
    {
        
$this->num $num;
        
$this->den $den;
    }
}

?>