-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplex.cpp
118 lines (99 loc) · 3.44 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
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
/* Assignment C++: 2
Author: Barry Yosilevich, ID: 208725218
*/
#include "Complex.h"
#include <cmath>
#include <iostream>
#define PI 3.141592653
using namespace std;
/* Constructor */
Complex::Complex(float argReal, float argImaginary) {
realVal = argReal;
imaginaryVal = argImaginary;
calculatePhase();
calculateRad();
}
/* privte calculating complex number's polar coordinates: radius and phase */
void Complex::calculateRad()
{
radius = sqrt(realVal * realVal + imaginaryVal * imaginaryVal);
}
void Complex::calculatePhase()
{
if (realVal == 0)
{
if (imaginaryVal == 0)
phase = 0;
else if (imaginaryVal > 0)
phase = 90;
else
phase = 270;
}
else
{
//int quadrant;
if (imaginaryVal >= 0)
phase = atan2(imaginaryVal, realVal) * 180 / PI;
else
phase = 360 + atan2(imaginaryVal, realVal) * 180 / PI;
}
}
/* public getters for copmlex number's polar coorinates: */
float Complex::getRad() const {
return this->radius;
}
float Complex::getPhase() const {
return this->phase;
}
/*Operators overloading: Complex ddition */
Complex Complex::operator+(const Complex& argComplex) const {
Complex result;
result.realVal = realVal + argComplex.realVal;
result.imaginaryVal = imaginaryVal + argComplex.imaginaryVal;
return result;
}
/* Complex substraction */
Complex Complex::operator-(const Complex& argComplex) const {
Complex result;
result.realVal = realVal - argComplex.realVal;
result.imaginaryVal = imaginaryVal - argComplex.imaginaryVal;
return result;
}
/* Complex multiplication */
Complex Complex::operator*(const Complex& argComplex) const {
Complex result;
result.realVal = realVal * argComplex.realVal - imaginaryVal * argComplex.imaginaryVal;
result.imaginaryVal = imaginaryVal * argComplex.realVal + realVal * argComplex.imaginaryVal;
return result;
}
/* Complex division using conjugate multiplication method*/
Complex Complex::operator/(const Complex& argComplex) const {
if (argComplex.getRad() == 0){
cout << "Zero-devision is not allowed!" << endl;
return *this;
}
else {
Complex argConjugate(argComplex.realVal, -argComplex.imaginaryVal); // Initing argument conjugate to multiply numerator and denominator with
Complex numerator = *this * argConjugate; // Numerator multiplication result: complex number
Complex denominator = argComplex * argConjugate; // Denominator multiplication result - real number
Complex result;
result.realVal = numerator.realVal / denominator.realVal; // Dividing numerator real and imaginary part with the real denominator
result.imaginaryVal = numerator.imaginaryVal / denominator.realVal;
return result;
}
}
/*Complex equallity operator - comparing real parts and imaginary parts of 2 complex parameters */
bool Complex::operator==(const Complex& argComplex) const {
return realVal == argComplex.realVal && imaginaryVal == argComplex.imaginaryVal;
}
/* Conjucgate operator*/
Complex Complex::operator~() const { return Complex(realVal, -imaginaryVal); }
/* cout << operator implementaation */
ostream& operator<<(ostream& output, const Complex& argComplex) {
if (argComplex.imaginaryVal < 0 )
return output << argComplex.realVal << " - " << abs(argComplex.imaginaryVal) << 'i';
//else imaginaryVal >= 0
return output << argComplex.realVal << " + " << abs(argComplex.imaginaryVal) << 'i';
}
/* Distructor */
Complex::~Complex() {};