forked from Laykel/ComplexNumbers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComplexNumber.hpp
executable file
·126 lines (109 loc) · 5.08 KB
/
ComplexNumber.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
/*
* Project : Complex numbers in C++
* File : ComplexNumbers.hpp
* Author : Luc Wachter
*
* Simple class to model complex numbers.
* Added genericity, but no checks to see if the chosen types make sense.
* The operators can take two complex numbers of different types, once again
* no checks.
*/
#ifndef COMPLEX_NUMBER_H
#define COMPLEX_NUMBER_H
#include <iostream>
#include <cmath>
// Pre-declaration of generic class
template <typename T = double> class ComplexNumber;
// Pre-declaration of friend functions -------------------------------------------
template <typename T, typename U>
bool operator== (const ComplexNumber<T>& lhs, const ComplexNumber<U>& rhs);
template <typename T, typename U>
bool operator== (const ComplexNumber<T>& lhs, const U rhs); // Example
template <typename T, typename U>
bool operator!= (const ComplexNumber<T>& lhs, const ComplexNumber<U>& rhs);
template <typename T, typename U>
bool operator< (const ComplexNumber<T>& lhs, const ComplexNumber<U>& rhs);
template <typename T, typename U>
bool operator> (const ComplexNumber<T>& lhs, const ComplexNumber<U>& rhs);
template <typename T, typename U>
bool operator<= (const ComplexNumber<T>& lhs, const ComplexNumber<U>& rhs);
template <typename T, typename U>
bool operator>= (const ComplexNumber<T>& lhs, const ComplexNumber<U>& rhs);
template <typename T, typename U>
ComplexNumber<T> operator- (ComplexNumber<T> lhs, const ComplexNumber<U>& rhs);
template <typename T, typename U>
ComplexNumber<T> operator- (ComplexNumber<T> lhs, const U rhs);
template <typename T, typename U>
ComplexNumber<T> operator+ (ComplexNumber<T> lhs, const ComplexNumber<U>& rhs);
template <typename T, typename U>
ComplexNumber<T> operator+ (ComplexNumber<T> lhs, const U rhs);
template <typename T, typename U>
ComplexNumber<T> operator* (ComplexNumber<T> lhs, const ComplexNumber<U>& rhs);
template <typename T, typename U>
ComplexNumber<T> operator* (ComplexNumber<T> lhs, const U rhs);
template <typename T, typename U>
ComplexNumber<T> operator/ (ComplexNumber<T> lhs, const ComplexNumber<U>& rhs);
template <typename T, typename U>
ComplexNumber<T> operator/ (ComplexNumber<T> lhs, const U rhs);
template <typename T>
std::ostream& operator<< (std::ostream& os, const ComplexNumber<T>& c);
// -------------------------------------------------------------------------------
template <typename T>
class ComplexNumber {
// Comparison operators
friend bool operator== <>(const ComplexNumber& lhs, const ComplexNumber& rhs);
friend bool operator== <>(const ComplexNumber& lhs, const T rhs); // Example
friend bool operator!= <>(const ComplexNumber& lhs, const ComplexNumber& rhs);
friend bool operator< <>(const ComplexNumber& lhs, const ComplexNumber& rhs);
friend bool operator> <>(const ComplexNumber& lhs, const ComplexNumber& rhs);
friend bool operator<= <>(const ComplexNumber& lhs, const ComplexNumber& rhs);
friend bool operator>= <>(const ComplexNumber& lhs, const ComplexNumber& rhs);
// Arithmetic operators
friend ComplexNumber operator- <>(ComplexNumber lhs, const ComplexNumber& rhs);
friend ComplexNumber operator- <>(ComplexNumber lhs, const T rhs);
friend ComplexNumber operator+ <>(ComplexNumber lhs, const ComplexNumber& rhs);
friend ComplexNumber operator+ <>(ComplexNumber lhs, const T rhs);
friend ComplexNumber operator* <>(ComplexNumber lhs, const ComplexNumber& rhs);
friend ComplexNumber operator* <>(ComplexNumber lhs, const T rhs);
friend ComplexNumber operator/ <>(ComplexNumber lhs, const ComplexNumber& rhs);
friend ComplexNumber operator/ <>(ComplexNumber lhs, const T rhs);
// Stream operator
friend std::ostream& operator<< <>(std::ostream& os, const ComplexNumber& c);
public:
// Constructors
ComplexNumber();
ComplexNumber(T realPart, T imaginaryPart);
ComplexNumber(const ComplexNumber& c);
// Conversion constructor
template <typename U> ComplexNumber(const ComplexNumber<U>& c);
// Selectors
T getRealPart() const;
T getImaginaryPart() const;
double getModule() const;
double getArgument() const;
void displayPolarForm() const; // Trigonometric form
// Modifiers
void setRealPart(T realPart);
void setImaginaryPart(T imaginaryPart);
// Member operators
ComplexNumber& operator+= (const ComplexNumber& rhs); // With complex
ComplexNumber& operator+= (const T rhs); // With scalar
// Incrementation operators increment the real part
ComplexNumber& operator++ (); // pré-incrémentation
ComplexNumber operator++ (int); // post-incrémentation
ComplexNumber& operator-= (const ComplexNumber& rhs);
ComplexNumber& operator-= (const T rhs);
// Decrementation operators decrement the real part
ComplexNumber& operator-- (); // pré-décrémentation
ComplexNumber operator-- (int); // post-décrémentation
ComplexNumber& operator*= (const ComplexNumber& rhs);
ComplexNumber& operator*= (const T rhs);
ComplexNumber& operator/= (const ComplexNumber& rhs);
ComplexNumber& operator/= (const T rhs);
private:
T realPart;
T imaginaryPart;
};
// Genericity nonsense
#include "ComplexNumberImpl.hpp"
#endif