-
Notifications
You must be signed in to change notification settings - Fork 0
/
Frac.py
103 lines (83 loc) · 1.97 KB
/
Frac.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
import sys
"""
Created on Sept 15 10:40:20 2021
@author: Fabrice Issac, Stephane Lamassé
Institutions: Pireh-Lamop
LICENCE GNU
This script aims to generate fraction conform to Chuquet's work
"""
__version__ = "0.1.0"
__authors__ = "Stephane Lamassé", "Fabrice Issac"
class Frac(object):
def __init__(self,n,d):
self.n = n
if d != 0:
self.d = d
else:
raise ValueError('Division par 0')
if self.d < 0:
self.n = - self.n
self.d = - self.d
self.__reduction__()
# affichage
def __str__(self):
if self.n == 0:
return "0"
elif self.d == 1:
return str(self.n)
else:
return str(self.n)+"/"+str(self.d)
# reduction de la fraction (Euclide)
def __reduction__(self):
a = abs(max(self.d,self.n))
b = abs(min(self.d,self.n))
while b != 0:
tmp = b
b = a % b
a = tmp
self.n = self.n // a
self.d = self.d // a
# surcharge addition
def __add__(self,f):
n = self.n * f.d + f.n * self.d
d = self.d * f.d
return Frac(n,d)
# surcharge soustraction
def __sub__(self,f):
n = self.n * f.d - f.n * self.d
d = self.d * f.d
return Frac(n,d)
# surcharge multiplication
def __mul__(self,f):
n = self.n * f.n
d = self.d * f.d
return Frac(n,d)
# surcharge puissance
def __pow__(self,p):
return Frac(self.n**p,self.d**p)
# surcharge de l'opérateur & pour le calcul du nombre moyen par ajout des numérateurs et dénominateurs
def __and__(self,f):
return Frac(self.n+f.n,self.d+f.d)
# surcharge opérateurs de comparaison
def __lt__(self,f):
if isinstance(f,Frac):
f = f.n/self.d
return self.n/self.d < f
def __gt__(self,f):
if isinstance(f,Frac):
f = f.n/self.d
return self.n/self.d > f
def __le__(self,f):
if isinstance(f,Frac):
f = f.n/self.d
return self.n/self.d <= f
def __ge__(self,f):
if isinstance(f,Frac):
f = f.n/self.d
return self.n/self.d >= f
# convertion en nombre à virgule
def float(self):
return self.n/self.d
if __name__ == '__main__':
pass