Complex conjugation
This commit is contained in:
parent
b2b1733fec
commit
bdc98ba3c2
2 changed files with 23 additions and 3 deletions
10
cnumber.cpp
10
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;
|
||||
}
|
||||
|
|
16
cnumber.hpp
16
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;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue