﻿

# Untitled

a guest
Dec 11th, 2019
106
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2. using namespace std;
3.
4. class Complex {
5. public:
6.
7. int x, y;
8.
9. Complex(int r, int i)
10. {
11. x = r;
12. y = i;
13. }
14.
15. Complex operator+ (Complex z2)
16. {
17. return Complex(x + z2.x, y + z2.y);
18. }
19.
20. Complex operator- (Complex z2)
21. {
22. return Complex(x - z2.x, y - z2.y);
23. }
24.
25. Complex operator* (int c)
26. {
27. return Complex(x * c, y * c);
28. }
29.
30. Complex operator* (Complex z2)
31. {
32. return Complex(x * z2.x - y * z2.y, x * z2.y + y * z2.x);
33. }
34.
35. Complex operator/ (Complex z2)
36. {
37. return Complex((x * z2.x + y * z2.y) / (z2.x * z2.x + z2.y * z2.y), (x * z2.y + z2.x * y) / (z2.x * z2.x + z2.y * z2.y));
38. }
39.
40. void Print()
41. {
42. cout << x << "+i*" << y;
43. }
44. };
45.
46. Complex operator*(int c, Complex z1)
47. {
48. return Complex(z1.x*c, z1.y * c);
49. }
50.
51. int main()
52. {
53. const int c = 2;
54.
55. Complex FirstNumber(10, 9), SecondNumber(4, 6);
56.
57. // Сумма
58. FirstNumber.Print(); cout << " + "; SecondNumber.Print(); cout << " = "; Complex Sum = FirstNumber + SecondNumber; Sum.Print(); cout << endl;
59.
60. // Разность
61. FirstNumber.Print(); cout << " - "; SecondNumber.Print(); cout << " = "; Complex Minus = FirstNumber - SecondNumber; Minus.Print(); cout << endl;
62.
63. // Произведение
64. FirstNumber.Print(); cout << " * "; SecondNumber.Print(); cout << " = "; Complex mply = FirstNumber * SecondNumber; mply.Print(); cout << endl;
65.
66. // Частное
67. FirstNumber.Print(); cout << " / "; SecondNumber.Print(); cout << " = "; Complex Div = FirstNumber / SecondNumber; Div.Print(); cout << endl;
68.
69. // Умножение на числа на константу
70. FirstNumber.Print(); cout << " * " << c << " = "; Complex C1_mply = FirstNumber * c; C1_mply.Print(); cout << endl;
71.
72. // Умножение константы на число
73. cout << c << " * "; FirstNumber.Print(); cout << " = "; Complex C2_mply = c * FirstNumber; C2_mply.Print(); cout << endl;
74.
75.
76. system("pause");
77. }
RAW Paste Data