feat: Finish part - 4
parent
dd46a13570
commit
342b442622
@ -0,0 +1,69 @@
|
|||||||
|
void basic_classes() {
|
||||||
|
var n1 = Complex(3, -4);
|
||||||
|
var n2 = Complex(5, 2);
|
||||||
|
|
||||||
|
print(n1 + n2);
|
||||||
|
print(n1.multiply(n2));
|
||||||
|
print(Complex.substract(n1, n2));
|
||||||
|
}
|
||||||
|
|
||||||
|
class Complex {
|
||||||
|
// Private property(member)
|
||||||
|
num _real;
|
||||||
|
num _imaginary;
|
||||||
|
|
||||||
|
// Getters & Setters
|
||||||
|
get real => _real;
|
||||||
|
set real(num newReal) => _real = newReal;
|
||||||
|
|
||||||
|
get imaginary => _imaginary;
|
||||||
|
set imaginary(num newImaginary) => _imaginary = newImaginary;
|
||||||
|
|
||||||
|
// Constructors
|
||||||
|
Complex(this._real, this._imaginary);
|
||||||
|
Complex.real(num real) : this(real, 0);
|
||||||
|
Complex.imaginary(num imaginary) : this(0, imaginary);
|
||||||
|
|
||||||
|
Complex operator +(Complex c) {
|
||||||
|
return Complex(this.real + c.real, this.imaginary + c.imaginary);
|
||||||
|
}
|
||||||
|
|
||||||
|
Complex operator *(Complex c) {
|
||||||
|
return Complex(
|
||||||
|
(this.real * c.real) - (this.imaginary * c.imaginary),
|
||||||
|
(this.real * c.imaginary) + (this.imaginary * c.real),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Complex multiply(Complex c) {
|
||||||
|
return Complex(
|
||||||
|
(this.real * c.real) - (this.imaginary * c.imaginary),
|
||||||
|
(this.real * c.imaginary) + (this.imaginary * c.real),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Complex substract(Complex c1, Complex c2) {
|
||||||
|
return Complex(
|
||||||
|
c1.real - c2.real,
|
||||||
|
c1.imaginary - c2.imaginary,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(dynamic other) {
|
||||||
|
if (other is! Complex) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return real == other.real && imaginary == other.imaginary;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
if (imaginary >= 0) {
|
||||||
|
return "$real + ${imaginary}i";
|
||||||
|
} else {
|
||||||
|
return "$real - ${imaginary.abs()}i";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue