python运算符重载、并且编写复数类的加减乘除_编程基础篇:定义一个复数类Complex,重载运算符“+,-,*,/”,使之能用于复数的加减乘除.....出现的问题及代码...

定义一个复数类Complex,重载运算符“+”、“-”、“*”、“/”,使之能用于复数的加、减、乘、除。要求:将“+”和“-”的运算符重载函数作为Complex的成员函数;将“*”和“/” 的运算符重载函数作为友元函数。编写程序,分别求两个复数之和、差、积、商。

提示:若已知c1=a+bi,c2=c+di,则:

c1+c2=(a+c)+(b+d)i

c1-c2=(a-c)+(b-d)i

c1*c2=(ac-bd)+(ad+bc)i

c1/c2=(ac+bd)/(c*c+d*d)+((bc-ad)/( c*c+d*d))i

运行结果参考如下:

07130827f3bec5a952883568578ef472.png

下面是代码

#include using namespace std;

class Complex {

public :

Complex (double x=0,double y=0):real(x),imag(y) {}

friend Complex operator * (Complex &c1,Complex &c2);

friend Complex operator / (Complex &c1,Complex &c2);

Complex operator + (Complex &c2 );

Complex operator - (Complex &c2 );

void display();

private :

double real;

double imag;

};

Complex operator * (Complex &c1,Complex &c2) {

return Complex (c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);

}

Complex operator / (Complex &c1,Complex &c2) {

Complex c;

c.real=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);

c.imag=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);

return c;

}

Complex Complex ::operator + (Complex &c2) {

return Complex (real+c2.real,imag+c2.imag);

}

Complex Complex ::operator - (Complex &c2) {

return Complex (real-c2.real,imag-c2.imag);

}

void Complex ::display() {

cout

出现过的问题,

1.

Complex (double x=0,double y=0):real(x),imag(y){}

参数初始化表这里需要对x,y赋初值,不然c3在编译时会报错;

2.

must take either zero or one argument

出现这个问题也困扰了我很久

二元运算符用成员重载时,   只需要一个参数,   另一个参数由this指针传入 (第一个参数由this指针自动传入到函数中去的.)

Complex operator + (Complex &c1,Complex &c2 );

所以这样写是不对的

下面是定义一个复数类 `Complex_` 和 `Complex`,并重载运算符 `+`、`-`、`*` 和 `/` 的示例代码: ```python class Complex_: def __init__(self, real=0, imag=0): self.real = real self.imag = imag def __add__(self, other): return Complex_(self.real + other.real, self.imag + other.imag) def __sub__(self, other): return Complex_(self.real - other.real, self.imag - other.imag) def __mul__(self, other): return Complex_(self.real * other.real - self.imag * other.imag, self.real * other.imag + self.imag * other.real) def __truediv__(self, other): denom = other.real ** 2 + other.imag ** 2 return Complex_((self.real * other.real + self.imag * other.imag) / denom, (self.imag * other.real - self.real * other.imag) / denom) def __str__(self): return f"{self.real} + {self.imag}j" class Complex: def __init__(self, real=0, imag=0): self.real = real self.imag = imag def __add__(self, other): return Complex(self.real + other.real, self.imag + other.imag) def __sub__(self, other): return Complex(self.real - other.real, self.imag - other.imag) def __mul__(self, other): return Complex(self.real * other.real - self.imag * other.imag, self.real * other.imag + self.imag * other.real) def __truediv__(self, other): denom = other.real ** 2 + other.imag ** 2 return Complex((self.real * other.real + self.imag * other.imag) / denom, (self.imag * other.real - self.real * other.imag) / denom) def __str__(self): return f"{self.real} + {self.imag}j" ``` 这里定义了两个复数类 `Complex_` 和 `Complex`,它们的实现方式有些不同。其,`Complex_` 类使用 `_` 后缀来避免与 Python 内置的 `complex` 类重名。两个类都有 `real` 和 `imag` 属性,分别表示实部和虚部。`__add__`、`__sub__`、`__mul__` 和 `__truediv__` 方法分别重载、乘和除运算符,使得我们可以直接对两个复数进行运算。`__str__` 方法用于复数转换为字符串形式,便于输出。 示例代码的 `__truediv__` 方法是重载除法运算符 `/` 的方法,因此需要使用 `__truediv__` 方法而不是 `__div__` 方法(`__div__` 方法在 Python 3 已被弃用)。 使用示例: ```python a = Complex_(1, 2) b = Complex_(3, 4) print(a + b) # 输出:4 + 6j print(a - b) # 输出:-2 - 2j print(a * b) # 输出:-5 + 10j print(a / b) # 输出:0.44 + 0.08j c = Complex(1, 2) d = Complex(3, 4) print(c + d) # 输出:4 + 6j print(c - d) # 输出:-2 - 2j print(c * d) # 输出:-5 + 10j print(c / d) # 输出:0.44 + 0.08j ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值