Question: Design a class in Python that represents complex numbers and supports important operations such as addition, subtraction, multiplication and division. For the Python you will

Design a class in Python that represents complex numbers and supports important operations such as addition, subtraction, multiplication and division. For the Python you will need to implement the following functions for each operation: op: Complex Complex ? Complex op: Complex double ? Complex op: double Complex ? Complex Where op is one of +, -, *, or /. In addition, you will need to overload the stream insertion operator << to print objects of this type. A constructor must be defined as well as overloading the assignment operator to allow for implicit conversion from doubles to Complex. Any other methods you deem appropriate should also be included. The more complete your class the better. The Python version you should also include functions for converting from complexes to strings.

here is the sample code

# # # Python version # # class rational: def __init__(self, num=0, den=1): self.num = num self.den = den def __add__(self, other): if isinstance(other, int): return rational(self.num + other * self.den, self.den) elif isinstance(other, rational): return rational(self.num * other.den + other.num * self.den, self.den * other.den) else: raise TypeError def __truediv__(self, other): if isinstance(other, int): return rational(self.num, self.den * other) elif isinstance(other, rational): return rational(self.num * other.den, self.den * other.num) else: raise TypeError def __float__(self): return float(self.num) / self.den def __int__(self): return self.num / self.den def __mul__(self, other): if isinstance(other, int): return rational(self.num * other, self.den) elif isinstance(other, rational): return rational(self.num * other.num, self.den * other.den) else: raise TypeError def __radd__(self, other): return self + other def __rtruediv__(self, other): return rational(other) / self def __rmul__(self, other): return self * other def __rsub__(self, other): return rational(other) - self def __str__(self): return '(' + str(self.num) + ' / ' + str(self.den) + ')' def __sub__(self, other): if isinstance(other, int): return rational(self.num - other * self.den, self.den) elif isinstance(other, rational): return rational(self.num * other.den - other.num * self.den, self.den * other.den) else: raise TypeError if __name__ == '__main__': a = rational(1, 2) b = rational(1, 3) i = 5 print('%s + %s = %s' % (a, b, a + b)) print('%s - %s = %s' % (a, b, a - b)) print('%s * %s = %s' % (a, b, a * b)) print('%s / %s = %s' % (a, b, a / b)) print('%s + %i = %s' % (a, i, a + i)) print('%s - %i = %s' % (a, i, a - i)) print('%s * %i = %s' % (a, i, a * i)) print('%s / %i = %s' % (a, i, a / i)) print('%i + %s = %s' % (i, a, i + a)) print('%i - %s = %s' % (i, a, i - a)) print('%i * %s = %s' % (i, a, i * a)) print('%i / %s = %s' % (i, a, i / a))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!