-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplex.cpp
158 lines (124 loc) · 2.63 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
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
#include <iostream>
#include <math.h>
#include "Complex.h"
using namespace std;
Complex::Complex()
{
Re = 0;
Im = 0;
}
Complex::Complex(double Re, double Im)
{
this->Re = Re;
this->Im = Im;
}
Complex::Complex(const Complex& value)
{
Re = value.Re;
Im = value.Im;
}
double Complex::getRe()
{
return Re;
}
double Complex::getIm()
{
return Im;
}
void Complex::setRe(double valueRe)
{
this->Re = valueRe;
}
void Complex::setIm(double valueIm)
{
this->Im = valueIm;
}
Complex Complex::operator+(Complex value)
{
return Complex(this->Re + value.Re, this->Im + value.Im);
}
Complex Complex::operator-(Complex value)
{
return Complex(this->Re - value.Re, this->Im - value.Im);
}
Complex Complex::operator*(Complex value)
{
return Complex((this->Re * value.Re) - (this->Im * value.Im), (this->Re * value.Im) + (value.Re * this->Im));
}
Complex Complex::operator/(Complex value)
{
double rRe = ((this->Re * value.Re) + (this->Im * value.Im)) / (pow(value.Re, 2) + pow(value.Im, 2));
double rIm = ((value.Re * this->Im) - (this->Re * value.Im)) / (pow(value.Re, 2) + pow(value.Im, 2));
return Complex(rRe, rIm);
}
Complex& Complex::operator=(Complex value)
{
Re = value.Re;
Im = value.Im;
return *this;
}
bool Complex::operator==(Complex value)
{
if (this->Re == value.Re && this->Im == value.Im)
{
return true;
}
else return false;
}
ostream& operator<<(ostream& output, Complex value)
{
output << value.Re << (value.Im >= 0 ? " + " : " - ") << abs(value.Im) << "i" << endl;
return output;
}
istream& operator>>(istream& input, Complex& value)
{
input >> value.Re;
input >> value.Im;
return input;
}
double Complex::absC()
{
return sqrt(pow(this->Re, 2) + pow(this->Im, 2));
}
Complex Complex::powC()
{
double powC = 0;
cout << "input degree(int): ";
cin >> powC;
return Complex(pow(this->absC(), powC)*cos(powC*argC()), pow(this->absC(), powC)*sin(powC*argC()));
}
void Complex::trigonometric()
{
cout << this->absC() << "*(cos(" << argC() << ") + i*sin(" << argC() << ")" << endl;
}
double Complex::argC()
{
double phi = 0;
double pi = 3.14159;
if (this->Re > 0)
{
phi = atan2(this->Im, this->Re);
}
else if (this->Re < 0 && this->Im >= 0)
{
phi = atan2(this->Im, this->Re) + pi;
}
else if (this->Re < 0 && this->Im < 0)
{
phi = atan2(this->Im, this->Re) - pi;
}
else if (this->Re == 0 && this->Im > 0)
{
phi = pi/2;
}
else if (this->Re == 0 && this->Im < 0)
{
phi = -pi / 2;
}
else
{
cout << "Error" << endl;
return 0;
}
return phi;
}