Advertisement
Guest User

inobiou

a guest
Dec 16th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. import math
  2.  
  3. class Frac():
  4. def __init__(self,num,denom=1):
  5. self.num = num
  6. self.denom = denom
  7.  
  8. def __add__(self, other): #gère le +
  9. if type(other)!=Frac:#Si on ajoute un entier à une fraction
  10. other = Frac(other)#On le convertit en fraction
  11. nouveauNum = self.num*other.denom+other.num*self.denom #calcul du numérateur
  12. nouveauDenom = self.denom*other.denom #calcul du dénominateur
  13. simplification = math.gcd(nouveauNum,nouveauDenom) #plus grand commun diviseur
  14. nouveauNum = nouveauNum//simplification #simplification entière du numérateur
  15. nouveauDenom = nouveauDenom//simplification #simplification entière du dénominateur
  16. return Frac(nouveauNum,nouveauDenom) #on renvoie le résultat
  17. def __sub__(self, other): #gère le - (soustraction)
  18. pass #à compléter
  19. def __mul__(self, other): #gère le *
  20. pass #à compléter
  21. def __truediv__(self, other): #gère le /
  22. pass #à compléter
  23. def __lt__(self, other): #gère le <
  24. pass #à compléter
  25. def __le__(self, other): #gère le <=
  26. pass #à compléter
  27. def __eq__(self, other): #gère le ==
  28. pass #à compléter
  29. def __ne__(self, other): #gère le !=
  30. pass #à compléter
  31. def __gt__(self, other): #gère le >
  32. pass #à compléter
  33. def __ge__(self, other): #gère le >=
  34. pass #à compléter
  35. def __neg__(self): #gère le - (opposé)
  36. pass #à compléter
  37. def __str__(self): #gère le print() et le str()
  38. return str(self.num)+"/"+str(self.denom)
  39.  
  40. a = Frac(1,3)+Frac(1,6)
  41. print(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement