Skip to content

Instantly share code, notes, and snippets.

@ashvini-maurya
Created May 5, 2018 11:14
Show Gist options
  • Save ashvini-maurya/1d928e63492a6bc99d29fc38a6b07a7b to your computer and use it in GitHub Desktop.
Save ashvini-maurya/1d928e63492a6bc99d29fc38a6b07a7b to your computer and use it in GitHub Desktop.
def gcd(n, d):
while n != d:
if n > d:
n = n - d
else:
d = d - n
return n
class Fraction:
def __init__(self, n, d):
self.numerator = int(n / gcd(abs(n), abs(d)))
self.denomerator = int(d / gcd(abs(n), abs(d)))
if self.denomerator < 0:
self.denomerator = abs(self.denomerator)
self.numerator = -1*self.numerator
elif self.denomerator == 0:
raise ZeroDivisionError
def Add(self, other):
return self.numerator*other.denomerator + self.denomerator*other.numerator, self.denomerator*other.denomerator
def Sub(self, other):
return self.numerator*other.denomerator - self.denomerator*other.numerator, self.denomerator*other.denomerator
def Mul(self, other):
return self.numerator*other.numerator, self.denomerator*other.denomerator
def Div(self, other):
return self.numerator*other.denomerator, self.denomerator*other.numerator
def main():
fA = Fraction(3,7) #from the given input
fB = Fraction(3,8) #from the given input
f3 = Fraction.Add(fA,fB)
f4 = Fraction.Sub(fA,fB)
f5 = Fraction.Mul(fA,fB)
f6 = Fraction.Div(fA,fB)
f8 = 0
print (f3)
print (f4)
print (f5)
print (f6)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment