-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.cpp
69 lines (54 loc) · 1.2 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
57
58
59
60
61
62
63
64
65
66
67
68
#include "complex.h"
//Constructors for Complex based on number of parameters
Complex::Complex()
{
this->x = 0.0;
this->y = 0.0;
}
Complex::Complex(const double &a)
{
this->x = a;
this->y = 0.0;
}
Complex::Complex(const double &a, const double &b)
{
this->x = a;
this->y = b;
}
//returns reference to real part
double& Complex::real()
{
return x;
}
//returns reference to imaginary part
double& Complex::imaginary()
{
return y;
}
//returns the absolute value of complex number
double Complex::absval()
{
return sqrt(x*x+y*y);
}
//returns the complex conjugate of the complex number
Complex Complex::conjug()
{
return Complex(x,-y);
}
//Operator overloads for addition, subtraction, division and multiplication
Complex operator+(const Complex &a, const Complex &b)
{
return Complex(a.x+b.x,a.y+b.y);
}
Complex operator-(const Complex &a, const Complex &b)
{
return Complex(a.x-b.x,a.y-b.y);
}
Complex operator*(const Complex &a, const Complex &b)
{
return Complex(a.x*b.x - a.y*b.y,a.x*b.y+a.y*b.x);
}
Complex operator/(const Complex &a, const Complex &b)
{
return Complex((a.x*b.x + a.y*b.y)/(b.x*b.x + b.y*b.y),(a.y*b.x - a.x*b.y)/(b.x*b.x + b.y*b.y));
}