Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # supponiamo di voler implementare un sistema in cui sia possibile effettuare
- # operazioni tra frazioni aritmetiche
- #
- # cosa intendiamo con "frazione aritmetica"?
- # quali sono le operazioni che intendiamo effettuare?
- #
- # una [frazione aritmetica] è un numero rappresentato da un [segno], un
- # [numeratore] e un [denominatore]
- # il [valore numerico] associato alla frazione è dato dal rapporto tra
- # numeratore e denominatore.
- #
- # Frazione
- # segno -> può essere + o - (poiché non mi piace + e - dico + => 1 e - => -1)
- # numeratore -> qualsiasi numero intero non negativo
- # denominatore -> qualsiasi numero intero positivo
- #
- class Frazione:
- #segno = 1 # segno positivo (esempio)
- #numeratore = 0
- #denonimatore = 1
- def stampa_a_video(self): # metodo della classe Frazione
- if (self.__numeratore == 0):
- print(0)
- return
- if (self.__segno == -1):
- print("-",end="")
- print(int(self.__numeratore), end="")
- if (self.__denominatore != 1):
- print("/", end="")
- print(int(self.__denominatore))
- else:
- print()
- # def __init__(self): # costruttore di classe Frazione
- # print("Chiamata al costruttore")
- # self.segno = 1
- # self.numeratore = 0
- # self.denominatore = 1
- def __init__(self, numeratore=0, denominatore=1):
- print("Chiamata ad init")
- self.__segno = 1
- if (numeratore < 0):
- numeratore *= -1
- self.__segno = -1
- if (denominatore < 0):
- denominatore *= -1
- self.__segno *= -1
- self.__numeratore = numeratore
- self.__denominatore = denominatore
- self.__riduzione()
- def leggi_numeratore(self): # accesso in lettura ad una variabile di stato <--------------------- getter
- return self.__numeratore
- def imposta_numeratore(self, numeratore): # accesso in scrittura ad una variabile di stato <----- setter
- if (numeratore < 0):
- numeratore *= -1
- self.__segno = -1
- self.__numeratore = numeratore
- self.__riduzione()
- def leggi_denominatore(self): # accesso in lettura ad una variabile di stato <--------------------- getter
- return self.__denominatore
- def imposta_denominatore(self, denominatore): # accesso in scrittura ad una variabile di stato <----- setter
- if (denominatore < 0):
- denominatore *= -1
- self.__segno = -1
- self.__denominatore = denominatore
- self.__riduzione()
- def get_segno(self):
- return self.__segno;
- def set_segno(self, segno):
- self.__segno = -1 if (segno < 0) else 1
- segno = property(fget=get_segno, fset=set_segno,fdel=None, doc=None)
- numeratore = property(leggi_numeratore, imposta_numeratore)
- denominatore = property(leggi_denominatore, imposta_denominatore)
- def __mcd(self, a, b):
- if (b == 0):
- return a
- if (a < b):
- (a,b)=(b,a)
- return self.__mcd(b, a % b)
- def __riduzione(self):
- d = self.__mcd(self.__numeratore, self.__denominatore)
- self.__numeratore /= d
- self.__denominatore /= d
- def addizione(self, f):
- return Frazione(f.__segno * f.numeratore * self.denominatore + self.__segno * self.numeratore * f.denominatore, f.denominatore * self.denominatore)
- def sottrazione(self, f):
- return Frazione(-f.__segno * f.numeratore * self.denominatore + self.__segno * self.numeratore * f.denominatore, f.denominatore * self.denominatore)
- def moltiplicazione(self, f):
- return Frazione(f.__segno * f.numeratore * self.numeratore * self.__segno, f.denominatore * self.denominatore)
- def divisione(self, f):
- return self.moltiplicazione(Frazione(f.denominatore * f.__segno, f.numeratore))
- def inverti(self):
- return Frazione(-self.__numeratore, self.__denominatore)
- def as_float(self): return self.__segno * self.__numeratore / self.__denominatore
- def __eq__(self, value): return self.__segno == value.segno and self.__numeratore == value.numeratore and self.__denominatore == value.denominatore
- def __ne__(self, value): return not self.__eq__(value)
- def __lt__(self, value): return self.as_float() < value.as_float()
- def __le__(self, value): return self.__lt__(value) or self.__eq__(value)
- def __gt__(self, value): return self.as_float() > value.as_float()
- def __ge__(self, value): return self.__gt__(value) or self.__eq__(value)
- def __str__(self):
- if (self.__numeratore == 0): return "0"
- s = "-" if (self.__segno == -1) else ""
- s += str(int(self.__numeratore))
- if (self.__denominatore != 1):
- s += "/" + str(int(self.__denominatore))
- return s
- def __add__(self, f): return self.addizione(f)
- f1 = Frazione(4,2) # f1 è variabile di istanza della classe Frazione e contiene una propria copia
- # di segno, numeratore, denominatore
- #f1.numeratore = -20
- #f1.denonimatore = 0
- #print("segno di f1: " + str(f1.segno) + " numeratore di f1: " +
- #str(f1.__numeratore) + " denominatore: " + str(f1.__denominatore))
- f1.stampa_a_video()
- #stampa_a_video(f1)
- f2 = Frazione(denominatore= -7,numeratore= 4)
- print("numeratore di f2: " + str(f2.leggi_numeratore()))
- f2.segno = -1
- #print("segno di f2: " + str(f2.segno) + " numeratore di f2: " + str(f2.__numeratore))
- f2.stampa_a_video()
- f2.imposta_numeratore(-2)
- f2.stampa_a_video()
- f1.numeratore = 15
- f1.denominatore = -2
- f1.stampa_a_video()
- f3 = f1.addizione(f2)
- f1.stampa_a_video()
- f2.stampa_a_video()
- f3.stampa_a_video()
- f3 = f1.sottrazione(f2)
- f3.stampa_a_video()
- f3 = f1.moltiplicazione(f2)
- f3.stampa_a_video()
- f3 = f1.divisione(f2)
- f3.stampa_a_video()
- f3 = f3.inverti()
- f3.stampa_a_video()
- f1 = Frazione(3, 4)
- f2 = Frazione(5, 4)
- if (f1 < f2):
- print("f1 < f2")
- else:
- print("f1 >= f2")
- if (f1 == f2):
- print("uguali")
- else:
- print("diversi")
- print(f1)
- print(f2)
- f3 = f1
- if (f1 != f3):
- print("diversi")
- else:
- print("uguali")
- print(f1)
- print(f3)
- f3.numeratore = 10
- f1.stampa_a_video()
- print("La frazione f1 vale: " + str(f1))
- print ("1/3 + 3/4 = " + str(Frazione(1,3) + Frazione(3,4)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement