-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.cpp
More file actions
393 lines (327 loc) · 9.65 KB
/
Complex.cpp
File metadata and controls
393 lines (327 loc) · 9.65 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include<algorithm>
#include<string>
#include "Complex.h" // class implemented
using namespace std;
// File scope starts here
/////////////////////////////// PUBLIC ///////////////////////////////////////
//============================= LIFECYCLE ====================================
// Complex Default + Overloaded Constructor
Complex::Complex(double aReal, double aImaginary) : mReal(aReal), mImaginary(aImaginary){
this->SetComplex(aReal, aImaginary);
}
// end Complex constructor
// String Conversion Constructor
Complex::Complex(const string& aString) {
int posPlus = aString.find("+");
int posMinus = aString.find("-");
int posSymbol = max(posPlus, posMinus);
int posI = aString.find("i");
int posJ = aString.find("j");
int posImSymbol = max(posI, posJ);
if (posSymbol == -1 || posImSymbol == -1) {
if (this->Is_Number(aString) && posImSymbol == -1)
this->SetComplex(stod(aString));
else if (this->Is_Number(aString) && posImSymbol != -1)
this->SetComplex(0, stod(aString));
else {
cout << "Provided string cannot be converted to complex number. Setting string to zero." << endl;
this->SetComplex();
}
return;
}
double realDouble = 1.0;
double imaginaryDouble = 1.0;
int posStart = -1;
// for negative symbols
if (posPlus == -1) {
imaginaryDouble = -1.0;
if (count(aString.begin(), aString.end(), '-') == 2) {
realDouble = -1.0;
posSymbol = aString.find_last_of("-");
posStart = aString.find_first_of("-");
}
}
else {
if (posMinus != -1) {
realDouble = -1.0;
posStart = aString.find_first_of("-");
}
}
string realString = aString.substr(posStart + 1, posSymbol);
string gap = aString.substr(posSymbol + 1, posImSymbol - posSymbol + 1);
string imaginaryString;
if (this->Is_Number(gap)) // a + bi case
imaginaryString = aString.substr(posSymbol + 1);
else // a + ib case
imaginaryString = aString.substr(posImSymbol + 1);
if (this->Is_Number(realString))
realDouble *= stod(realString);
else
realDouble = 0.0;
imaginaryDouble *= stod(imaginaryString);
this->SetComplex(realDouble, imaginaryDouble);
}
// end Complex constructor
//============================= OPERATORS ====================================
// Stream Insertion
ostream& operator <<(ostream& rOs, const Complex& rFrom) {
rOs << "(" << rFrom.GetReal() << ") + (" << rFrom.GetImaginary() << ")i";
return rOs;
}
// end stream insertion
// Stream Extraction
istream& operator >>(istream& rIs, Complex& rTo) {
int aReal, aImaginary;
cout << "Enter real part: ";
rIs >> aReal;
cout << "Enter imaganiary part: ";
rIs >> aImaginary;
rTo.SetComplex(aReal, aImaginary);
return rIs;
}
// end stream extraction
// Addition operator
Complex Complex::operator +(const Complex& rhs) {
Complex t = *this;
return t += rhs;
}
// end Addition operator
// Overloaded addition operator
Complex Complex::operator +(const double rhs) {
Complex t = *this;
return t += rhs;
}
// end Addition operator
// Friend function to overloaded addition operator
Complex operator +(const double lhs, const Complex& rhs) {
Complex t = rhs;
return t += lhs;
}
// end Addition operator
// Addition assignment operator
Complex& Complex::operator +=(const Complex& rhs) {
*this += rhs.GetReal();
this->SetImaginary(this->GetImaginary() + rhs.GetImaginary());
return *this;
}
// end addition assignment operator
// Overloaded Addition assignment operator
Complex& Complex::operator +=(const double rhs) {
this->SetReal(this->GetReal() + rhs);
return *this;
}
// end addition assignment operator
// Subtraction operator
Complex Complex::operator -(const Complex& rhs) {
Complex t = *this;
return t -= rhs;
}
// end Subtraction operator
// Overloaded subtraction operator
Complex Complex::operator -(const double rhs) {
Complex t = *this;
return t -= rhs;
}
// end subtraction operator
// Friend function to overloaded subtraction operator
Complex operator -(const double lhs, const Complex& rhs) {
Complex t = rhs;
t = -t;
return t += lhs;
}
// end subtraction operator
// Subtraction assignment operator
Complex& Complex::operator -=(const Complex& rhs) {
*this -= rhs.GetReal();
this->SetImaginary(this->GetImaginary() - rhs.GetImaginary());
return *this;
}
// end subtraction assignment operator
// Overloaded subtraction assignment operator
Complex& Complex::operator -=(const double rhs) {
this->SetReal(this->GetReal() - rhs);
return *this;
}
// end subtraction assignment operator
// Multiplication operator
Complex Complex::operator *(const Complex& rhs) {
Complex t(this->GetComplex());
return (t *= rhs);
}
// end multiplication operator
// Overloaded multiplication operator
Complex Complex::operator *(const double rhs) {
Complex t(this->GetComplex());
return (t *= rhs);
}
// end multiplication operator
// Friend function to overloaded multiplication operator
Complex operator *(const double lhs, const Complex& rhs) {
Complex t = rhs;
return t *= lhs;
}
// end multiplication operator
// Multiplication assignment operator
Complex& Complex::operator *=(const Complex& rhs) {
this->SetComplex(this->GetReal()*rhs.GetReal() - this->GetImaginary()*rhs.GetImaginary(), this->GetReal()*rhs.GetImaginary() + this->GetImaginary()*rhs.GetReal());
return *this;
}
// end multiplication assignment operator
// Overloaded multiplication assignment operator
Complex& Complex::operator *=(const double rhs) {
this->SetComplex(this->GetReal()*rhs, this->GetImaginary()*rhs);
return *this;
}
// end multiplication assignment operator
// Division operator
Complex Complex::operator /(const Complex& rhs) {
Complex t(this->GetComplex());
return (t /= rhs);
}
// end division operator
// Overloaded division operator
Complex Complex::operator /(const double rhs) {
Complex t(this->GetComplex());
return (t /= rhs);
}
// end division operator
// Friend function to overloaded division operator
Complex operator /(const double lhs, const Complex& rhs) {
Complex t;
t =(Complex)lhs;
return (t /= rhs);
}
// end division operator
// Division assignment operator
Complex& Complex::operator /=(const Complex& rhs) {
Complex nmrtr, dnmntr, cnjgt = rhs;
cnjgt = *cnjgt;
double dDenom;
nmrtr = this->GetComplex();
nmrtr*=cnjgt;
dnmntr = cnjgt * rhs;
dDenom = (double)dnmntr;
this->SetComplex(nmrtr / dDenom);
return *this;
}
// end division assignment operator
// Overloaded division assignment operator
Complex& Complex::operator /=(const double rhs) {
this->SetComplex(this->GetReal() / rhs, this->GetImaginary() / rhs);
return *this;
}
// end division assignment operator
// Equality operator
bool Complex::operator == (const Complex& rhs) {
if ((this->GetReal() == rhs.GetReal()) && (this->GetImaginary() == rhs.GetImaginary())) {
return true;
}
else
return false;
}
// end equality operator
// Non-Equality operator
bool Complex::operator != (const Complex& rhs)
{
if (*this == rhs)
return false;
else
return true;
}
// end non-Equality operator
// Negation operator
Complex Complex::operator -() {
Complex temp(-this->GetReal(), -this->GetImaginary());
return temp;
}
//end negation operator
// Conjugate operator
Complex Complex::operator *() {
Complex t(this->GetReal(), -this->GetImaginary());
return t;
}
//end conjugate operator
// pre-increment operator
Complex& Complex::operator++() {
*this += 1;
return *this;
}
// end pre-increment operator
// post-increment operator
Complex Complex::operator ++(int) {
Complex t = *this;
*this += 1;
return t;
}
// end post-increment operator
// pre-decrement operator
Complex& Complex::operator --() {
*this -= 1;
return *this;
}
// end pre-decrement operator
// post-decrement operator
Complex Complex::operator --(int) {
Complex t = *this;
*this -= 1;
return t;
}
// end post-decrement operator
// Function operator to overload float
Complex::operator double() {
return this->GetReal();
}
// end function operator
//============================= ACESS ===================================
// function that sets real part of complex number
void Complex::SetReal(double aReal) {
this->mReal = aReal;
}
// end function SetReal
// function that sets imaginary part of complex number
void Complex::SetImaginary(double aImaginary) {
this->mImaginary = aImaginary;
}
// end function SetImaginary
// function that sets the Complex number
void Complex::SetComplex(double aReal, double aImaginary) {
this->SetReal(aReal);
this->SetImaginary(aImaginary);
}
// end function SetComplex
// overloaded function that sets the Complex number
void Complex::SetComplex(const Complex& rComplex) {
this->SetComplex(rComplex.GetReal(), rComplex.GetImaginary());
}
// end function SetComplex
// function that gets real part of complex number
double Complex::GetReal()const {
return this->mReal;
}
// end function GetReal
// function that gets imaginary part of complex number
double Complex::GetImaginary()const {
return this->mImaginary;
}
// end function GetImaginary
// function that gets the Complex number
const Complex& Complex::GetComplex()const {
return *this;
}
// end function GetComplex
/////////////////////////////// PRIVATE ///////////////////////////////////
/** utility function to check for leap year.
*
* @param testNumber The string to be tested for numbers.
*
* @return true if testNumber is numeric, false otherwise.
*/
bool Complex::Is_Number(const string& testNumber) {
try{
std::stod(testNumber);
}
catch (...){
return false;
}
return true;
}