-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.cpp
More file actions
163 lines (137 loc) · 2.84 KB
/
Complex.cpp
File metadata and controls
163 lines (137 loc) · 2.84 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
#include "Complex.h"
#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
TComplex::TComplex()
{
Re = 0.0;
Im = 0.0;
Update();
}
TComplex::TComplex(float re_, float im_)
{
Re = re_;
Im = im_;
Update();
}
TComplex::TComplex(TComplex& z)
{
Im = z.Im;
Re = z.Re;
Update();
}
float TComplex::GetIm() const
{
return Im;
}
float TComplex::GetRe() const
{
return Re;
}
float TComplex::GetAbs() const
{
return abs;
}
float TComplex::GetArg() const
{
return arg;
}
void TComplex::SetIm(float im)
{
Im = im;
Update();
}
void TComplex::SetRe(float re)
{
Re = re;
Update();
}
void TComplex::Update()
{
abs = sqrt((Im * Im) + (Re*Re));
arg = acos(Re / abs);
}
void TComplex::PrintF()
{
cout << endl;
cout << "z=" << abs << "(cos(" << (arg/M_PI)*180 << ") + i*sin(" << (arg/M_PI)*180 << ")" << endl;
}
TComplex TComplex::Pow(float p)
{
abs = pow(abs, p);
float temp = arg/(2*M_PI);
arg = arg-floor(temp)*2*M_PI;
Re = cos(arg)*abs;
Im = sin(arg)*abs;
return *this;
}
TComplex TComplex::operator+(const TComplex& Z) const
{
TComplex res;
res.Im = Im + Z.Im;
res.Re = Re + Z.Re;
return res;
}
TComplex TComplex::operator-(const TComplex& Z) const
{
TComplex res;
res.Im = Im - Z.Im;
res.Re = Re - Z.Re;
return res;
}
TComplex TComplex::operator+=(const TComplex& Z)
{
Im = Im + Z.Im;
Re = Re + Z.Re;
Update();
return *this;
}
TComplex TComplex::operator-=(const TComplex& Z)
{
Im = Im - Z.Im;
Re = Re - Z.Re;
Update();
return *this;
}
TComplex TComplex::operator*(const TComplex& Z) const
{
TComplex res;
res.Re = Re * Z.Re - Im * Z.Im;
res.Im = Re * Z.Im + Im * Z.Re;
return res;
}
TComplex TComplex::operator/(const TComplex& Z) const
{
TComplex res;
res.Re = ((Re * Z.Re) + (Im * Z.Im)) / ((Z.Re * Z.Re) + (Z.Im * Z.Im));
res.Re = ((Im * Z.Re) - (Re * Z.Im)) / ((Z.Re * Z.Re) + (Z.Im * Z.Im));
return res;
}
TComplex TComplex::operator=(const TComplex& Z)
{
Im = Z.Im;
Re = Z.Re;
Update();
return *this;
}
bool TComplex::operator==(const TComplex& Z) const
{
return Im == Z.Im && Re == Z.Re;
}
ostream& operator << (ostream& str, const TComplex& Z)
{
str << "Äåéñòâèòåëüíàÿ ÷àñòü ðàâíà " << Z.Re << endl;
str << "Ìíèìàÿ ÷àñòü ðàâíà " << Z.Im << endl;
str << "Ìîäóëü êîìïëåêñíîãî ÷èñëà ðàâåí " << Z.abs << endl;
str << "Óãîë â ðàäèàíàõ ðàâåí " << Z.arg << endl;
str << "Óãîë â ðàâåí " << (Z.arg / M_PI) * 180 << endl;
return str;
}
istream& operator>>(istream& istr, TComplex& Z)
{
istr >> Z.Re;
istr >> Z.Im;
Z.Update();
return istr;
}