-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplex.hpp
207 lines (176 loc) · 5.54 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// Complex number class implementation
//
#pragma once
#include "Complex.h"
template<typename T>
Complex<T>::Complex(double real_p, double imag_p){
m_real = T(real_p);
m_imag = T(imag_p);
}
template<typename T>
Complex<T>::Complex(const Complex & obj){ //Copy construct
m_real = obj.m_real;
m_imag = obj.m_imag;
}
template<typename T>
template<typename T2>
Complex<T>::Complex(const Complex<T2> & obj){ //Copy construct another class
m_real = obj.m_real;
m_imag = obj.m_imag;
}
template<typename T>
T Complex<T>::real() const noexcept{ //Return real part
return m_real;
}
template<typename T>
template<typename T1> //edit real part
void Complex<T>::real(T1 && obj){
m_real = decltype(T(), T1())(obj);
}
template<typename T>
T Complex<T>::imag() const noexcept{ //Return imag part
return m_imag;
}
template<typename T>
template<typename T1> //Edit imag part
void Complex<T>::imag(T1 && obj){
m_imag = decltype(T(), T1())(obj);
}
template<typename T>
template<typename T2> //Complex = Complex
Complex<T>& Complex<T>::operator=(const Complex<T2> & obj){
m_real = obj.m_real;
m_imag = obj.m_imag;
return *this;
}
template<typename T>
template<typename T2> //Complex = real
Complex<T>& Complex<T>::operator=(const T2 & obj){
m_real = obj;
return *this;
}
template<typename T>
template<typename T2> // Complex *= real
Complex<T>& Complex<T>::operator *= (const T2 & obj){
m_real *= obj;
m_imag *= obj;
return *this;
}
template<typename T>
template<typename T2> // Complex += real
Complex<T>& Complex<T>::operator += (const T2 & obj){
m_real += obj;
return *this;
}
template<typename T>
template<typename T2> // Complex -= real
Complex<T>& Complex<T>::operator -= (const T2 & obj){
m_real -= obj;
return *this;
}
template<typename T1, typename T2> //global real * Complex
Complex<decltype(T1() + T2())> operator *(const T1 & left, const Complex<T2> & right){
Complex<decltype(T1() + T2())> tmp(right);
tmp *= left;
return tmp;
}
template<typename T1, typename T2> //global Complex * real
Complex<decltype(T1() + T2())> operator *(const Complex<T1> & left, const T2 & right){
Complex<decltype(T1() + T2())> tmp(left);
tmp *= right;
return tmp;
}
template<typename T1, typename T2> //global Complex + real
Complex<decltype(T1() + T2())> operator +(const Complex<T1> & left, const T2 & right){
Complex<decltype(T1() + T2())> tmp(left);
tmp += right;
return tmp;
}
template<typename T1, typename T2> //global real + Complex
Complex<decltype(T1() + T2())> operator +(const T2 & left, const Complex<T1> & right){
Complex<decltype(T1() + T2())> tmp(right);
tmp += left;
return tmp;
}
template<typename T1, typename T2> //global Complex - real
Complex<decltype(T1() + T2())> operator -(const Complex<T1> & left, const T2 & right){
Complex<decltype(T1() + T2())> tmp(left);
tmp -= right;
return tmp;
}
template<typename T1, typename T2> //global real - Complex
Complex<decltype(T1() + T2())> operator -(const T2 & left, const Complex<T1> & right){
Complex<decltype(T1() + T2())> tmp(right);
tmp.m_real = left - tmp.m_real;
return tmp;
}
template<typename T>
template<typename T2> //Complex += Complex
Complex<T>& Complex<T>::operator += (const Complex<T2> & obj){
m_real += obj.m_real;
m_imag += obj.m_imag;
return *this;
}
template<typename T>
template<typename T2> //Complex -= Complex
Complex<T>& Complex<T>::operator -= (const Complex<T2> & obj){
m_real -= obj.m_real;
m_imag -= obj.m_imag;
return *this;
}
template<typename T>
template<typename T2> //Complex *= Complex
Complex<T>& Complex<T>::operator *= (const Complex<T2> & obj){
Complex<T> tmp;
tmp.m_real = m_real * obj.m_real - (m_imag * obj.m_imag);
tmp.m_imag = m_real * obj.m_imag + m_imag * obj.m_real;
*this = tmp;
return *this;
}
template<typename T1, typename T2> //global Complex + Complex
Complex<decltype(T1() + T2())> operator +(const Complex<T1> & left, const Complex<T2> & right){
Complex<decltype(T1() + T2())> tmp(left.m_real, left.m_imag);
tmp += right;
return tmp;
}
template<typename T1, typename T2> //global Complex - Complex
Complex<decltype(T1() + T2())> operator -(const Complex<T1> & left, const Complex<T2> & right){
Complex<decltype(T1() + T2())> tmp(left.m_real, left.m_imag);
tmp -= right;
return tmp;
}
template<typename T1, typename T2> //Two Complex * Complex
Complex<decltype(T1() + T2())> operator *(const Complex<T1> & left, const Complex<T2> & right){
Complex<decltype(T1() + T2())> tmp(left.m_real, left.m_imag);
tmp *= right;
return tmp;
}
template<typename T> //Overloaded global operator « for print complex
ostream & operator << (ostream &out, const Complex<T> &obj){
out << obj.m_real << " ";
if (obj.m_imag >= 0) out << "+ " << obj.m_imag << "*i";
else out << "- " << (-1) * obj.m_imag << "*i";
return out;
}
template<typename T> //Overloaded global operator » for print complex
istream & operator >> (istream &in, Complex<T> &obj){
char tmp;
T num;
in >> num;
obj.m_real = num;
in >> tmp;
if(tmp == '-' || tmp == '+'){
in >> num;
if (tmp == '-'){
obj.m_imag = (-1) * num;
}else{
obj.m_imag = num;
}
}
if(tmp == '*'){
obj.m_imag = obj.m_real;
obj.m_real = 0;
}
return in;
}