-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.cpp
71 lines (56 loc) · 1.27 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
#include <iostream>
#include "complex.h"
#include <cmath>
template<typename T>
Complex<T>::Complex() {}
template<typename T>
Complex<T>::Complex(T r_, T i_) {
r = r_;
i = i_;
}
template<typename T>
void Complex<T>::display() {
std::cout << r << " + " << i << "i" << std::endl;
}
template<typename T>
Complex<T> Complex<T>::operator+(const Complex<T>& c) const {
Complex summed = Complex(0, 0);
summed.r = r + c.r;
summed.i = i + c.i;
return summed;
}
template<typename T>
Complex<T> Complex<T>::operator-(const Complex<T>& c) const {
return Complex(r - c.r, i - c.i);
}
template<typename T>
Complex<T> Complex<T>::operator/(float s) {
return Complex<T>(r/s, i/s);
}
template<typename T>
void Complex<T>::operator+=(const Complex<T>& c) {
r += c.r;
i += c.i;
}
template<typename T>
void Complex<T>::operator-=(const Complex<T>& c) {
r -= c.r;
i -= c.i;
}
template<typename T>
void Complex<T>::operator*=(const Complex<T>& c) {
r *= c.r;
i *= c.i;
}
template<typename T>
T Complex<T>::magnitude_squared() {
return r * r + i * i;
}
template<typename T>
T Complex<T>::magnitude() {
return std::sqrt(magnitude_squared());
}
//template class Complex<long double>;
//template class Complex<double>;
//template class Complex<float>;
//template class Complex<int>;