You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.5 KiB
Dart
70 lines
1.5 KiB
Dart
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";
|
|
}
|
|
}
|
|
}
|