# Untitled

Mar 8th, 2021
724
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2.
3.
4. class Complex
5. {
6.     private:
7.
8.     double real, unreal;
9.
10.     public:
11.
12.     Complex(double real, double unreal)
13.     {
14.         this->real = real;
15.         this->unreal = unreal;
16.     }
17.
18.     Complex(double real) : Complex(real, 1.0) {}
19.
20.     Complex() :Complex(0., 0.) {}
21.
22.     ~Complex() {}
23.
24.     double getReal() const { return real; }
25.     double getUnreal() const { return unreal; }
26.
27.     void setReal(double real) { this->real = real; }
28.     void setUnreal(double unreal) { this->unreal = unreal; }
29.
31.
32.     Complex operator+(Complex& c)
33.     {
34.         Complex result(real + c.real, unreal + c.unreal);
35.         return result;
36.     }
37.
38.     Complex operator-(Complex& c)
39.     {
40.         Complex result(real - c.real, unreal - c.unreal);
41.         return result;
42.     }
43.
44.     Complex operator*(Complex& c)
45.     {
46.         double cReal = c.real, cUnreal = c.unreal;
47.         Complex result(real * cReal - unreal * cUnreal, real * cUnreal + cReal * unreal);
48.         return result;
49.     }
50.
51.     Complex operator/(Complex& c)
52.     {
53.         double cReal = c.real, cUnreal = c.unreal;
54.         Complex result((real * cReal + unreal * cUnreal) / (cReal * cReal + cUnreal * cUnreal), (cReal * unreal - real * cUnreal) / (cReal * cReal + cUnreal * cUnreal));
55.         return result;
56.     }
57.
58.     // operations with array
59.
60.     Complex sum(Complex *arr, size_t s)
61.     {
62.         Complex res = 0;
63.         for (size_t i = 0; i < s; ++i) res = res + arr[i];
64.         return res;
65.     }
66.
67.     Complex dif(Complex* arr, size_t s)
68.     {
69.         Complex res = 0;
70.         for (size_t i = 0; i < s; ++i) res = res - arr[i];
71.         return res;
72.     }
73.
74.     Complex compos(Complex* arr, size_t s)
75.     {
76.         Complex res(1., 1.);
77.         for (size_t i = 0; i < s; ++i) res = res * arr[i];
78.         return res;
79.     }
80.
81.     Complex div(Complex* arr, size_t s)
82.     {
83.         Complex res(1., 1.);
84.         for (size_t i = 0; i < s; ++i) res = res / arr[i];
85.         return res;
86.     }
87. };
88.
89. std::ostream& operator<<(std::ostream& out, const Complex& c)
90. {
91.     double Real = c.getReal(), Unreal = c.getUnreal();
92.
93.     if (Unreal >= 0) return out << Real << " + " << Unreal << "i";
94.     else return out << Real << " - " << -Unreal << "i";
95. }
96.
97.
98.
99.
100. int main()
101. {
102.
103.
104.     return 0;
105. }
RAW Paste Data