-
Notifications
You must be signed in to change notification settings - Fork 0
/
Complex.hpp
66 lines (49 loc) · 1.62 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
// written by Lior Trachtman: 211791041
// EMAIL: lior16122000@gmail.com
#ifndef COMPLEX_HPP
#define COMPLEX_HPP
#include <iostream>
#include <cmath>
using namespace std;
class Complex {
public:
double real;
double imag;
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
// Getter methods
double getReal() const { return real; }
double getImag() const { return imag; }
// Overloaded operators for arithmetic operations
Complex operator+(const Complex &other) const {
return {real + other.real, imag + other.imag};
}
Complex operator-(const Complex &other) const {
return {real - other.real, imag - other.imag};
}
Complex operator*(const Complex &other) const {
return {real * other.real - imag * other.imag, real * other.imag + imag * other.real};
}
bool operator==(const Complex& other) const {
return (real == other.real) && (imag == other.imag);
}
bool operator!=(const Complex& other) const {
return !(*this == other);
}
bool operator<(const Complex& other) const {
return abs(real) + abs(imag) < abs(other.real) + abs(other.imag);
}
bool operator>(const Complex& other) const {
return abs(real) + abs(imag) > abs(other.real) + abs(other.imag);
}
bool operator<=(const Complex& other) const {
return !(*this > other);
}
bool operator>=(const Complex& other) const {
return !(*this < other);
}
friend ostream& operator<<(ostream& os, const Complex& c) {
os << c.real << " + " << c.imag << "i";
return os;
}
};
#endif // COMPLEX_HPP