## C#复数类（实现各类运算符的重载） 原

北风其凉

1.类的实现

``````/// <summary>
/// 复数类
/// </summary>
class ComplexNumber
{
//实部
double _RealPart;
public double RealPart
{
get { return _RealPart; }
set { _RealPart = value; }
}

//虚部
double _ImaginaryPart;
public double ImaginaryPart
{
get { return _ImaginaryPart; }
set { _ImaginaryPart = value; }
}

/// <summary>
/// 创建一个复数类实例
/// </summary>
/// <param name="rp">实部</param>
/// <param name="ip">虚部</param>
public ComplexNumber(double rp, double ip)
{
RealPart = rp;
ImaginaryPart = ip;
}

/// <summary>
/// 返回一个复数的共轭复数
/// </summary>
/// <param name="cn"></param>
/// <returns></returns>
public static ComplexNumber ConjugateOf(ComplexNumber cn)
{
return new ComplexNumber(cn.RealPart, -cn.ImaginaryPart);
}

/// <summary>
/// 以字符串形式输出复数
/// </summary>
/// <returns></returns>
public override string ToString()
{
//输出：实部 +/- 虚部 i 虚部前的加减号取决于虚部的正负
return string.Format(" {0} {1} {2} i ",
RealPart.ToString("#0.00"),
ImaginaryPart >= 0 ? "+" : "-",
Math.Abs(ImaginaryPart).ToString("#0.00"));
}

#region 重载一元运算符

//取正、取负
public static ComplexNumber operator +(ComplexNumber cn)
{ return cn; }
public static ComplexNumber operator -(ComplexNumber cn)
{ return new ComplexNumber(-cn.RealPart, -cn.ImaginaryPart); }

#endregion
#region 重载二元运算符

//加法 (a+bi)+(c+di)=(a+c)+(b+d)i
public static ComplexNumber operator +(double num, ComplexNumber cn)
{ return new ComplexNumber(cn.RealPart + num, cn.ImaginaryPart); }
public static ComplexNumber operator +(ComplexNumber cn, double num)
{ return new ComplexNumber(cn.RealPart + num, cn.ImaginaryPart); }
public static ComplexNumber operator +(ComplexNumber cn1, ComplexNumber cn2)
{
return new ComplexNumber(
cn1.RealPart + cn2.RealPart,
cn1.ImaginaryPart + cn2.ImaginaryPart);
}

//减法 (a-bi)+(c-di)=(a-c)+(b-d)i
public static ComplexNumber operator -(double num, ComplexNumber cn)
{ return new ComplexNumber(cn.RealPart - num, cn.ImaginaryPart); }
public static ComplexNumber operator -(ComplexNumber cn, double num)
{ return new ComplexNumber(cn.RealPart - num, cn.ImaginaryPart); }
public static ComplexNumber operator -(ComplexNumber cn1, ComplexNumber cn2)
{
return new ComplexNumber(
cn1.RealPart - cn2.RealPart,
cn1.ImaginaryPart - cn2.ImaginaryPart);
}

public static ComplexNumber operator *(double num, ComplexNumber cn)
{ return new ComplexNumber(cn.RealPart * num, cn.ImaginaryPart * num); }
public static ComplexNumber operator *(ComplexNumber cn, double num)
{ return new ComplexNumber(cn.RealPart * num, cn.ImaginaryPart * num); }
public static ComplexNumber operator *(ComplexNumber cn1, ComplexNumber cn2)
{
return new ComplexNumber(
cn1.RealPart * cn2.RealPart - cn1.ImaginaryPart * cn2.ImaginaryPart,
cn1.RealPart * cn2.ImaginaryPart + cn1.ImaginaryPart * cn2.RealPart);
}

//除法：
//实数除以复数：n/(c+di)=n*(c-di)/(c*c+d*d)
public static ComplexNumber operator /(double num, ComplexNumber cn)
{
return num * new ComplexNumber(cn.RealPart, -cn.ImaginaryPart) /
(cn.RealPart * cn.RealPart + cn.ImaginaryPart * cn.ImaginaryPart);
}
//复数除以实数：(a+bi)/n=a/n+b/n*i
public static ComplexNumber operator /(ComplexNumber cn, double num)
{
return new ComplexNumber(cn.RealPart / num, cn.ImaginaryPart / num);
}
public static ComplexNumber operator /(ComplexNumber cn1, ComplexNumber cn2)
{
double a = cn1.RealPart, b = cn1.ImaginaryPart;
double c = cn2.RealPart, d = cn2.ImaginaryPart;
return new ComplexNumber(a * c + b * d, a * d + b * c) / (c * c + d * d);
}

#endregion
#region 重载关系运算符

//等于
public static bool operator ==(ComplexNumber cn1, ComplexNumber cn2)
{
return cn1.RealPart == cn2.RealPart &&
cn1.ImaginaryPart == cn2.ImaginaryPart
? true : false;
}
public static bool operator ==(ComplexNumber cn, double n)
{ return cn.RealPart == n && cn.ImaginaryPart == 0 ? true : false; }
public static bool operator ==(double n, ComplexNumber cn)
{ return cn.RealPart == n && cn.ImaginaryPart == 0 ? true : false; }

//不等于
public static bool operator !=(ComplexNumber cn1, ComplexNumber cn2)
{
return cn1.RealPart != cn2.RealPart ||
cn1.ImaginaryPart != cn2.ImaginaryPart
? true : false;
}
public static bool operator !=(ComplexNumber cn, double n)
{ return cn.RealPart != n || cn.ImaginaryPart != 0 ? true : false; }
public static bool operator !=(double n, ComplexNumber cn)
{ return cn.RealPart != n || cn.ImaginaryPart != 0 ? true : false; }

//重载==和!=运算符后应该重写Equal是和GetHashCode函数
//重写Equals函数
public override bool Equals(object obj)
{
if (!(obj is ComplexNumber)) return false;
else
{
ComplexNumber cn = (ComplexNumber)obj;
if (this.RealPart != cn.RealPart) return false;
if (this.ImaginaryPart != cn.ImaginaryPart) return false;
return true;
}
}
//重写GetHashCode函数
public override int GetHashCode()
{ return base.GetHashCode(); }

#endregion
#region 重载true和false

//实部和虚部至少有一个不为0时，为true，否则为false
public static bool operator true(ComplexNumber cn)
{ return cn.RealPart != 0 || cn.ImaginaryPart != 0 ? true : false; }
public static bool operator false(ComplexNumber cn)
{ return cn.RealPart == 0 && cn.ImaginaryPart == 0 ? true : false; }

#endregion
#region 转换运算符

//只能显示转换，强制转换时舍弃虚部
public static explicit operator int(ComplexNumber cn) { return (int)cn.RealPart; }
public static explicit operator long(ComplexNumber cn) { return (long)cn.RealPart; }
public static explicit operator double(ComplexNumber cn) { return cn.RealPart; }

#endregion
}``````

