c sharp 定义完整的ComplexNumber类,在其中重载加减乘除等基本算术操作符,相等和不等操作符,以及大小比较操作符
时间: 2024-05-08 18:15:29 浏览: 88
以下是定义完整的ComplexNumber类,并重载基本算术操作符、相等和不等操作符、大小比较操作符的示例代码:
```csharp
using System;
public class ComplexNumber
{
private double real;
private double imaginary;
public ComplexNumber(double real, double imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
public double Real
{
get { return real; }
set { real = value; }
}
public double Imaginary
{
get { return imaginary; }
set { imaginary = value; }
}
public static ComplexNumber operator +(ComplexNumber c1, ComplexNumber c2)
{
return new ComplexNumber(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public static ComplexNumber operator -(ComplexNumber c1, ComplexNumber c2)
{
return new ComplexNumber(c1.real - c2.real, c1.imaginary - c2.imaginary);
}
public static ComplexNumber operator *(ComplexNumber c1, ComplexNumber c2)
{
double real = c1.real * c2.real - c1.imaginary * c2.imaginary;
double imag = c1.real * c2.imaginary + c1.imaginary * c2.real;
return new ComplexNumber(real, imag);
}
public static ComplexNumber operator /(ComplexNumber c1, ComplexNumber c2)
{
double divisor = c2.real * c2.real + c2.imaginary * c2.imaginary;
double real = (c1.real * c2.real + c1.imaginary * c2.imaginary) / divisor;
double imag = (c1.imaginary * c2.real - c1.real * c2.imaginary) / divisor;
return new ComplexNumber(real, imag);
}
public static bool operator ==(ComplexNumber c1, ComplexNumber c2)
{
if (ReferenceEquals(c1, c2))
{
return true;
}
if (ReferenceEquals(c1, null) || ReferenceEquals(c2, null))
{
return false;
}
return c1.real == c2.real && c1.imaginary == c2.imaginary;
}
public static bool operator !=(ComplexNumber c1, ComplexNumber c2)
{
return !(c1 == c2);
}
public static bool operator >(ComplexNumber c1, ComplexNumber c2)
{
return c1.Magnitude() > c2.Magnitude();
}
public static bool operator <(ComplexNumber c1, ComplexNumber c2)
{
return c1.Magnitude() < c2.Magnitude();
}
public static bool operator >=(ComplexNumber c1, ComplexNumber c2)
{
return c1.Magnitude() >= c2.Magnitude();
}
public static bool operator <=(ComplexNumber c1, ComplexNumber c2)
{
return c1.Magnitude() <= c2.Magnitude();
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
ComplexNumber other = (ComplexNumber)obj;
return real == other.real && imaginary == other.imaginary;
}
public override int GetHashCode()
{
return Tuple.Create(real, imaginary).GetHashCode();
}
public double Magnitude()
{
return Math.Sqrt(real * real + imaginary * imaginary);
}
public override string ToString()
{
return string.Format("({0}, {1})", real, imaginary);
}
}
```
在上面的代码中,我们定义了一个ComplexNumber类,包含了实部和虚部两个字段,以及相应的属性和构造函数。我们重载了加减乘除四个基本算术操作符,使得两个复数对象可以直接进行加减乘除运算。我们还重载了相等和不等操作符,以及大小比较操作符。其中,大小比较操作符的实现依赖于复数的模长(magnitude),即实部和虚部的平方和的平方根。
在重载操作符时,我们也需要注意一些细节。例如,在重载相等和不等操作符时,我们需要先判断两个对象是否为null,然后再进行实际的相等性判断。此外,我们还重载了Equals和GetHashCode方法,以便在使用复数对象作为字典键或集合元素时能够正确地进行比较和哈希。最后,我们还实现了Magnitute和ToString方法,分别用于计算复数的模长和生成表示复数对象的字符串。
阅读全文