Question
The code and its functions will need to be written in Python.
Define two functions, one for multiplying two fractions and the second, for dividing two fractions.The code written, should be folded into the current code for Fractions (shown below).
The code below is the one in which the new functions will need to be added to:
def gcd(m,n):
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
class Fraction:
def __init__(self,top,bottom):
self.num = top
self.den = bottom
def __str__(self):
return str(self.num)+"/"+str(self.den)
def __add__(self,otherfraction):
newnum = self.num*otherfraction.den +
self.den*otherfraction.num
newden = self.den * otherfraction.den
common = gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
def __eq__(self, other):
firstnum = self.num * other.den
secondnum = other.num * self.den
return firstnum == secondnum
def main():
x = Fraction(1,2)
y = Fraction(2,3)
print(x+y)
print(x == y)
main()
Explanation / Answer
def gcd(m,n): while m%n !=0: oldm = m oldn = n m = oldn n = oldm%oldn return n class Fraction: def __init__(self, top, bottom): self.num = top self.den = bottom def show(self): print self.num,"/",self.den def __str__(self): return str(self.num)+"/"+str(self.den) def __add__(self, otherfraction): newnum = self.num * otherfraction.den + self.den * otherfraction.num newden = self.den * otherfraction.den common = gcd(newnum,newden) return Fraction(newnum//common, newden//common) def __eq__(self, other): firstnum = self.num * other.den secondnum = other.num * self.den return firstnum == secondnum def __sub__(self, otherfraction): newnum = self.num * otherfraction.den - self.den * otherfraction.num newden = self.den * otherfraction.den common = gcd(newnum,newden) return Fraction(newnum//common, newden//common) def __mul__(self, otherfraction): newnum = self.num * otherfraction.num newden = self.den * otherfraction.den common = gcd(newnum,newden) return Fraction(newnum//common, newden//common) def __truediv__(self, otherfraction): newnum = self.num * otherfraction.den newden = self.den * otherfraction.num common = gcd(newnum, newden) return Fraction(newnum//common, newden//common) def __lt__(self, otherfraction): num1 = self.num * otherfraction.den num2 = otherfraction.num * otherfraction.num if num1