Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Complex {
- public:
- int x, y;
- Complex(int r, int i)
- {
- x = r;
- y = i;
- }
- Complex operator+ (Complex z2)
- {
- return Complex(x + z2.x, y + z2.y);
- }
- Complex operator- (Complex z2)
- {
- return Complex(x - z2.x, y - z2.y);
- }
- Complex operator* (int c)
- {
- return Complex(x * c, y * c);
- }
- Complex operator* (Complex z2)
- {
- return Complex(x * z2.x - y * z2.y, x * z2.y + y * z2.x);
- }
- Complex operator/ (Complex z2)
- {
- 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));
- }
- void Print()
- {
- cout << x << "+i*" << y;
- }
- };
- Complex operator*(int c, Complex z1)
- {
- return Complex(z1.x*c, z1.y * c);
- }
- int main()
- {
- const int c = 2;
- Complex FirstNumber(10, 9), SecondNumber(4, 6);
- // Сумма
- FirstNumber.Print(); cout << " + "; SecondNumber.Print(); cout << " = "; Complex Sum = FirstNumber + SecondNumber; Sum.Print(); cout << endl;
- // Разность
- FirstNumber.Print(); cout << " - "; SecondNumber.Print(); cout << " = "; Complex Minus = FirstNumber - SecondNumber; Minus.Print(); cout << endl;
- // Произведение
- FirstNumber.Print(); cout << " * "; SecondNumber.Print(); cout << " = "; Complex mply = FirstNumber * SecondNumber; mply.Print(); cout << endl;
- // Частное
- FirstNumber.Print(); cout << " / "; SecondNumber.Print(); cout << " = "; Complex Div = FirstNumber / SecondNumber; Div.Print(); cout << endl;
- // Умножение на числа на константу
- FirstNumber.Print(); cout << " * " << c << " = "; Complex C1_mply = FirstNumber * c; C1_mply.Print(); cout << endl;
- // Умножение константы на число
- cout << c << " * "; FirstNumber.Print(); cout << " = "; Complex C2_mply = c * FirstNumber; C2_mply.Print(); cout << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement