Complex conjugation

main
mickenordin 9 years ago
parent b2b1733fec
commit bdc98ba3c2

@ -6,6 +6,12 @@ int main() {
cnumber a(1, -3); cnumber a(1, -3);
cnumber b(2, 5); cnumber b(2, 5);
cout << "a = " << a << " b = " << b << " a + b = " << a + b << endl; cout << "a = " << a << endl;
cout << "a = " << a << " b = " << b << " a * b = " << a * b << endl; cout << "a* = " << a.conjugate() << endl;
cout << "b = " << b << endl;
cout << "b* = " << b.conjugate() << endl;
cout << "a + b = " << a + b << endl;
cout << "(a + b)* = " << (a + b).conjugate() << endl;
cout << "a * b = " << a * b << endl;
cout << "(a * b)* = " << (a * b).conjugate() << endl;
} }

@ -11,6 +11,7 @@ class cnumber {
} }
public: public:
// Constructor
cnumber(int a, int b) { cnumber(int a, int b) {
r = a; r = a;
i = b; i = b;
@ -18,6 +19,14 @@ class cnumber {
signi = get_sign(i); signi = get_sign(i);
} }
// Member functions
cnumber conjugate() {
cnumber z(this->r, this->i * -1);
return z;
}
// Operators
cnumber operator+(const cnumber &that) { cnumber operator+(const cnumber &that) {
cnumber z(that.r + this->r, that.i + this->i); cnumber z(that.r + this->r, that.i + this->i);
return z; return z;
@ -31,7 +40,12 @@ class cnumber {
os << z.r << (z.signi ? '+' : '\0' ) << z.i << 'i'; os << z.r << (z.signi ? '+' : '\0' ) << z.i << 'i';
return os; return os;
} }
void operator=(const cnumber &z ) {
r = z.r;
i = z.i;
signr = z.signr;
signi = z.signi;
}
}; };

Loading…
Cancel
Save