-
Notifications
You must be signed in to change notification settings - Fork 6
/
complex.cpp
58 lines (46 loc) · 1.72 KB
/
complex.cpp
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
#include <string>
#include <cmath>
namespace mlb {
class Complex {
private:
double real;
double imaginary;
public:
// Constructor
Complex(double r, double i) : real(r), imaginary(i) {}
// Getter functions
double getReal() const {
return real;
}
double getImaginary() const {
return imaginary;
}
// Function to calculate the magnitude of the complex number
double magnitude() const {
return sqrt(real * real + imaginary * imaginary);
}
// Function to calculate the phase (argument) of the complex number in radians
double phase() const {
return atan2(imaginary, real);
}
// Overloaded addition operator
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imaginary + other.imaginary);
}
// Overloaded subtraction operator
Complex operator-(const Complex& other) const {
return Complex(real - other.real, imaginary - other.imaginary);
}
// Overloaded multiplication operator
Complex operator*(const Complex& other) const {
return Complex(real * other.real - imaginary * other.imaginary,
real * other.imaginary + imaginary * other.real);
}
// Overloaded division operator
Complex operator/(const Complex& other) const {
double denominator = other.real * other.real + other.imaginary * other.imaginary;
return Complex((real * other.real + imaginary * other.imaginary) / denominator,
(imaginary * other.real - real * other.imaginary) / denominator);
}
};
}