from math import * def gcd(a,b): u, v = a, b counter2=0; while v != 0: r = u % v #print "The remainder is", r u = v v = r counter2 +=1 return u class Fraction: def __init__(self,num=0,denom=1): if denom==0: # fraction is undefined self._n=0 self._d=0 else : factor = gcd(abs(num),abs(denom)) if denom < 0: factor=-factor self._n=num/factor self._d=denom/factor def __str__(self): if self._d == 0: return 'Undefined' elif self._d == 1 : return str(self._n) else: return str(self._n) + '/' + str(self._d) def __add__(self,other): # Addition of two fractions return Fraction(self._n*other._d+other._n*self._d,self._d*other._d) def __sub__(self,other): # Subtraction of two fractions return Fraction(self._n*other._d-other._n*self._d,self._d*other._d) def __mul__(self,other): # Multiplication of two fractions return Fraction(self._n*other._n,self._d*other._d) def __div__(self,other): # division of two fractions self/other return Fraction(self._n*other._d,self._d*other._n) def __neg__(self): # negation of a fraction return Fraction(-self._n,self._d) def n_invert(self): # negative reciprocal of a fraction return Fraction(self._d,self._n).__neg__() #return -Fraction(self._d,self._n) # conversion of a fraction to a mixed number # so far if a user tries to convert a proper fraction to a mixed number, # the warning is displayed, but the mixed number is still created, # with 0 as the whole part (quotient) def fraction2mixed(self): if self._n < self._d: print "Warning: you tried to convert a proper fraction to a mixed number!" print "The mixed number was created, but its whole part (quotient) is 0" return MixedNumber(0,self._n,self._d) else: return MixedNumber(self._n//self._d,self._n%self._d,self._d) class MixedNumber(Fraction): def __init__(self,w=0,num=0,denom=1): # initially, by default, we have 0 0/1 self._w=w if num >= denom: # if fractional part is improper fraction, fix it self._w += num//denom num = num%denom self._f=Fraction(num,denom) def __str__(self): if self._w == 0: return str(self._f) else: return str(self._w) + ' ' + str(self._f) # conversion of a mixed number to improper fraction def mixed2fraction(self): return Fraction(self._w,1) + self._f def __add__(self,other): # Addition of two mixed numbers or #a mixed number and a fraction a=self.mixed2fraction() print self, ' is ', a if isinstance(other,MixedNumber): b=other.mixed2fraction() print other, ' is ', b c=a+b elif isinstance(other,Fraction): c=a+other elif isinstance(other,int): b=Fraction(other,1) c=a+b else: print 'Tried to perform the following operation:',self,' + ',other raiseTypeError('These items cannot be added') return c.fraction2mixed() def __sub__(self,other): # Subtraction of two fractions pass def __mul__(self,other): # Multiplication of two fractions pass def __div__(self,other): # division of two mixed numbers a=self.mixed2fraction() print self, ' is ', a if isinstance(other,MixedNumber): b=other.mixed2fraction() print other, ' is ', b c=a/b elif isinstance(other,Fraction): c=a/other elif isinstance(other,int): b=Fraction(other,1) c=a/b else: print 'Tried to perform the following operation:',self,' / ',other raiseTypeError('These items cannot be added') return c.fraction2mixed() def __neg__(self): # negation of a fraction pass def n_invert(self): # negative reciprocal of a fraction pass def main(): print "Converting mixed numbers to improper fractions" # m1 = 2 5/4 - should be converted to 3 1/4 immediately m1=MixedNumber(2,5,4) m2=MixedNumber(1,2,3) # m2 = 1 2/3 print m1 print m2 f1=m1.mixed2fraction() print m1, " is ",f1 f2=m2.mixed2fraction() print m2, " is ",f2 tt=raw_input('Press Enter to continue') print "Converting fraction to a mixed number" f3=Fraction(4,12) print f3 m3=f3.fraction2mixed() print f3, " is ", m3 tt=raw_input('Press Enter to continue') print "Adding two mixed numbers" m4=m1+m2 print m1, " + ", m2, " is ",m4 tt=raw_input('Press Enter to continue') print "Adding a mixed number and a fraction" m5 = m1 + f3 print m1," + ", f3, " is", m5 tt=raw_input('Press Enter to continue') print "Adding a mixed number and an integer" m6 = m1+2 print m1," + ", 2, " is",m6 tt=raw_input('Press Enter to continue') print "Dividing a mixed number by a mixed number" m7=m1/m2 print m7 tt=raw_input('Press Enter to continue') print "Dividing a mixed number by 2" m8=m1/2 print m8 tt=raw_input('Press Enter to continue') print "Multiplication" m9=m1*m2 print m9 main()