From bdc98ba3c21a4bed35a97a5f19ff706bb1abc717 Mon Sep 17 00:00:00 2001 From: mickenordin Date: Sat, 18 Jun 2016 21:11:21 +0200 Subject: [PATCH] Complex conjugation --- cnumber.cpp | 10 ++++++++-- cnumber.hpp | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/cnumber.cpp b/cnumber.cpp index 868dfdf..068e724 100644 --- a/cnumber.cpp +++ b/cnumber.cpp @@ -6,6 +6,12 @@ int main() { cnumber a(1, -3); cnumber b(2, 5); - cout << "a = " << a << " b = " << b << " a + b = " << a + b << endl; - cout << "a = " << a << " b = " << b << " a * b = " << a * b << endl; + cout << "a = " << a << 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; } diff --git a/cnumber.hpp b/cnumber.hpp index d2ed41c..0b76a83 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -11,6 +11,7 @@ class cnumber { } public: + // Constructor cnumber(int a, int b) { r = a; i = b; @@ -18,6 +19,14 @@ class cnumber { signi = get_sign(i); } + // Member functions + cnumber conjugate() { + cnumber z(this->r, this->i * -1); + return z; + } + + + // Operators cnumber operator+(const cnumber &that) { cnumber z(that.r + this->r, that.i + this->i); return z; @@ -31,7 +40,12 @@ class cnumber { os << z.r << (z.signi ? '+' : '\0' ) << z.i << 'i'; return os; } - + void operator=(const cnumber &z ) { + r = z.r; + i = z.i; + signr = z.signr; + signi = z.signi; + } };