from Fraccion import Fraccion class Complejos: """Definicion de la clase Complejos""" def __init__(self, rp=0, rq=1, ip=0, iq=1): """Constructor nulo rp/rq + ip/iqi""" self.R = Fraccion(rp, rq) self.I = Fraccion(ip, iq) def __str__(self): return str(self.R) + " + " + str(self.I) + "i" def visualiza(self): """Visualiza la Fraccion""" print("{} + {}i".format(self.R, self.I)) def __add__(self, a): """Suma""" r = self.R + a.R i = self.I + a.I return Complejos(r.P, r.Q, i.P, i.Q) def __sub__(self, a): """Resta""" r = self.R - a.R i = self.I - a.I return Complejos(r.P, r.Q, i.P, i.Q) def __mul__(self, a): """Multiplicaciòn""" r = self.R * a.R - self.I * a.I i = self.R * a.I + self.I * a.R return Complejos(r.P, r.Q, i.P, i.Q) def __truediv__(self, a): """Divisiòn""" t = (a.R * a.R) + (a.I * a.I) r = ((self.R * a.R) + (self.I * a.I)) / t i = ((self.I * a.R) - (self.R * a.I)) / t return Complejos(r.P, r.Q, i.P, i.Q) def __ne__(self, a): """No igual""" if self.R == a.R and self.I == a.I: return False return True def __eq__(self, a): """igual""" if self.R == a.R and self.I == a.I: return True return False """ Prueba de las clases """ if __name__ == "__main__": a = Complejos(1.0, 3.0) b = Complejos(2.0, 3.0) c = Complejos() a.visualiza() b.visualiza() c.visualiza() print("Suma de dos operandos") c = a + b r = str(a) + " + " + str(b) + " = " + str(c) print(r) print("Resta de dos operandos") c = a - b r = str(a) + " - " + str(b) + " = " + str(c) print(r) print("Multiplicacion de dos operandos") c = a * b r = str(a) + " + " + str(b) + " = " + str(c) print(r) print("Division de dos operandos") c = a / b r = str(a) + " - " + str(b) + " = " + str(c) print(r)