-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.php
83 lines (74 loc) · 2.21 KB
/
number.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
interface Number {
public function add( $a );
public function subtract( $a );
public function multiply( $a );
public function divide( $a );
}
class Real implements Number {
public $value;
function __construct ( $real ) {
$this->value = $real;
}
public function add ( $a ) {
}
public function subtract( $a ) {
}
public function multiply( $a ) {
}
public function divide( $a ) {
}
}
class Integer implements Number {
public $value;
function __construct ( $int ) {
$this->value = $int;
}
public function add ( $a ) {
return $this->value + $a;
}
public function subtract( $a ) {
return $this->value - $a;
}
public function multiply( $a ) {
return $this->value * $a;
}
public function divide( $a ) {
return $this->value / $a;
}
}
class Fraction implements Number {
public $numerator;
public $denominator;
function __construct ( $numerator, $denominator ) {
$this->numerator = $numerator;
$this->denominator = $denominator;
}
public function GCD( $a, $b ) {
if ( $b == 0 ) {
return $a;
}
return GCD( $b, $a % $b );
}
public function LCM( $a, $b ) {
return abs( $a * $b )/GCD( $a, $b );
}
public function add ( $f ) {
$numerator = $f->denominator;
$numerator = $numerator->multiply( $this->numerator );
$temp = $this->denominator;
$temp - $temp->multiply( $f->numerator );
$numerator = $numerator->add( $temp );
$denominator = $f->denominator;
$denominator = $denominator->multiply( $this->denominator );
return new Fraction ( $numerator, $denominator );
}
public function subtract( $a ) {
}
public function multiply( $a ) {
return new Fraction( $this->numerator->multiply
}
public function divide( $a ) {
}
}
?>