-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwork33.cpp
More file actions
92 lines (91 loc) · 2.4 KB
/
Copy pathwork33.cpp
File metadata and controls
92 lines (91 loc) · 2.4 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
#include <iostream>
using namespace std;
class Complex
{
private:
double x;
double y;
public:
Complex(double x = 0.0, double y = 0.0) : x(x), y(y) {}
Complex &operator+=(const Complex &c)
{
x += c.x;
y += c.y;
return *this;
}
Complex &operator-=(const Complex &c)
{
x -= c.x;
y -= c.y;
return *this;
}
Complex &operator*=(const Complex &c)
{
double newX = x * c.x - y * c.y;
double newY = x * c.y + y * c.x;
x = newX;
y = newY;
return *this;
}
Complex &operator/=(const Complex &c)
{
double denom = c.x * c.x + c.y * c.y;
double newX = (x * c.x + y * c.y) / denom;
double newY = (y * c.x - x * c.y) / denom;
x = newX;
y = newY;
return *this;
}
friend Complex operator+(const Complex &c1, const Complex &c2)
{
return Complex(c1.x + c2.x, c1.y + c2.y);
}
friend Complex operator-(const Complex &c1, const Complex &c2)
{
return Complex(c1.x - c2.x, c1.y - c2.y);
}
friend Complex operator*(const Complex &c1, const Complex &c2)
{
return Complex(c1.x * c2.x - c1.y * c2.y, c1.x * c2.y + c1.y * c2.x);
}
friend Complex operator/(const Complex &c1, const Complex &c2)
{
double denom = c2.x * c2.x + c2.y * c2.y;
return Complex((c1.x * c2.x + c1.y * c2.y) / denom, (c1.y * c2.x - c1.x * c2.y) / denom);
}
friend bool operator==(const Complex &c1, const Complex &c2)
{
return c1.x == c2.x && c1.y == c2.y;
}
friend bool operator!=(const Complex &c1, const Complex &c2)
{
return !(c1 == c2);
}
friend ostream &operator<<(ostream &out, const Complex &c)
{
out << "(" << c.x << ", " << c.y << ")";
return out;
}
friend istream &operator>>(istream &in, Complex &c)
{
in >> c.x >> c.y;
return in;
}
};
int main()
{
Complex c1, c2;
cin >> c1 >> c2;
cout << "c1 = " << c1 << "\n"
<< "c2 = " << c2 << endl;
cout << "c1+c2 = " << c1 + c2 << endl;
cout << "c1-c2 = " << c1 - c2 << endl;
cout << "c1*c2 = " << c1 * c2 << endl;
cout << "c1/c2 = " << c1 / c2 << endl;
cout << (c1 += c2) << endl;
cout << (c1 -= c2) << endl;
cout << (c1 *= c2) << endl;
cout << (c1 /= c2) << endl;
cout << (c1 == c2) << " " << (c1 != c2) << endl;
return 0;
}