重载运算符,让你的复数类使用起来更加方便
最近在做傅里叶变化和巴特沃斯滤波,又要使用到复数。
之前我也有发表过类似复数类的文章,不过当时的写法用起来没有那么方便。不能跟int、float...等数据类型一样使用起来那么方便。
重载部分运算符后使用起来也更加方便、快捷,当然有些运算符是不允许重载的,有些是不能显示重载的。具体那些不能重载,那些不能显示重载,看下表边一清二楚。
运算符 | 可重载性 |
+、-、!、~、++、--、true、false | 可以重载这些一元运算符, true和false运算符必须成对重载 |
+、-、*、/、%、&、|、^、<<、>> | 可以重载这些二元运算符 |
==、!=、<、>、<=、>= | 可以重载比较运算符,必须成对重载 |
&&、|| | 不能重载条件逻辑运算符,但可以使用能够重载的&和|进行计算 |
[] | 不能重载数组索引运算符,但可以定义索引器 |
() | 不能重载转换运算符,但可以定义新的转换运算符(请参见 explicit 和 implicit) |
+=、-=、*=、/=、%=、&=、|=、^=、<<=、>>= | 不能显式重载赋值运算符,在重写单个运算符如+、-、%时,它们会被 隐式重写 |
=、.、?:、->、new、is、sizeof、typeof | 不能重载这些运算符 |
运算符重载的具体格式 public static result-type operator unary-operator (object parameter1,object parameter2,...);注意一定是要声明成static,否则无法编译通过。
我主要使用的是C#语言来实现运算符重载。具体代码如下;
/// <summary>
/// Complex 的摘要说明
/// 复数类
/// 创建人:风幻影
/// 创建时间:2018-8-20 19:19:22
/// </summary>
public class Complex
{
#region 变量、属性
/// <summary>
/// 实部
/// </summary>
float real;
/// <summary>
/// 实部
/// </summary>
public float Real { get { return real; } set { real = value; } }
/// <summary>
/// 虚部
/// </summary>
float img;
/// <summary>
/// 虚部
/// </summary>
public float Img { get { return img; } set { img = value; } }
#endregion
#region 构造函数
public Complex(float real, float img)
{
this.real = real;
this.img = img;
}
public Complex()
{
this.real = 0.0f;
this.img = 0.0f;
}
#endregion
#region 运算符重载
public static Complex operator +(Complex com0)
{
return new Complex(com0.Real, com0.Img);
}
public static Complex operator +(Complex com0, Complex com1)
{
return new Complex(com0.Real + com1.Real, com0.Img + com1.Img);
}
public static Complex operator +(Complex com0, float d)
{
return new Complex(com0.Real + d, com0.Img);
}
public static Complex operator +(float d, Complex com0)
{
return new Complex(com0.Real + d, com0.Img);
}
public static Complex operator -(Complex com0, Complex com1)
{
return new Complex(com0.Real - com1.Real, com0.Img - com1.Img);
}
public static Complex operator -(Complex com0, float d)
{
return new Complex(com0.Real - d, com0.Img);
}
public static Complex operator -(float d, Complex com0)
{
return new Complex(d - com0.Real, -com0.Img);
}
public static Complex operator *(Complex com0, Complex com1)
{
return new Complex(com0.Real * com1.Real - com1.Img * com0.Img, com0.Real * com1.Img + com0.Img * com1.Real);
}
public static Complex operator *(Complex com0, float d)
{
return new Complex(com0.Real * d, com0.Img * d);
}
public static Complex operator *(float d, Complex com0)
{
return new Complex(com0.Real * d, com0.Img * d);
}
public static Complex operator /(Complex com0, Complex com1)
{
float denominator = 1 / (com1.Real * com1.Real + com1.Img * com1.Img);
return new Complex((com0.Real * com1.Real + com0.Img * com1.Img) * denominator, (-com0.Real * com1.Img + com0.Img * com1.Real) * denominator);
}
public static Complex operator /(Complex com0, float d)
{
return new Complex(com0.Real / d, com0.Img / d);
}
public static Complex operator /(float d, Complex com0)
{
float denominator = 1 / (com0.Real * com0.Real + com0.Img * com0.Img);
return new Complex(d * com0.Real * denominator, -d * com0.Img * denominator);
}
public static bool operator ==(Complex com0, Complex com1)
{
return (com0.Real == com1.Real) && (com0.Img == com1.Img);
}
public static bool operator !=(Complex com0, Complex com1)
{
return (com0.Real != com1.Real) || (com0.Img != com1.Img);
}
public override bool Equals(object obj)
{
return (this.Real == ((Complex)obj).Real) && (this.Img == ((Complex)obj).Img);
}
#endregion
#region 其他方法
/// <summary>
/// 取模运算
/// </summary>
/// <returns></returns>
public double Mod()
{
return Math.Sqrt(this.Real * this.Real * this.Img * this.Img);
}
/// <summary>
/// 重载ToString方法
/// </summary>
/// <returns></returns>
public override string ToString()
{
return base.ToString();
}
/// <summary>
/// 深度克隆
/// </summary>
/// <returns></returns>
public Complex Clone()
{
return new Complex(this.Real,this.Img) ;
}
#endregion
}
由于最近也比较忙。写得也比较匆忙,如有错的地方,欢迎大家批评指正。