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,quotient=0,num=0,denom=1): # initially, by default, we have 0 0/1 self._q=quotient if num < denom: # fractional part if a proper fraction Fraction.__init__(self,num,denom) else: # takes care of improper fraction as a fractional part # of the mixed number self._q += num // denom r=num%denom Fraction.__init__(self,r,denom) def __str__(self): if self._d == 0: # if denominator is 0, the mixed number is undefined return 'Undefined' elif self._n == 0: # if the numerator is 0 if self._q == 0: # and the whole part(quotient) is 0 return str(0) else: # and the whole part (quotient) is not 0 return str(self._q) else: str1=' ' + str(self._n) + '\n' str2=str(self._q)+' -\n' str3=' ' + str(self._d) return str1+str2+str3+'\n' #return str(self._q) + ' ' + str(self._n) + ' ' + str(self._d) # conversion of a mixed number to improper fraction def mixed2fraction(self): return Fraction(self._q*self._d+self._n,self._d) 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 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\n", m3 tt=raw_input('Press Enter to continue') print "Adding two mixed numbers" m4=m1+m2 print m1, " +\n", m2, " is\n",m4 tt=raw_input('Press Enter to continue') print "Adding a mixed number and a fraction" m5 = m1 + f3 print m1," + ", f3, " is\n", m5 tt=raw_input('Press Enter to continue') print "Adding a mixed number and an integer" m6 = m1+2 print m1," + ", 2, " is\n",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()