-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycomplex.cpp
186 lines (158 loc) · 3.57 KB
/
mycomplex.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <cmath>
#include "mycomplex.h"
using namespace std;
/**
*@param a_Re - Real part of complex value
*@param a_Im - Imagine part of complex value
*/
Complex::Complex(double a_Re, double a_Im) {
Re = a_Re;
Im = a_Im;
}
Complex::Complex(const Complex & aRval) {
Re = aRval.Re;
Im = aRval.Im;
}
/**@brief Destructor for class Complex. Set real and imagine to null
*/
Complex::~Complex() {
Re = 0.0;
Im = 0.0;
}
/**@brief Initialize field of complex class
*/
void Complex::Set(double a_Re, double a_Im) {
Re = a_Re;
Im = a_Im;
}
Complex::operator double() {
return abs();
}
/**@brief Finding abs of complex value
*/
double Complex::abs() {
return sqrt(Re * Re + Im * Im);
}
//Operators for arithmetic operations with complex values
Complex Complex::operator + (const Complex & aRval) {
Complex Result;
Result.Re = Re + aRval.Re;
Result.Im = Im + aRval.Im;
return Result;
}
Complex Complex::operator - (const Complex & aRval) {
Complex Result;
Result.Re = Re - aRval.Re;
Result.Im = Im - aRval.Im;
return Result;
}
Complex Complex::operator + (const double & aRval) {
Complex Result;
result.Re = Re + aRval;
result.Im = Im;
return Result;
}
Complex Complex::operator - (const double & aRval) {
Complex Result( * this);
Result.Re = Re - aRval;
return Result;
}
Complex Complex::operator * (const Complex & aRval) {
Complex Result;
Result.Re = Re * aRval.Re - Im * aRval.Im;
Result.Im = Re * aRval.Im + Im * aRval.Re;
return Result;
}
Complex Complex::operator * (const double & aRval) {
Complex Result;
Result.Re = Re * aRval;
Result.Im = Im * aRval;
return Result;
}
Complex Complex::operator / (const double & aRval) {
Complex Result;
Result.Re = Re / aRval;
Result.Im = Im / aRval;
return Result;
}
Complex & Complex::operator += (const Complex & arval) {
Re += arval.Re;
Im += arval.Im;
return *this;
}
Complex & Complex::operator -= (const Complex & aRval) {
Re -= aRval.Re;
Im -= aRval.Im;
return *this;
}
Complex & Complex::operator *= (const Complex & aRval) {
double tmpRe = Re;
Re = Re * aRval.Re - Im * aRval.Im;
Im = Im * aRval.Re + tmpRe * aRval.Im;
return *this;
}
Complex & Complex::operator += (const double & aRval) {
Re += aRval;
return *this;
}
Complex & Complex::operator -= (const double & aRval) {
Re -= aRval;
return *this;
}
Complex & Complex::operator *= (const double & aRval) {
Re *= aRval;
Im *= aRval;
return *this;
}
Complex & Complex::operator /= (const double & aRval) {
Re /= aRval;
Im /= aRval;
return *this;
}
Complex & Complex::operator = (const Complex & aRval) {
Re = aRval.Re;
Im = aRval.Im;
return *this;
}
Complex & Complex::operator = (const double & aRval) {
Re = aRval;
Im = 0.0;
return *this;
}
/**
*@return stream
*/
istream & operator >> (istream & stream, Complex & aRval) {
char tmp[256];
stream >> aRval.Re >> aRval.Im >> tmp;
return stream;
}
/**
*@return stream
*/
ostream & operator << (ostream & stream, Complex & aRval)
{
stream << aRval.Re;
if (!(aRval.Im < 0)) stream << '+';
stream << aRval.Im << 'i';
return stream;
}
Complex operator + (const double & aLval, const Complex & aRval) {
Complex Result;
Result.Re = aLval + aRval.Re;
Result.Im = aRval.Im;
return Result;
}
Complex operator - (const double & aLval, const Complex & aRval) {
Complex Result;
Result.Re = aLval - aRval.Re;
Result.Im = -aRval.Im;
return Result;
}
Complex operator * (const double & aLval, const Complex & aRval) {
Complex Result;
Result.Re = aLval * aRval.Re;
Result.Im = aLval * aRval.Im;
return Result;
}