復數是C#中沒有的,不能直接調用的。但是我們可以通過封裝,構造自己的復數形式。這里我自己封裝了一個Complex類,也不知道寫得如何。可能還有一些東西沒有考慮。
不過這里包含了復數的基本暈算了了,包括加減乘除、取模運算、計算相位角等!詳細信息其直接閱讀代碼。都包含注釋了。
/// <summary>
/// 復數類
/// </summary>
public class Complex
{
private double real;//實部
private double image;//虛部
/// <summary>
/// 獲取或設置實部
/// </summary>
public double Real
{
get { return real; }
set { real = value; }
}
/// <summary>
/// 獲取或者設置虛部
/// </summary>
public double Image
{
get { return image; }
set { image = value; }
}
public Complex(double real, double image)
{
this.real = real;
this.image = image;
}
public Complex() { }
/// <summary>
/// 取共軛
/// </summary>
public Complex Conjugate()
{
//Complex complex = new Complex();
//complex.real = this.real;
//complex.image = -complex.image;
//return complex;
return new Complex(this.real, -this.image);
}
/// <summary>
/// 加法重載函數
/// </summary>
/// <param name="C">加數</param>
/// <param name="c">加數</param>
/// <returns>復數相加的結果</returns>
public static Complex operator +(Complex C, Complex c)
{
//Complex com = new Complex();
//com.real = C.real + c.real;
//com.image = C.image + c.image;
//return com;
return new Complex(c.real + C.real, C.image + c.image);
}
/// <summary>
/// 復數的加法,可以同時實現多個復數相加
/// 其實跟直接用+號來相加的結果是一樣的,
/// 個人只是想多學習可變參數的用法
/// </summary>
/// <param name="complexs"></param>
/// <returns></returns>
public Complex Add(params Complex[] complexs)
{
if (complexs.Length == 0)
{
throw new Exception("輸入的參數不能為空!");
}
Complex com = new Complex();
foreach (Complex c in complexs)
{
com = com + c;
}
return com;
}
/// <summary>
/// 復數的減法重載函數
/// </summary>
/// <param name="C">被減數</param>
/// <param name="c">減數</param>
/// <returns>復數相減后的結果</returns>
public static Complex operator -(Complex C, Complex c)
{
//Complex com = new Complex();
//com.real = C.real -c.real;
//com.image = C.image - c.image;
//return com;
return new Complex(C.real - c.real, C.image - c.Image);
}
/// <summary>
/// 雙等號函數的重載
/// </summary>
/// <param name="C"></param>
/// <param name="c"></param>
/// <returns>如果相等返回true,否則返回fasle</returns>
public static bool operator ==(Complex C, Complex c)
{
return (C.real == c.real && C.image == c.image);
}
/// <summary>
/// 不等號函數的重載
/// </summary>
/// <param name="C"></param>
/// <param name="c"></param>
/// <returns></returns>
public static bool operator !=(Complex C, Complex c)
{
return (C.real != c.real || C.image != c.image);
}
/// <summary>
/// 復數的相減,可以同時實現多個復數相減
/// 其實跟直接用-號來相加的結果是一樣的,
/// 個人只是想多學習可變參數的用法
/// </summary>
/// <param name="complexs">數的集合</param>
/// <returns>相減操作后的復數</returns>
public Complex Minus(params Complex[] complexs)
{
if (complexs.Length == 0)
{
throw new Exception("輸入的參數不能為空!");
}
Complex com =complexs[0];
for (int i = 1; i < complexs.Length; i++)
{
com = com - complexs[i];
}
return com;
}
/// <summary>
/// 復數的乘法運算
/// </summary>
/// <param name="c"></param>
/// <param name="C"></param>
/// <returns></returns>
public static Complex operator *(Complex c, Complex C)
{
//(a+b*i)*(c+d*i)=(ac-bd)+(ad+bc)*i
return new Complex(c.real * C.real-c.image*C.image, c.real*C.image+c.image * C.real);
}
public Complex Multiplicative(params Complex[] complexs)
{
if (complexs.Length == 0)
{
throw new Exception("輸入的參數不能為空!");
}
Complex com = complexs[0];
for (int i = 1; i < complexs.Length; i++)
{
com += complexs[i];
}
return null;
}
/// <summary>
/// 復數除法
/// </summary>
/// <param name="C"></param>
/// <param name="c"></param>
/// <returns></returns>
public static Complex operator /(Complex C, Complex c)
{
if (c.real == 0 && c.image == 0)
{
throw new Exception("除數的虛部和實部不能同時為零(除數不能為零)");
}
double real = (C.real * c.real + c.image * C.image)/(c.real*c.real+c.image+c.image);
double image=(C.image*c.real-c.image*C.real)/(c.real*c.real+c.image+c.image);
return new Complex(real,image);
}
/// <summary>
/// 復數除法運算
/// </summary>
/// <param name="complexs">一系列復數</param>
/// <returns>除法運算后的結果</returns>
public Complex Divison(params Complex[] complexs)
{
if (complexs.Length == 0)
{
throw new Exception("輸入的參數不能為空!");
}
foreach (Complex com in complexs)
{
if (com.image==0&&com.real==0)
{
throw new Exception("除數的實部和虛部不能同時為零!");
}
}
Complex COM = new Complex();
COM = complexs[0];
for (int i = 1; i < complexs.Length; i++)
{
COM = COM / complexs[i];
}
return COM;
}
/// <summary>
/// 取模運算
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public double Mod(Complex c)
{
return Math.Sqrt(c.real * c.real + c.image * c.image);
}
/// <summary>
/// 判斷復數是否相等
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj is Complex)
{
Complex com = (Complex)obj;
return (com.real == this.real && com.image == this.image);
}
return false;
}
/// <summary>
/// 計算復數相位角
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public static double GetAngle(Complex c)
{
return Math.Atan2(c.real, c.image);
}
public override string ToString()
{
//string str = null;
//if (this.image == 0)
//{
// str = "=";
//}
//else if (this.image > 0)
//{
// str = ">";
//}
//switch (str)
//{
// case ">":
// if (this.real == 0)
// {
// return string.Format("{0}i", this.image);
// }
// return string.Format("{0}+{1}i", this.real, this.image);
// case "=":
// return string.Format("{0}",this.real);
// default:
// if (this.real == 0)
// {
// return string.Format("{0}i", this.image);
// }
// return string.Format("{0}+{1}i", this.real, this.image);
//}
return string.Format("<{0} , {1}>", this.real, this.image);
}
}
第一次發博文,也知道自己的水平菜菜的,慢慢進步。。