-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtema5.py
More file actions
64 lines (46 loc) · 1.87 KB
/
tema5.py
File metadata and controls
64 lines (46 loc) · 1.87 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# for finding the greatest common divisor of two integer numbers
from math import gcd
class Fractie:
# Initialize the fraction
def __init__(self, numarator, numitor):
self.numarator = numarator
self.numitor = numitor
# Get the fraction as a string
def __str__(self):
return f'{self.numarator}/{self.numitor}'
# Add two fractions
def __add__(self, other):
# Get the greatest common divisor of the two denominators
cmmdc = gcd(self.numitor, other.numitor)
numarator = self.numarator * \
(other.numitor // cmmdc) + other.numarator * (self.numitor // cmmdc)
numitor = self.numitor * other.numitor // cmmdc
return Fractie.simplify(numarator, numitor)
# Subtract two fractions
def __sub__(self, other):
# Get the greatest common divisor of the two denominators
cmmdc = gcd(self.numitor, other.numitor)
numarator = self.numarator * \
(other.numitor // cmmdc) - other.numarator * (self.numitor // cmmdc)
numitor = self.numitor * other.numitor // cmmdc
return Fractie.simplify(numarator, numitor)
# Get the inverse of the fraction
def inverse(self):
return Fractie.simplify(self.numitor, self.numarator)
# Simplify a fraction
@staticmethod
def simplify(numarator, numitor):
cmmdc = gcd(numarator, numitor)
return Fractie(numarator // cmmdc, numitor // cmmdc)
def main():
# Define two different fractions
f1 = Fractie(1, 12)
f2 = Fractie(1,4)
# Print the sum of the two fractions
print(f'Suma celor doua fractii este: {f1 + f2}')
# Print the difference of the two fractions
print(f'Diferenta celor doua fractii este: {f2 - f1}')
# Print the inverse of the first fraction
print(f'Inversa primei fractii este: {f1.inverse()}')
if __name__ == '__main__':
main()