From 271d186d27e1faddead3f135ad6cb44c20a4261c Mon Sep 17 00:00:00 2001 From: mickenordin Date: Sat, 18 Jun 2016 19:15:56 +0200 Subject: [PATCH 01/17] First draft of complex number --- cnumber.cpp | 9 +++++++++ cnumber.hpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 cnumber.cpp create mode 100644 cnumber.hpp diff --git a/cnumber.cpp b/cnumber.cpp new file mode 100644 index 0000000..738e42e --- /dev/null +++ b/cnumber.cpp @@ -0,0 +1,9 @@ +#include +#include "cnumber.hpp" + + +int main() { + cnumber z(1, -7); + + cout << z << endl; +} diff --git a/cnumber.hpp b/cnumber.hpp new file mode 100644 index 0000000..33ceb3b --- /dev/null +++ b/cnumber.hpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +class cnumber { + private: + int r, i; + bool signr, signi; + bool get_sign(int i) { + return (i >= 0); + } + + public: + cnumber(int a, int b) { + r = a; + i = b; + signr = get_sign(r); + signi = get_sign(i); + + } + + friend ostream &operator<<( ostream &os, const cnumber &z ) { + os << z.r << (z.signi ? '+' : '-') << 'i' << (z.signi ? z.i : z.i * -1); + return os; + } + + +}; + From 0f2a7c753600089b42bfb9f896208135b95e9b5c Mon Sep 17 00:00:00 2001 From: mickenordin Date: Sat, 18 Jun 2016 19:41:57 +0200 Subject: [PATCH 02/17] more operators --- cnumber.cpp | 5 +++-- cnumber.hpp | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/cnumber.cpp b/cnumber.cpp index 738e42e..86a78b9 100644 --- a/cnumber.cpp +++ b/cnumber.cpp @@ -3,7 +3,8 @@ int main() { - cnumber z(1, -7); + cnumber a(-1, 4); + cnumber b(4, -7); - cout << z << endl; + cout << "a = " << a << " b = " << b << " a + b = " << a + b << endl; } diff --git a/cnumber.hpp b/cnumber.hpp index 33ceb3b..90b2c54 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -18,9 +18,13 @@ class cnumber { signi = get_sign(i); } + cnumber operator+(const cnumber& that) { + cnumber z(that.r + this->r, that.i + this->i); + return z; + } friend ostream &operator<<( ostream &os, const cnumber &z ) { - os << z.r << (z.signi ? '+' : '-') << 'i' << (z.signi ? z.i : z.i * -1); + os << z.r << (z.signi ? '+' : '\0' ) << z.i << 'i'; return os; } From b2b1733fec263da708050275b4feaf08d684fedd Mon Sep 17 00:00:00 2001 From: mickenordin Date: Sat, 18 Jun 2016 20:21:18 +0200 Subject: [PATCH 03/17] Also multiplication --- cnumber.cpp | 5 +++-- cnumber.hpp | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/cnumber.cpp b/cnumber.cpp index 86a78b9..868dfdf 100644 --- a/cnumber.cpp +++ b/cnumber.cpp @@ -3,8 +3,9 @@ int main() { - cnumber a(-1, 4); - cnumber b(4, -7); + 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; } diff --git a/cnumber.hpp b/cnumber.hpp index 90b2c54..d2ed41c 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -18,10 +18,14 @@ class cnumber { signi = get_sign(i); } - cnumber operator+(const cnumber& that) { + cnumber operator+(const cnumber &that) { cnumber z(that.r + this->r, that.i + this->i); return z; } + cnumber operator*(const cnumber &that) { + cnumber z( (that.r * this->r ) - (that.i * this->i) , (that.r * this->i) + (this->r * that.i) ); + return z; + } friend ostream &operator<<( ostream &os, const cnumber &z ) { os << z.r << (z.signi ? '+' : '\0' ) << z.i << 'i'; From bdc98ba3c21a4bed35a97a5f19ff706bb1abc717 Mon Sep 17 00:00:00 2001 From: mickenordin Date: Sat, 18 Jun 2016 21:11:21 +0200 Subject: [PATCH 04/17] 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; + } }; From 8892c7851f800cc28759c18750fcd80ac6eb703e Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 18 Apr 2020 10:25:05 +0200 Subject: [PATCH 05/17] Better output for << operator --- cnumber.hpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cnumber.hpp b/cnumber.hpp index 0b76a83..81b0e29 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -37,7 +37,23 @@ class cnumber { } friend ostream &operator<<( ostream &os, const cnumber &z ) { - os << z.r << (z.signi ? '+' : '\0' ) << z.i << 'i'; + if (z.r != 0) { + os << z.r; + if (z.signi && (z.i != 0)) { + os << '+'; + } + } + if (z.i != 0) { + if ((z.i != 1) && (z.i != -1)) { + os << z.i; + } + else { + if (! z.signi) { + os << '-'; + } + } + os << 'i'; + } return os; } void operator=(const cnumber &z ) { From 523f2fc0b6acff214a03d08f96f86926559d22fe Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 18 Apr 2020 10:37:53 +0200 Subject: [PATCH 06/17] Add subtraction --- cnumber.hpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cnumber.hpp b/cnumber.hpp index 81b0e29..09e7e79 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -28,7 +28,11 @@ class cnumber { // Operators cnumber operator+(const cnumber &that) { - cnumber z(that.r + this->r, that.i + this->i); + cnumber z(this->r + that.r, this->i + that.i); + return z; + } + cnumber operator-(const cnumber &that) { + cnumber z(this->r - that.r, this->i - that.i); return z; } cnumber operator*(const cnumber &that) { From 23d1ea1faa9c1c6b8f5f08f2fb6e0fe47ee14796 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 18 Apr 2020 11:16:20 +0200 Subject: [PATCH 07/17] Add division --- cnumber.cpp | 10 ++++++++-- cnumber.hpp | 28 ++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/cnumber.cpp b/cnumber.cpp index 068e724..33e8818 100644 --- a/cnumber.cpp +++ b/cnumber.cpp @@ -3,15 +3,21 @@ int main() { - cnumber a(1, -3); - cnumber b(2, 5); + cnumber a(3,2); + cnumber b(4,-3); cout << "a = " << a << endl; cout << "a* = " << a.conjugate() << endl; + cout << "a*a* = " << a * a.conjugate() << endl; cout << "b = " << b << endl; cout << "b* = " << b.conjugate() << endl; + cout << "b*b* = " << 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; 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 09e7e79..d289e33 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -4,7 +4,7 @@ using namespace std; class cnumber { private: - int r, i; + float r, i; bool signr, signi; bool get_sign(int i) { return (i >= 0); @@ -18,9 +18,23 @@ class cnumber { signr = get_sign(r); signi = get_sign(i); + } + cnumber(float a, float b) { + r = a; + i = b; + signr = get_sign(r); + signi = get_sign(i); + + } + cnumber(const cnumber &z) { + r = z.r; + i = z.i; + signr = z.signr; + signi = z.signi; + } // Member functions - cnumber conjugate() { + cnumber conjugate() const { cnumber z(this->r, this->i * -1); return z; } @@ -36,14 +50,20 @@ class cnumber { return z; } cnumber operator*(const cnumber &that) { - cnumber z( (that.r * this->r ) - (that.i * this->i) , (that.r * this->i) + (this->r * that.i) ); + cnumber z( (this->r * that.r ) - (this->i * that.i) , (this->r * that.i) + (that.r * this->i) ); return z; } + cnumber operator/(const cnumber &that) { + cnumber numerator( (this->r * that.r ) - (this->i * ( that.i * -1)) , (this->r * (that.i * -1)) + (that.r * this->i) ); + cnumber denominator( (that.r * that.r ) - (that.i * ( that.i * -1)) , (that.r * (that.i * -1)) + (that.r * that.i) ); + cnumber ratio(numerator.r / denominator.r, numerator.i / denominator.r); + return ratio; + } friend ostream &operator<<( ostream &os, const cnumber &z ) { if (z.r != 0) { os << z.r; - if (z.signi && (z.i != 0)) { + if (z.signi && (z.i > 0)) { os << '+'; } } From b914c09fa6f5515790d62c5e4304c8c4ecfae48a Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 18 Apr 2020 16:49:11 +0200 Subject: [PATCH 08/17] Use my own fractions instead --- README.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..c647655 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +git clone https://github.com/mickenordin/fractions.git +git clone https://github.com/mickenordin/complex-numbers.git +cd complex-numbers +g++ -o cnumber cnumber.cpp +./cnumber From 641c03b0a4158fd99e2f1bc17774bf56afa56c91 Mon Sep 17 00:00:00 2001 From: Mikael Nordin Date: Sat, 18 Apr 2020 16:54:54 +0200 Subject: [PATCH 09/17] Update README.md --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c647655..b36a673 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ -git clone https://github.com/mickenordin/fractions.git -git clone https://github.com/mickenordin/complex-numbers.git -cd complex-numbers +```bash +git clone https://github.com/mickenordin/fractions.git` +git clone https://github.com/mickenordin/complex-numbers.git` +cd complex-numbers g++ -o cnumber cnumber.cpp ./cnumber +``` From 2590da865a923ad0b04391d97dadf927b9a0b3de Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 18 Apr 2020 16:57:06 +0200 Subject: [PATCH 10/17] Use my own fractions --- cnumber.hpp | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/cnumber.hpp b/cnumber.hpp index d289e33..4e5527e 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -1,59 +1,76 @@ #include +#include "../fractions/fractions.hpp" using namespace std; class cnumber { private: - float r, i; + fraction r, i; bool signr, signi; - bool get_sign(int i) { - return (i >= 0); + bool get_sign(const fraction &q) { + return q.sign(); } public: // Constructor - cnumber(int a, int b) { + cnumber(const fraction &a, const fraction &b) { r = a; i = b; signr = get_sign(r); signi = get_sign(i); - } - cnumber(float a, float b) { - r = a; - i = b; + cnumber(int a, int b) { + fraction qa(a); + fraction qb(b); + r = qa; + i = qb; + signr = get_sign(r); + signi = get_sign(i); + } + cnumber(double a, double b) { + fraction qa(a); + fraction qb(b); + r = qa; + i = qb; signr = get_sign(r); signi = get_sign(i); - } cnumber(const cnumber &z) { r = z.r; i = z.i; signr = z.signr; signi = z.signi; - } // Member functions cnumber conjugate() const { - cnumber z(this->r, this->i * -1); + cnumber z(fraction(this->r.get_n(),this->r.get_d()),fraction(this->i.get_n() * -1, this->i.get_d())); return z; } - // Operators - cnumber operator+(const cnumber &that) { + cnumber operator+(const cnumber &that) const { cnumber z(this->r + that.r, this->i + that.i); return z; } - cnumber operator-(const cnumber &that) { + cnumber operator-(const cnumber &that) const { cnumber z(this->r - that.r, this->i - that.i); return z; } - cnumber operator*(const cnumber &that) { + cnumber operator*(const int i) const { + cnumber that(i,0); + cnumber z( *this * that); + return z; + } + cnumber operator*(const fraction &dec) const { + cnumber that(dec,0); + cnumber z( *this * that); + return z; + } + cnumber operator*(const cnumber &that) const { cnumber z( (this->r * that.r ) - (this->i * that.i) , (this->r * that.i) + (that.r * this->i) ); return z; } - cnumber operator/(const cnumber &that) { + cnumber operator/(const cnumber &that) const { cnumber numerator( (this->r * that.r ) - (this->i * ( that.i * -1)) , (this->r * (that.i * -1)) + (that.r * this->i) ); cnumber denominator( (that.r * that.r ) - (that.i * ( that.i * -1)) , (that.r * (that.i * -1)) + (that.r * that.i) ); cnumber ratio(numerator.r / denominator.r, numerator.i / denominator.r); @@ -86,6 +103,5 @@ class cnumber { signr = z.signr; signi = z.signi; } - }; From 8fbe2e2950fd159fc9cf553d2cc22eff4595ad74 Mon Sep 17 00:00:00 2001 From: Mikael Nordin Date: Sat, 18 Apr 2020 17:17:18 +0200 Subject: [PATCH 11/17] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b36a673..e70ec17 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ```bash -git clone https://github.com/mickenordin/fractions.git` -git clone https://github.com/mickenordin/complex-numbers.git` +git clone https://github.com/mickenordin/fractions.git +git clone https://github.com/mickenordin/complex-numbers.git cd complex-numbers g++ -o cnumber cnumber.cpp ./cnumber From 797347f4427f7a55d678a0986fd78915b513b2b7 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sun, 19 Apr 2020 22:56:30 +0200 Subject: [PATCH 12/17] Simplyfy a bit --- cnumber.cpp | 12 +++++------ cnumber.hpp | 61 ++++++++++++++--------------------------------------- 2 files changed, 22 insertions(+), 51 deletions(-) diff --git a/cnumber.cpp b/cnumber.cpp index 33e8818..8311fce 100644 --- a/cnumber.cpp +++ b/cnumber.cpp @@ -12,12 +12,12 @@ int main() { cout << "b = " << b << endl; cout << "b* = " << b.conjugate() << endl; cout << "b*b* = " << 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; cout << "a - b = " << a - b << endl; - cout << "(a - b)* = " << (a - b).conjugate() << endl; + cout << "(a - b)* = " << (a - 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; + 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 4e5527e..ee7b63c 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -5,45 +5,27 @@ using namespace std; class cnumber { private: - fraction r, i; - bool signr, signi; - bool get_sign(const fraction &q) { - return q.sign(); - } + fraction r,i; public: // Constructor cnumber(const fraction &a, const fraction &b) { r = a; i = b; - signr = get_sign(r); - signi = get_sign(i); } cnumber(int a, int b) { - fraction qa(a); - fraction qb(b); - r = qa; - i = qb; - signr = get_sign(r); - signi = get_sign(i); + r = a; + i = b; } cnumber(double a, double b) { - fraction qa(a); - fraction qb(b); - r = qa; - i = qb; - signr = get_sign(r); - signi = get_sign(i); - } - cnumber(const cnumber &z) { - r = z.r; - i = z.i; - signr = z.signr; - signi = z.signi; + r = a; + i = b; } // Member functions cnumber conjugate() const { - cnumber z(fraction(this->r.get_n(),this->r.get_d()),fraction(this->i.get_n() * -1, this->i.get_d())); + fraction a(this->r.get_n(), this->r.get_d()); + fraction b(this->i.get_n(), this->i.get_d()); + cnumber z(a,b * -1); return z; } @@ -56,20 +38,14 @@ class cnumber { cnumber z(this->r - that.r, this->i - that.i); return z; } - cnumber operator*(const int i) const { - cnumber that(i,0); - cnumber z( *this * that); - return z; - } - cnumber operator*(const fraction &dec) const { - cnumber that(dec,0); - cnumber z( *this * that); - return z; - } cnumber operator*(const cnumber &that) const { - cnumber z( (this->r * that.r ) - (this->i * that.i) , (this->r * that.i) + (that.r * this->i) ); + cnumber z( (that.r * this->r ) - (that.i * this->i) , (that.r * this->i) + (this->r * that.i) ); return z; } + cnumber operator*(const fraction &q) const { + cnumber that(q,fraction(0)); + return *this * that; + } cnumber operator/(const cnumber &that) const { cnumber numerator( (this->r * that.r ) - (this->i * ( that.i * -1)) , (this->r * (that.i * -1)) + (that.r * this->i) ); cnumber denominator( (that.r * that.r ) - (that.i * ( that.i * -1)) , (that.r * (that.i * -1)) + (that.r * that.i) ); @@ -80,19 +56,16 @@ class cnumber { friend ostream &operator<<( ostream &os, const cnumber &z ) { if (z.r != 0) { os << z.r; - if (z.signi && (z.i > 0)) { + if ((z.i.get_sign() == 1) && (z.i > 0)) { os << '+'; } } if (z.i != 0) { if ((z.i != 1) && (z.i != -1)) { os << z.i; + } else if (z.i == -1) { + os << '-'; } - else { - if (! z.signi) { - os << '-'; - } - } os << 'i'; } return os; @@ -100,8 +73,6 @@ class cnumber { void operator=(const cnumber &z ) { r = z.r; i = z.i; - signr = z.signr; - signi = z.signi; } }; From b9e3db439fdad62179a5e9ae957e7298c8e6be24 Mon Sep 17 00:00:00 2001 From: Mikael Nordin Date: Sun, 19 Apr 2020 23:03:28 +0200 Subject: [PATCH 13/17] Update README.md --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index e70ec17..54f8c6e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +**Compile and run** ```bash git clone https://github.com/mickenordin/fractions.git git clone https://github.com/mickenordin/complex-numbers.git @@ -5,3 +6,20 @@ cd complex-numbers g++ -o cnumber cnumber.cpp ./cnumber ``` +**Output** +``` +a = 3+2i +a* = 3-2i +a*a* = 13 +b = 4-3i +b* = 4+3i +b*b* = 25 +a + b = 7-i +(a + b)* = 7+i +a - b = -1+5i +(a - b)* = -1-5i +a * b = 18-i +(a * b)* = 18+i +a / b = (6/25)+(17/25)i +(a / b)* = (6/25)-(17/25)i +``` From d291f7a330b7ed72ec57c502c940a0b4de4abde3 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 16 Jul 2022 09:37:03 +0200 Subject: [PATCH 14/17] Add copy constructor --- cnumber.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cnumber.hpp b/cnumber.hpp index ee7b63c..277f228 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -9,6 +9,10 @@ class cnumber { public: // Constructor + cnumber(const cnumber &z) { + r = z.r; + i = z.i; + } cnumber(const fraction &a, const fraction &b) { r = a; i = b; From ae46252c3e3eaa257de264d0dcc20548edae55fc Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 16 Jul 2022 12:24:32 +0200 Subject: [PATCH 15/17] Fix indentation --- cnumber.hpp | 151 +++++++++++++++++++++++++++------------------------- 1 file changed, 77 insertions(+), 74 deletions(-) diff --git a/cnumber.hpp b/cnumber.hpp index 277f228..51a2f3e 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -1,82 +1,85 @@ -#include +#pragma once #include "../fractions/fractions.hpp" +#include using namespace std; class cnumber { - private: - fraction r,i; - - public: - // Constructor - cnumber(const cnumber &z) { - r = z.r; - i = z.i; - } - cnumber(const fraction &a, const fraction &b) { - r = a; - i = b; - } - cnumber(int a, int b) { - r = a; - i = b; - } - cnumber(double a, double b) { - r = a; - i = b; - } - // Member functions - cnumber conjugate() const { - fraction a(this->r.get_n(), this->r.get_d()); - fraction b(this->i.get_n(), this->i.get_d()); - cnumber z(a,b * -1); - return z; - } +private: + fraction r, i; - // Operators - cnumber operator+(const cnumber &that) const { - cnumber z(this->r + that.r, this->i + that.i); - return z; - } - cnumber operator-(const cnumber &that) const { - cnumber z(this->r - that.r, this->i - that.i); - return z; - } - cnumber operator*(const cnumber &that) const { - cnumber z( (that.r * this->r ) - (that.i * this->i) , (that.r * this->i) + (this->r * that.i) ); - return z; - } - cnumber operator*(const fraction &q) const { - cnumber that(q,fraction(0)); - return *this * that; - } - cnumber operator/(const cnumber &that) const { - cnumber numerator( (this->r * that.r ) - (this->i * ( that.i * -1)) , (this->r * (that.i * -1)) + (that.r * this->i) ); - cnumber denominator( (that.r * that.r ) - (that.i * ( that.i * -1)) , (that.r * (that.i * -1)) + (that.r * that.i) ); - cnumber ratio(numerator.r / denominator.r, numerator.i / denominator.r); - return ratio; - } +public: + // Constructor + cnumber(const cnumber &z) { + r = z.r; + i = z.i; + } + cnumber(const fraction &a, const fraction &b) { + r = a; + i = b; + } + cnumber(int a, int b) { + r = a; + i = b; + } + cnumber(double a, double b) { + r = a; + i = b; + } + // Member functions + cnumber conjugate() const { + fraction a(this->r.get_n(), this->r.get_d()); + fraction b(this->i.get_n(), this->i.get_d()); + cnumber z(a, b * -1); + return z; + } - friend ostream &operator<<( ostream &os, const cnumber &z ) { - if (z.r != 0) { - os << z.r; - if ((z.i.get_sign() == 1) && (z.i > 0)) { - os << '+'; - } - } - if (z.i != 0) { - if ((z.i != 1) && (z.i != -1)) { - os << z.i; - } else if (z.i == -1) { - os << '-'; - } - os << 'i'; - } - return os; - } - void operator=(const cnumber &z ) { - r = z.r; - i = z.i; - } -}; + // Operators + cnumber operator+(const cnumber &that) const { + cnumber z(this->r + that.r, this->i + that.i); + return z; + } + cnumber operator-(const cnumber &that) const { + cnumber z(this->r - that.r, this->i - that.i); + return z; + } + cnumber operator*(const cnumber &that) const { + cnumber z((that.r * this->r) - (that.i * this->i), + (that.r * this->i) + (this->r * that.i)); + return z; + } + cnumber operator*(const fraction &q) const { + cnumber that(q, fraction(0)); + return *this * that; + } + cnumber operator/(const cnumber &that) const { + cnumber numerator((this->r * that.r) - (this->i * (that.i * -1)), + (this->r * (that.i * -1)) + (that.r * this->i)); + cnumber denominator((that.r * that.r) - (that.i * (that.i * -1)), + (that.r * (that.i * -1)) + (that.r * that.i)); + cnumber ratio(numerator.r / denominator.r, numerator.i / denominator.r); + return ratio; + } + friend ostream &operator<<(ostream &os, const cnumber &z) { + if (z.r != 0) { + os << z.r; + if ((z.i.get_sign() == 1) && (z.i > 0)) { + os << '+'; + } + } + if (z.i != 0) { + if ((z.i != 1) && (z.i != -1)) { + os << z.i; + } else if (z.i == -1) { + os << '-'; + } + os << 'i'; + } + return os; + } + void operator=(const cnumber &z) { + r = z.r; + i = z.i; + } +}; From 6db1b1e943d6a57f94d3fd0bd205b3bba08802b8 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 16 Jul 2022 13:02:27 +0200 Subject: [PATCH 16/17] Print zeros --- cnumber.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cnumber.hpp b/cnumber.hpp index 51a2f3e..f8cd7d8 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -76,6 +76,9 @@ public: } os << 'i'; } + if (z.r == 0 && z.i == 0) { + os << '0'; + } return os; } void operator=(const cnumber &z) { From e5cd0fc7d19549623e1f185d0a0e0c3248b4613e Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 16 Jul 2022 13:51:57 +0200 Subject: [PATCH 17/17] Add equality operators --- cnumber.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cnumber.hpp b/cnumber.hpp index f8cd7d8..6c50a33 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -85,4 +85,6 @@ public: r = z.r; i = z.i; } + const bool operator==(const cnumber &z) const { return r == z.r && i == z.i; } + const bool operator!=(const cnumber &z) const { return r != z.r || i != z.i; } };