import sys import math #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% '''rational.py: Module to do rational arithmetic. For full documentation, see http://www.nmt.edu/tcc/help/lang/python/examples/rational/. Exports: gcd ( a, b ): [ a and b are integers -> return the greatest common divisor of a and b ] Rational ( a, b ): [ (a is a nonnegative integer) and (b is a positive integer) -> return a new Rational instance with numerator a and denominator b ] .n: [ the numerator ] .d: [ the denominator ] .__add__(self, other): [ other is a Rational instance -> return the sum of self and other as a Rational instance ] .__sub__(self, other): [ other is a Rational instance -> return the difference of self and other as a Rational instance ] .__mul__(self, other): [ other is a Rational instance -> return the product of self and other as a Rational instance ] .__div__(self, other): [ other is a Rational instance -> return the quotient of self and other as a Rational instance ] .__str__(self): [ return a string representation of self ] .__float__(self): [ return a float approximation of self ] .mixed(self): [ return a string representation of self as a mixed fraction ] ''' def gcd ( a, b ): '''Greatest common divisor function; Euclid's algorithm. [ a and b are integers -> return the greatest common divisor of a and b ] ''' if b == 0: return a else: return gcd(b, a%b) class Rational: """An instance represents a rational number. """ def __init__ ( self, a, b ): """Constructor for Rational. """ if b == 0: raise ZeroDivisionError, ( "Il denominatore non puo' essere zero.") else: g = gcd ( a, b ) self.n = a / g self.d = b / g def __add__ ( self, other ): """Add two rational numbers. """ return Rational ( self.n * other.d + other.n * self.d, self.d * other.d ) def __sub__ ( self, other ): """Return self minus other. """ return Rational ( self.n * other.d - other.n * self.d, self.d * other.d ) def __mul__ ( self, other ): """Implement multiplication. """ return Rational ( self.n * other.n, self.d * other.d ) def __div__ ( self, other ): """Implement division. """ return Rational ( self.n * other.d, self.d * other.n ) def __str__ ( self ): '''Display self as a string. ''' return "%d/%d" % ( self.n, self.d ) def __float__ ( self ): """Implement the float() conversion function. """ return float ( self.n ) / float ( self.d ) def mixed ( self ): """Render self as a mixed fraction in string form. """ #-- 1 -- # [ whole := self.n / self.d, truncated # n2 := self.n % self.d ] whole, n2 = divmod ( self.n, self.d ) #-- 2 -- # [ if (whole is zero) and (n2 is zero) -> # return "0" # else if (whole is zero) and (n2 is nonzero) -> # return str(n2)+"/"+str(self.d) # else if n2 is zero -> # return str(whole) # else -> # return str(whole)+" and "+str(n2)+"/"+str(self.d) ] if whole == 0: if n2 == 0: return "0" else: return ("%s/%s" % (n2, self.d) ) else: if n2 == 0: return str(whole) else: return ("%s and %s/%s" % (whole, n2, self.d) ) #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% def scelta(): scelta=input("\nVuoi fare un'altra operazione?\n\nSe vuoi continuare premi 1 mentre se vuoi chiudere il programma digita 0\n\n") if scelta: tipologia() else: print "\nCiao ciao\nAlla prossima\n" sys.exit() def tipologia(): tipologia =input("\nChe operazione vuoi fare tra le due frazioni?\n\n1.) Somma\n2.) Sottrazione\n3.) Prodotto\n4.) Divisione\n\nFai la tua scelta e premi invio\n\n") if tipologia == 1: somma() if tipologia == 2: sottrazione() if tipologia == 3: prodotto() if tipologia == 4: divisione() if tipologia == 5: sys.exit() def somma(): n1=input("\nInserisci il primo numeratore: ") d1=input("Inserisci il primo denominatore: ") n2=input("Inserisci il secondo numeratore: ") d2=input("Inserisci il secondo denominatore: ") fra1=Rational( n1, d1 ) fra2=Rational( n2, d2 ) somma=fra1+fra2 print "\nLa somma tra le due frazioni e': " ,somma def sottrazione(): n1=input("\nInserisci il primo numeratore: ") d1=input("Inserisci il primo denominatore: ") n2=input("Inserisci il secondo numeratore: ") d2=input("Inserisci il secondo denominatore: ") fra1=Rational( n1, d1 ) fra2=Rational( n2, d2 ) sottrazione=fra1-fra2 print "\nLa differenza tra le due frazioni e': " ,sottrazione def prodotto(): n1=input("\nInserisci il primo numeratore: ") d1=input("Inserisci il primo denominatore: ") n2=input("Inserisci il secondo numeratore: ") d2=input("Inserisci il secondo denominatore: ") fra1=Rational( n1, d1 ) fra2=Rational( n2, d2 ) prodotto=fra1*fra2 print "\nIl prodotto tra le due frazioni e': " ,prodotto def divisione(): n1=input("\nInserisci il primo numeratore: ") d1=input("Inserisci il primo denominatore: ") n2=input("Inserisci il secondo numeratore: ") d2=input("Inserisci il secondo denominatore: ") fra1=Rational( n1, d1 ) fra2=Rational( n2, d2 ) divisione=fra1/fra2 print "\nLa divisione tra le due frazioni e': " ,divisione print "\nBenvenuto nel programma che esegue operazioni sulle frazioni" while True: scelta()