-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.hpp
112 lines (92 loc) · 3.19 KB
/
complex.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Email: [email protected]
* ID: ****6281
* @SamuraiPolix - Samuel Lazareanu
*/
#ifndef COMPLEX_HPP
#define COMPLEX_HPP
#include <string>
class Complex {
// private by default
double _real;
double _img;
public:
// Constructors
Complex(double real = 0.0, double img = 0.0) : _real(real), _img(img) {}; // inline constructor - defaults to 0, 0
Complex(const Complex& c) : _real(c._real), _img(c._img) {}; // copy constructor
// Destructor
~Complex() = default; // can be removed but its better practice to keep it
// Getters - all inline for simplicity
double getReal() const { return _real; }
double getImg() const { return _img; }
// Setters - all inline for simplicity
void setReal(double real) { this->_real = real; }
void setImg(double img) { this->_img = img; }
// Getter & Setter - flexible using reference and non-const
double& real() { return this->_real;}
double& img() { return this->_img;}
// Overloaded operators - not really required in assignment
Complex operator+(const Complex& c) const {
return Complex(_real + c._real, _img + c._img);
}
Complex operator-(const Complex& c) const {
return Complex(_real - c._real, _img - c._img);
}
Complex operator*(const Complex& c) const {
return Complex(_real * c._real - _img * c._img, _real * c._img + _img * c._real);
}
Complex operator/(const Complex& c) const {
double denominator = c._real * c._real + c._img * c._img;
return Complex((_real * c._real + _img * c._img) / denominator, (_img * c._real - _real * c._img) / denominator);
}
Complex& operator+=(const Complex& c) {
_real += c._real;
_img += c._img;
return *this;
}
Complex& operator-=(const Complex& c) {
_real -= c._real;
_img -= c._img;
return *this;
}
Complex& operator*=(const Complex& c) {
double temp = _real;
_real = _real * c._real - _img * c._img;
_img = temp * c._img + _img * c._real;
return *this;
}
Complex& operator/=(const Complex& c) {
double denominator = c._real * c._real + c._img * c._img;
double temp = _real;
_real = (_real * c._real + _img * c._img) / denominator;
_img = (_img * c._real - temp * c._img) / denominator;
return *this;
}
Complex& operator=(const Complex& c) {
_real = c._real;
_img = c._img;
return *this;
}
bool operator==(const Complex& c) const {
return _real == c._real && _img == c._img;
}
bool operator!=(const Complex& c) const {
return _real != c._real || _img != c._img;
}
friend std::ostream& operator<<(std::ostream& os, const Complex& c) {
os << c._real << "+";
if (c._img != 1 && c._img != 0){
os << c._img;
}
os << "i";
return os;
}
// Conversion functions
operator double() const { return _real; }
// to string
operator std::string() const { return to_string(); }
std::string to_string() const {
return std::to_string(_real)+"+"+std::to_string(_img)+"i";
}
};
#endif // COMPLEX_HPP