Advertisement
Bad_Programist

Untitled

Feb 27th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. def gcd(a, b):
  2.     while b > 0:
  3.         a, b = b, a%b
  4.     return a
  5.  
  6. class Fraction(object):
  7.    
  8.     def __init__(self, numerator, denumerator):
  9.         numerator // denumerator # проверка на равенство нулю знаменатель
  10.         cur_gcd = gcd(numerator, denumerator)
  11.         self.numerator = numerator // cur_gcd
  12.         self.denumerator = denumerator // cur_gcd
  13.        
  14.     def __str__(self):
  15.         if self.denumerator < 0:
  16.             return f'{-self.numerator} / {-self.denumerator}'
  17.         return (f'{self.numerator} / {self.denumerator}')
  18.    
  19.     def __add__(self, second):
  20.         return Fraction(self.numerator*second.denumerator + self.denumerator * second.numerator,
  21.                        self.denumerator * second.denumerator)
  22.    
  23.     def __sub__(self, second):
  24.         return Fraction(self.numerator*second.denumerator - self.denumerator * second.numerator,
  25.                        self.denumerator * second.denumerator)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement