class Rational:
def __init__(self,num,denom):
self._numerator = num
self._denominator = denom
self.reduce()
def add(self,f):
num = self._numerator * f._denominator + f._numerator * self._denominator
denom = self._denominator * f._denominator
return Fraction(num,denom)
def multiply(self, f):
num = self._numerator * f._numerator
denom = self._denominator * f._denominator
return Fraction(num,denom)
def subtract(self, f):
num = self._numerator * f._denominator - f._numerator * self._denominator
denom = self._denominator * f._denominator
return Fraction(num,denom)
def divide(self, f):
num = self._numerator * f._denominator
denom = self._denominator * f._numerator
return Fraction(num,denom)
def get_numerator(self):
return self._numerator
##def set_numerator(self,n):
## self._numerator = n
def get_denominator(self):
return self._denominator
@staticmethod
def gcd(m,n):
while n != 0:
r = m%n
m = n
n = r
return m
def reduce(self):
g = Fraction.gcd(self._numerator,self._denominator)
if g != 0:
self._numerator = self._numerator / g
self._denominator = self._denominator / g
if self._denominator < 0:
self._numerator = -self._numerator
self._denominator = -self._denominator
def __str__(self):
if self._denominator == 1:
return str(int(self._numerator))
else:
return str(int(self._numerator)) + "/" + str(int(self._denominator))