You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.9 KiB

#include <iostream>
#include "../fractions/fractions.hpp"
using namespace std;
class cnumber {
private:
4 years ago
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;
4 years ago
}
cnumber(int a, int b) {
4 years ago
r = a;
i = b;
}
cnumber(double a, double b) {
4 years ago
r = a;
i = b;
}
// Member functions
4 years ago
cnumber conjugate() const {
4 years ago
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;
}
// Operators
cnumber operator+(const cnumber &that) const {
4 years ago
cnumber z(this->r + that.r, this->i + that.i);
return z;
}
cnumber operator-(const cnumber &that) const {
4 years ago
cnumber z(this->r - that.r, this->i - that.i);
8 years ago
return z;
}
cnumber operator*(const cnumber &that) const {
4 years ago
cnumber z( (that.r * this->r ) - (that.i * this->i) , (that.r * this->i) + (this->r * that.i) );
return z;
}
4 years ago
cnumber operator*(const fraction &q) const {
cnumber that(q,fraction(0));
return *this * that;
}
cnumber operator/(const cnumber &that) const {
4 years ago
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;
4 years ago
if ((z.i.get_sign() == 1) && (z.i > 0)) {
os << '+';
}
}
if (z.i != 0) {
if ((z.i != 1) && (z.i != -1)) {
os << z.i;
4 years ago
} else if (z.i == -1) {
os << '-';
}
os << 'i';
}
return os;
}
void operator=(const cnumber &z ) {
r = z.r;
i = z.i;
}
};