Fork me on GitHub

重载运算符,让你的复数类使用起来更加方便

最近在做傅里叶变化和巴特沃斯滤波,又要使用到复数。

之前我也有发表过类似复数类的文章,不过当时的写法用起来没有那么方便。不能跟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
}

由于最近也比较忙。写得也比较匆忙,如有错的地方,欢迎大家批评指正。

posted @   黄高林  阅读(591)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示