-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex
More file actions
65 lines (53 loc) · 1.46 KB
/
complex
File metadata and controls
65 lines (53 loc) · 1.46 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
#include <iostream>
#include <cmath>
// задача - реализовать класс комплексных чисел
class Complex {
private:
double x, y;
public:
Complex(double a = 0.0, double b = 0.0):
x(a), y(b) {
}
double Re() {
return x;
}
double Im() {
return y;
}
};
Complex operator+(Complex z1, Complex z2) {
return {z1.Re() + z2.Re(), z1.Im() + z2.Im()};
}
Complex operator-(Complex z1, Complex z2) {
return {z1.Re() - z2.Re(), z1.Im() - z2.Im()};
}
Complex operator*(Complex z1, Complex z2) {
return {z1.Re()*z2.Re() - z2.Im()*z1.Im(), z1.Im()*z2.Re() + z2.Im()*z1.Re()};
}
Complex operator/(Complex z1, Complex z2) {
return {(z1.Re()*z2.Re() + z2.Im()*z1.Im())/((z2.Re())*(z2.Re()) + (z2.Im())*(z2.Im())),
(z2.Re()*z1.Im() - z1.Re()*z2.Im())/((z2.Re())*(z2.Re()) + (z2.Im())*(z2.Im()))};
}
Complex operator+(Complex z1) {
return z1;
}
Complex operator-(Complex z1) {
Complex minus_z1 = {-z1.Re(), -z1.Im()};
return minus_z1;
}
bool operator==(Complex z1, Complex z2) {
return (z1.Re() == z2.Re() && z2.Im() == z1.Im());
}
bool operator!=(Complex z1, Complex z2) {
return (z1.Re() != z2.Re() || z2.Im() != z1.Im());
}
double abs(Complex z1) {
double absolute = sqrt((z1.Re())*(z1.Re()) + (z1.Im())*(z1.Im()));
return absolute;
}
int main() {
Complex z(2, -3);
Complex i(3, 4);
Complex u = -i;
std::cout << u.Re() << ' ' << u.Im() << 'i';
}