-
Notifications
You must be signed in to change notification settings - Fork 0
/
Giang_Wk9_Ex.py
73 lines (54 loc) · 1.66 KB
/
Giang_Wk9_Ex.py
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
# Alex Giang
# Professor Abolghasemi
# CIS 502
# 11 March 2024
# Week 8, Exercise
# This file is intended to demonstrate beginning knowledge in implementing
# operator overloading.
import threading
class ComplexNumber:
def __init__(self, real, imag=0):
self.re = real
self.im = imag
def __add__(self, other):
return ComplexNumber(
self.re + other.re,
self.im + other.im
)
def __sub__(self, other):
return ComplexNumber(
self.re - other.re,
self.im - other.im
)
def __mul__(self, other):
return ComplexNumber(
self.re * other.re - self.im * other.im,
self.re * other.im + self.im * other.re
)
def recip(self):
conj = ComplexNumber(self.re, -self.im)
den = (self * conj).re
return ComplexNumber(
conj.re / den,
conj.im / den
)
def __truediv__(self, other):
return self * ComplexNumber.recip(other)
def __str__(self):
return str(self.re) + ' + ' + str(self.im) + 'i'
if __name__ == "__main__":
num1 = ComplexNumber(2, 3)
num2 = ComplexNumber(1, 4)
# Perform addition
sum_result = num1 + num2
print("Addition Result:", sum_result)
# Perform subtraction
sub_result = num1 - num2
print("Subtraction Result:", sub_result)
# Perform multiplication
mul_result = num1 * num2
print("Multiplication Result:", mul_result)
# Perform division
div_result = num1 / num2
print("Division Result:", div_result)
# Perform division