mmathlib/cnumber.hpp

52 lines
933 B
C++
Raw Normal View History

2016-06-18 19:15:56 +02:00
#include <iostream>
using namespace std;
class cnumber {
private:
int r, i;
bool signr, signi;
bool get_sign(int i) {
return (i >= 0);
}
public:
2016-06-18 21:11:21 +02:00
// Constructor
2016-06-18 19:15:56 +02:00
cnumber(int a, int b) {
r = a;
i = b;
signr = get_sign(r);
signi = get_sign(i);
}
2016-06-18 21:11:21 +02:00
// Member functions
cnumber conjugate() {
cnumber z(this->r, this->i * -1);
return z;
}
// Operators
2016-06-18 20:21:18 +02:00
cnumber operator+(const cnumber &that) {
2016-06-18 19:41:57 +02:00
cnumber z(that.r + this->r, that.i + this->i);
return z;
}
2016-06-18 20:21:18 +02:00
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;
}
2016-06-18 19:15:56 +02:00
friend ostream &operator<<( ostream &os, const cnumber &z ) {
2016-06-18 19:41:57 +02:00
os << z.r << (z.signi ? '+' : '\0' ) << z.i << 'i';
2016-06-18 19:15:56 +02:00
return os;
}
2016-06-18 21:11:21 +02:00
void operator=(const cnumber &z ) {
r = z.r;
i = z.i;
signr = z.signr;
signi = z.signi;
}
2016-06-18 19:15:56 +02:00
};