-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfraction.py
87 lines (69 loc) · 2.14 KB
/
fraction.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import math
class Fraction:
def __init__(self, nom, denom):
self.nom = nom
self.denom = denom
def __str__(self):
return f"{self.nom}/{self.denom}"
def simplify(self):
found = False
nom = math.fabs( self.nom )
denom = math.fabs( self.denom )
while not found:
if nom == denom:
found = True
else:
if nom > denom:
nom = nom - denom
else:
denom = denom - nom
else:
self.nom = int(self.nom/nom)
self.denom = int(self.denom/nom)
@staticmethod
def sum(numb1, numb2):
denom = numb1.denom*numb2.denom
nom = numb1.nom*numb2.denom + numb2.nom*numb1.denom
return Fraction(nom, denom)
@staticmethod
def difference(numb1, numb2):
denom = numb1.denom * numb2.denom
nom = numb1.nom * numb2.denom - numb2.nom * numb1.denom
return Fraction(nom, denom)
@staticmethod
def multiplication(numb1, numb2):
nom = numb1.nom*numb2.nom
denom = numb1.denom*numb2.denom
return Fraction(nom, denom)
@staticmethod
def division(numb1, numb2):
nom = numb1.nom*numb2.denom
denom = numb1.denom*numb2.nom
return Fraction(nom, denom)
def main():
fract1 = Fraction(7, 21)
print(f"ułamek: {fract1}")
fract2 = Fraction(28, 70)
print(f"ułamek: {fract2}")
print("----\ndziałania:")
sum = Fraction.sum(fract1, fract2)
sum.simplify()
print(f"{fract1} + {fract2} = {sum}")
diff = Fraction.difference(fract1, fract2)
diff.simplify()
print(f"{fract1} - {fract2} = {diff}")
multiplication = Fraction.multiplication(fract1, fract2)
multiplication.simplify()
print(f"{fract1} * {fract2} = {multiplication}")
division = Fraction.division(fract1, fract2)
division.simplify()
print(f"{fract1} : {fract2} = {division}")
print('----')
print(f"skracanie {fract1}")
fract1.simplify()
print(fract1)
print(f"skracanie {fract2}")
fract2.simplify()
print(fract2)
if __name__ == "__main__":
main()