Simplyfy a bit

main
Micke Nordin 5 years ago
parent 8fbe2e2950
commit 797347f442

@ -6,44 +6,26 @@ using namespace std;
class cnumber { class cnumber {
private: private:
fraction r,i; fraction r,i;
bool signr, signi;
bool get_sign(const fraction &q) {
return q.sign();
}
public: public:
// Constructor // Constructor
cnumber(const fraction &a, const fraction &b) { cnumber(const fraction &a, const fraction &b) {
r = a; r = a;
i = b; i = b;
signr = get_sign(r);
signi = get_sign(i);
} }
cnumber(int a, int b) { cnumber(int a, int b) {
fraction qa(a); r = a;
fraction qb(b); i = b;
r = qa;
i = qb;
signr = get_sign(r);
signi = get_sign(i);
} }
cnumber(double a, double b) { cnumber(double a, double b) {
fraction qa(a); r = a;
fraction qb(b); i = 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 // Member functions
cnumber conjugate() const { 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; return z;
} }
@ -56,20 +38,14 @@ class cnumber {
cnumber z(this->r - that.r, this->i - that.i); cnumber z(this->r - that.r, this->i - that.i);
return z; 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 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; return z;
} }
cnumber operator*(const fraction &q) const {
cnumber that(q,fraction(0));
return *this * that;
}
cnumber operator/(const cnumber &that) const { 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 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 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 ) { friend ostream &operator<<( ostream &os, const cnumber &z ) {
if (z.r != 0) { if (z.r != 0) {
os << z.r; os << z.r;
if (z.signi && (z.i > 0)) { if ((z.i.get_sign() == 1) && (z.i > 0)) {
os << '+'; os << '+';
} }
} }
if (z.i != 0) { if (z.i != 0) {
if ((z.i != 1) && (z.i != -1)) { if ((z.i != 1) && (z.i != -1)) {
os << z.i; os << z.i;
} } else if (z.i == -1) {
else {
if (! z.signi) {
os << '-'; os << '-';
} }
}
os << 'i'; os << 'i';
} }
return os; return os;
@ -100,8 +73,6 @@ class cnumber {
void operator=(const cnumber &z ) { void operator=(const cnumber &z ) {
r = z.r; r = z.r;
i = z.i; i = z.i;
signr = z.signr;
signi = z.signi;
} }
}; };

Loading…
Cancel
Save