Merge cnumbers

main
Micke Nordin 2 years ago
commit 999dcb96c7
Signed by: micke
GPG Key ID: 014B273D614BE877

@ -6,6 +6,8 @@ git clone https://code.smolnet.org/micke/mmathlib.git
cd mmathlib
g++ -o fractions fractions.cpp
./fractions
g++ -o cnumber cnumber.cpp
./cnumber
```
**Output**
@ -24,4 +26,18 @@ g++ -o fractions fractions.cpp
(7/3)/(1/11)=(77/3)
(7/3)/(1/11)=25.66666667
0.75=(3/4)
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
```

@ -0,0 +1,23 @@
#include <iostream>
#include "cnumber.hpp"
int main() {
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;
}

@ -0,0 +1,90 @@
#pragma once
#include "../fractions/fractions.hpp"
#include <iostream>
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;
}
// 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';
}
if (z.r == 0 && z.i == 0) {
os << '0';
}
return os;
}
void operator=(const cnumber &z) {
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; }
};
Loading…
Cancel
Save