karolinagergert

Untitled

Jul 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1.  
  2. # -*- coding: utf-8 -*-
  3. from functools import total_ordering
  4. # ДЗ НА СТРОКАХ 24, 29, 32, 37 !!!
  5.  
  6. @total_ordering
  7. class Fraction:
  8. def __init__(self, numerator, denominator): #numerator denominator
  9. numerator/denominator
  10. self.a = numerator
  11. self.b = denominator
  12.  
  13. def reduce(self): #сокращение
  14. while b!=0:
  15. a,b = b,a%b
  16. self.numerator /= a
  17. self.denominator /= a
  18. return self
  19.  
  20. def __add__(self, other): #+
  21. if type(other) == int:
  22. other = Fraction(other,1)
  23. return Fraction(self.numerator * other.denominator + self.denominator * other.numerator, self.denominator * other.denominator)
  24.  
  25. def __sub__(self, other): #-
  26. if type(other) == int:
  27. other = Fraction(other,1)
  28. return Fraction(self.numerator * other.denominator - self.denominator * other.numerator, self.denominator * other.denominator)
  29.  
  30. def __neg__(self): #унарный -
  31. return Fraction(-self.numerator, -self.denominator)
  32.  
  33. def __mul__(self, other): #*
  34. self._reduce()
  35. other._reduce()
  36. return self.numerator * other.numerator + '/' + (self.denominatior * other.denominatior)
  37.  
  38. def __int__(self): #приведение к int
  39. if type(self) == int or self==0:
  40. self=self.numerator
  41. return Fractoin(self)
  42. if self.numerator >= self.denominator:
  43. self.numerator=self.numerator%self.denominator
  44. return Fraction(self.numerator//self.denominator, self.numerator + '/' + self.denominatior)
  45.  
  46. def __float__(self): #дробь
  47. self.numerator/ self.denominator
  48. return(self)
  49.  
  50. def __radd__(self,other): #смена мест
  51. return self+other
  52.  
  53. def __str__(self): #вывод
  54. self._reduce()
  55. return str(self.numerator)+ '/' + str(self.denominator)
  56.  
  57. def __eq__(self,other): #==
  58. self._reduce()
  59. other._reduce()
  60. return self.numerator == other.numerator and self.denuminator == other.denominator
  61.  
  62. def __lt__(self, other): #<
  63. self._reduce()
  64. other._reduce()
  65. return self.numerator * other.denominator - self.denominatior * other.numerator < 0
Advertisement
Add Comment
Please, Sign In to add comment