set(2,3);
$f2->set(3,4);
$f1->add($f2);
print $f1 . "
\n";
$fa = new Fraction;
$fb = new Fraction;
$w = new Weird;
$fa->set(1,2); # set fa to be 1/2.
$fb->set(2,10); # set fb to be 2/10 (or 1/5)
$fb->mul($fa); # multiply fa * fb, making fa now be 2/20 (or 1/10)
$fb->mul($w);
print $fb . "
";
print $fb->decimal(); # print 0.1 and not 1/10.
class Weird
{
var $top;
var $bot;
function __construct()
{
$this->top = 1;
$this->bot = "Two";
}
}
class Fraction {
var $top, $bot; # Instance variables
function mul($other)
{
if (is_integer($other))
$this->top *= $other;
else
{
$this->top = $this->top * $other->top;
$this->bot = $this->bot * $other->bot;
}
}
# Constructor
function __construct() {
}
function add($frac2) {
$this->set($this->top*$frac2->bot + $frac2->top * $this->bot, $this->bot * $frac2->bot);
}
function set($top, $bot) {
$this->top = $top;
$this->bot = $bot;
}
function __toString() {
return "$this->top / $this->bot";
}
}
?>