Created
May 5, 2018 11:14
-
-
Save ashvini-maurya/1d928e63492a6bc99d29fc38a6b07a7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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