Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. class Fraction:
  2.     #Constructor. Puts fraction in simplest form
  3.     def __init__(self,a,b):
  4.         self.num = a
  5.         self.den = b
  6.         self.simplify()
  7.     #Print Fraction as a String
  8.     def __str__(self):
  9.         if self.den==1:
  10.             return str(self.num)
  11.         else:
  12.             return str(self.num)+"/"+str(self.den)
  13.     #Get the Numerator
  14.     def getNum(self):
  15.         return self.num
  16.     #Get the Denominator
  17.     def getDen(self):
  18.         return self.den
  19.     #Give Numerical Approximation of Fraction
  20.     def approximate(self):
  21.         return self.num/self.den
  22.     #Simplify fraction
  23.     def simplify(self):
  24.         x = self.gcd(self.num,self.den)
  25.         self.num = self.num // x
  26.         self.den = self.den // x
  27.     #Find the GCD of a and b
  28.     def gcd(self,a,b):
  29.         if b==0:
  30.             return a
  31.         else:
  32.             return self.gcd(b,a % b)
  33.     #Complete these methods in lab
  34.     def __add__(self,other):
  35.         return 0
  36.     def __sub__(self,other):
  37.         return 0
  38.     def __mul__(self,other):
  39.         return 0
  40.     def __truediv__(self,other):
  41.         return 0
  42.     def __pow__(self,exp):
  43.         return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement