-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL8T7.py
More file actions
46 lines (37 loc) · 2.08 KB
/
L8T7.py
File metadata and controls
46 lines (37 loc) · 2.08 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
# Урок 8. Практическое задание 7.
# ФИО: Артур Назарян
# Курс: Основы языка Python
# Факультет: Geek University Python-разработки
#
# Реализовать проект «Операции с комплексными числами». Создайте класс «Комплексное число», реализуйте
# перегрузку методов сложения и умножения комплексных чисел. Проверьте работу проекта, создав
# экземпляры класса (комплексные числа) и выполнив сложение и умножение созданных экземпляров.
# Проверьте корректность полученного результата.
class ComplexDigitStd:
"""Работа с комплексными числами с использованием стандартных функций Python"""
def __init__(self, num1, num2):
self.num = complex(num1, num2)
def __add__(self, other):
return f'{self.num + other.num}'
def __mul__(self, other):
return f'{self.num * other.num}'
class ComplexDigitMy:
"""Самостоятельная реализация работы с комплексными числами"""
def __init__(self, num1, num2):
self.num1 = num1
self.num2 = num2
def __add__(self, other):
return f'Сумма ({self.num1}{self.num2:+}j) и ({other.num1}{other.num2:+}j) равна ' \
f'({self.num1 + other.num1}{self.num2 + other.num2:+}j)'
def __mul__(self, other):
return f'Произведение ({self.num1}{self.num2:+}j) и ({other.num1}{other.num2:+}j)' \
f' равно ({self.num1 * other.num1 + self.num2 * other.num2 * -1}' \
f'{self.num1 * other.num2 + self.num2 * other.num1:+}j)'
n_1 = ComplexDigitStd(3, 1)
n_2 = ComplexDigitStd(2, -3)
print(n_1 + n_2)
print(n_1 * n_2)
n_1 = ComplexDigitMy(3, 1)
n_2 = ComplexDigitMy(2, -3)
print(n_1 + n_2)
print(n_1 * n_2)