2.Main函数调用

``````static void Main(string[] args)
{
ComplexNumber cn = new ComplexNumber(1, 1);

//两个复数
ComplexNumber cn1 = new ComplexNumber(1, 1);
ComplexNumber cn2 = new ComplexNumber(1, -1);

//打印复数
Console.WriteLine("cn1: " + cn1);
Console.WriteLine("cn2: " + cn2);

//求复数的共轭复数
Console.WriteLine("Conjugate of cn1: " + ComplexNumber.ConjugateOf(cn1));
Console.WriteLine("Conjugate of cn2: " + ComplexNumber.ConjugateOf(cn2));

//复数取正负
Console.WriteLine("+cn1: " + (+cn1));
Console.WriteLine("-cn1: " + (-cn1));

//复数加法
Console.WriteLine("cn1+cn2: " + (cn1 + cn2));
Console.WriteLine("cn1+1.2: " + (cn1 + 1.2));
Console.WriteLine("1.2+cn1: " + (1.2 + cn1));

//复数减法
Console.WriteLine("cn1-cn2: " + (cn1 - cn2));
Console.WriteLine("cn1-1.2: " + (cn1 - 1.2));
Console.WriteLine("1.2-cn1: " + (1.2 - cn1));

//复数乘法
Console.WriteLine("cn1*cn2: " + (cn1 * cn2));
Console.WriteLine("cn1*1.2: " + (cn1 * 1.2));
Console.WriteLine("1.2*cn1: " + (1.2 * cn1));

//复数除法
Console.WriteLine("cn1/cn2: " + (cn1 / cn2));
Console.WriteLine("cn1/1.2: " + (cn1 / 1.2));
Console.WriteLine("1.2/cn1: " + (1.2 / cn1));

//检验是否相等
if (cn1 == cn2) { Console.WriteLine("cn1==cn2"); }
if (cn1 != cn2) { Console.WriteLine("cn1!=cn2"); }
if (cn1 + cn2 == 2) { Console.WriteLine("cn1+cn2==2"); }
if (cn1 + cn2 != 2) { Console.WriteLine("cn1+cn2!=2"); }

//显式类型转换
Console.WriteLine("(int)(cn1+cn2): " + (int)(cn1 + cn2));
Console.WriteLine("(long)(cn1+cn2): " + (long)(cn1 + cn2));
Console.WriteLine("(double)(cn1+cn2): " + (double)(cn1 + cn2));

}``````

3.运行示例

### 北风其凉

C#运算符重载

2012/03/09
776
0
C++类或结构作为map的key值

1.只有重载<的类或者结构才能作为map的key值。 string可以作为key值是因为string重载了< 2.如果不重载<会提示如下错误： error C2676: 二进制“<”: “const C”不定义该运算符或到预定义运算...

2015/08/20
18
0

3.1 基于类的软件复用技术 　 C++中的类库技术是软件复用的基础，在软件开发过程中，程序员通过调用类库中的函数可以达到软件复用的目的。比如，调用C++类库中的输入输出流函数可以实现输入输...

2014/04/24
0
0
.NET的数学库NMath实用教程——复数的值操作和逻辑运算

NMath是一个适用于所有.NET语言，如C＃、Visual Basic、F＃和.NET的数学库，它包含了.NET平台上的面向对象数字计算的基础类。我们将以连载的形式向大家介绍NMath的实用教程，有任何建议或提示...

ymy_666666
01/29
10
0

2012/12/18
580
0

1、类和对象 类是对象的蓝图和模板，而对象是实例；即对象是具体的实例，类是一个抽象的模板 当我们把一大堆拥有共同特征的对象的静态特征（属性）和动态特征（行为）都抽取出来后，就可以定...

huijue

8
0
redis异常解决 ：idea启动本地redis出现 jedis.exceptions.JedisDataException: NOAUTH Authentication required

10
0
Spring 之 IoC 源码分析 (基于注解方式)

25
0
Docker安装PostgresSql

Docker安装PostgresSql 拉取docker镜像 # docker pull postgres:10.1010.10: Pulling from library/postgres9fc222b64b0a: Pull complete 38296355136d: Pull complete 2809e135bbdb: Pu......

Tree

8
0

20
0