from math import * # finds Greatest Common Divisor for two integers 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 # Fraction class 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 # the sign is always in numerator 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) #! in a mixed number the sign is stored with the whole part def fraction2mixed(self): if abs(self._n) < self._d: # used abs to take into account negative fractions 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: if self._n < 0: # if the fraction's numerator is negative, # the sign will be stored in the whole part of the mixed number return MixedNumber(-(abs(self._n)//self._d),abs(self._n)%self._d,self._d) else: return MixedNumber(self._n//self._d,self._n%self._d,self._d) # Mixed Numbers Class 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 if self._w < 0: self._w = -(abs(self._w) + num//denom) else: 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): if self._w < 0: # if the mixed number is negative return -(Fraction(abs(self._w),1) + self._f) else: # if the mixed number is positive return Fraction(self._w,1) + self._f def __add__(self,other): # Addition of two mixed numbers or #a mixed number and a fraction, or ... a=self.mixed2fraction() # first mixed number is converted to a fraction if isinstance(other,MixedNumber): b=other.mixed2fraction() # second mixed number is covnerted to a fraction c=a+b elif isinstance(other,Fraction): # if second number is a fraction c=a+other elif isinstance(other,int): # if second number is integer 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 mixed numbers a=self.mixed2fraction() # first mixed number is converted to a fraction if isinstance(other,MixedNumber): b=other.mixed2fraction() # second mixed number is covnerted to a fraction c=a-b elif isinstance(other,Fraction): # if second number is a fraction c=a-other elif isinstance(other,int): # if second number is integer b=Fraction(other,1) c=a-b else: print 'Tried to perform the following operation:',self,' - ',other raiseTypeError('subtraction cannot be done') return c.fraction2mixed() def __mul__(self,other): # Multiplication of two mixed numbers a=self.mixed2fraction() if isinstance(other,MixedNumber): b=other.mixed2fraction() 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('Cannot find the product of these items') return c.fraction2mixed() def __div__(self,other): # division of two mixed numbers a=self.mixed2fraction() if isinstance(other,MixedNumber): b=other.mixed2fraction() 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('Division cannot be performed') return c.fraction2mixed() def __neg__(self): # negation of a mixed number return MixedNumber(-self._w, self._f._n,self._f._d) # not needed #def n_invert(self): # negative reciprocal of a fraction # pass def main(): print "Adding a negative mixed number and a positive mixed number" m1=MixedNumber(-2,5,4) m2=MixedNumber(1,2,3) print m1, '+', m2, " is ",(m1+m2) tt=raw_input('Press Enter to continue') print "Converting a negative fraction to a negative mixed number" f=Fraction(-14,12) m=f.fraction2mixed() print f, " is ", m tt=raw_input('Press Enter to continue') print "Adding a negative mixed number and a fraction" f1=Fraction(2,3) m3=m1+f1 print m1, " + ", f1, " is ",m3 tt=raw_input('Press Enter to continue') print "Negating a mixed number" print '-(',m1,") is ", -m1 tt=raw_input('Press Enter to continue') print "Multiplying two mixed numbers" m4 = m1*m2 print m1," * ", m2, " is",m4 tt=raw_input('Press Enter to continue') print "Dividing a mixed number by a mixed number" m5=m1/m2 print m1,' / ',m2,' is ', m5 tt=raw_input('Press Enter to continue') print "Subtracting two mixed numbers" m6=m1-m2 print m1,' - ',m2,' is ', m6 main()