-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.cpp
165 lines (131 loc) · 3.21 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <cmath>
#include "complex.h"
// Constructors
complex::complex() : complex(0) {}
complex::complex(double r, double i) : r(r), i(i) {}
// Getters
double complex::real() const {
return r;
}
double complex::imaginary() const {
return i;
}
// Setters
complex &complex::real(double real) {
r = real;
return *this;
}
complex &complex::imaginary(double imaginary) {
i = imaginary;
return *this;
}
// Other member functions
double complex::abs() const {
return sqrt(r * r + i * i);
}
complex complex::complement() const {
return complex(r, -i);
}
void complex::print() const {
cout << r << " + " << i << "i";
}
// Unary operators
complex complex::operator-() {
return complex(-r, -i);
}
complex complex::operator~() {
return complex(r, -i);
}
// Binary Operators
complex complex::operator+(const complex &rhs) {
return complex(r + rhs.r, i + rhs.i);
}
complex complex::operator-(const complex &rhs) {
return complex(r - rhs.r, i - rhs.i);
}
complex complex::operator*(const complex &rhs) {
return complex(r * rhs.r - i * rhs.i, r * rhs.i + i * rhs.r);
}
complex complex::operator/(const complex &rhs) {
double a = rhs.abs();
return complex(*this * rhs.complement()) / (a * a);
}
complex complex::operator+(const double &rhs) {
return complex(r + rhs, i);
}
complex complex::operator-(const double &rhs) {
return complex(r - rhs, i);
}
complex complex::operator*(const double &rhs) {
return complex(r * rhs, i * rhs);
}
complex complex::operator/(const double &rhs) {
return complex(r / rhs, i / rhs);
}
// Assignment operators
complex &complex::operator+=(const complex &rhs) {
r += rhs.r;
i += rhs.i;
return *this;
}
complex &complex::operator-=(const complex &rhs) {
r -= rhs.r;
i -= rhs.i;
return *this;
}
complex &complex::operator*=(const complex &rhs) {
double newReal = r * rhs.r - i * rhs.i;
double newImaginary = r * rhs.i + i * rhs.r;
r = newReal;
i = newImaginary;
return *this;
}
complex &complex::operator/=(const complex &rhs) {
*this = complex(*this / rhs);
return *this;
}
complex &complex::operator=(const double &rhs) {
r = rhs;
i = 0;
return *this;
}
complex &complex::operator+=(const double &rhs) {
r += rhs;
return *this;
}
complex &complex::operator-=(const double &rhs) {
r -= rhs;
return *this;
}
complex &complex::operator*=(const double &rhs) {
r *= rhs;
i *= rhs;
return *this;
}
complex &complex::operator/=(const double &rhs) {
r /= rhs;
i /= rhs;
return *this;
}
complex operator+(const double &lhs, const complex &rhs) {
return complex(lhs + rhs.r, rhs.i);
}
complex operator-(const double &lhs, const complex &rhs) {
return complex(lhs - rhs.r, -rhs.i);
}
complex operator*(const double &lhs, const complex &rhs) {
return complex(lhs * rhs.r, lhs * rhs.i);
}
complex operator/(const double &lhs, const complex &rhs) {
return complex(lhs) / complex(lhs - rhs.r, -rhs.i);
}
// Input/Output operators
ostream &operator<<(ostream &out, const complex &c) {
out << c.r;
if (c.i != 0) out << " + " << c.i << "i";
return out;
}
istream &operator>>(istream &in, complex &c) {
in >> c.r >> c.i;
return in;
}