# SOS请各位前辈帮帮帮小弟呀！！！（才学C++）

libobby 2006-04-22 07:08:05

...全文
90 4 点赞 打赏 收藏 举报

4 条回复

libobby 2006-04-22

• 打赏
• 举报

zCheng 2006-04-22

public struct Complex
{
/// <summary>
/// 表示零复数。
/// </summary>
public static readonly Complex Zero = new Complex(0.0, 0.0);

private double real;
private double imaginary;

/// <summary>
/// 获取或设置复数的实部。
/// </summary>
/// <value>表示复数的实部。</value>
public double Real
{
get { return real; }
set { real = value; }
}
/// <summary>
/// 获取或设置复数的虚部。
/// </summary>
/// <value>表示复数的虚部。</value>
public double Imaginary
{
get { return imaginary; }
set { imaginary = value; }
}

/// <summary>
/// 获取复数的模。
/// </summary>
/// <value>表示复数的模。</value>
public double Length
{
get { return Math.Sqrt(real * real + imaginary * imaginary); }
}

/// <summary>
/// 获取复数的幅角。
/// </summary>
/// <value>表示复数的幅角。</value>
public double Arc
{
get { return Math.Atan2(imaginary, real); }
}

/// <summary>
/// 判断该复数是否为实数。
/// </summary>
/// <value>表示该复数是否为实数。</value>
public bool IsReal
{
get { return imaginary == 0; }
}
/// <summary>
/// 判断该复数是否为纯虚数。
/// </summary>
/// <value>表示该复数是否为纯虚数。</value>
public bool IsPureImaginary
{
get { return real == 0; }
}

/// <summary>
/// 获取该复数的共轭复数。
/// </summary>
public Complex ConjugateComplex
{
get { return new Complex(real, -imaginary); }
}
/// <summary>
/// 初始化一个复数的新实例。
/// </summary>
/// <param name="real">复数的实部。</param>
/// <param name="imaginary">复数的虚部。</param>
public Complex(double real, double imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
/// <summary>
/// 初始化一个复数的新实例。
/// </summary>
/// <param name="real">复数的实部。</param>
/// <param name="imaginary">复数的虚部。</param>
public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}

/// <summary>
/// 求该复数本身。
/// </summary>
/// <param name="c">要求算的复数。</param>
/// <returns>该复数本身。</returns>
public static Complex operator +(Complex c)
{
return c;
}
/// <summary>
/// 求该复数的相反数。
/// </summary>
/// <param name="c">要求算的复数。</param>
/// <returns>给定复数的相反数。</returns>
public static Complex operator -(Complex c)
{
return new Complex(-c.real, -c.imaginary);
}

/// <summary>
/// 复数的加法运算。
/// </summary>
/// <param name="c1">第一个复数。</param>
/// <param name="c2">第二个复数。</param>
/// <returns>两个复数的和。</returns>
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
/// <summary>
/// 复数的减法运算。
/// </summary>
/// <param name="c1">第一个复数。</param>
/// <param name="c2">第二个复数。</param>
/// <returns>两个复数的差。</returns>
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary);
}
/// <summary>
/// 复数的乘法运算。
/// </summary>
/// <param name="c1">第一个复数。</param>
/// <param name="c2">第二个复数。</param>
/// <returns>两个复数的积。</returns>
public static Complex operator *(Complex c1, Complex c2)
{
return new Complex(c1.real * c2.real - c1.imaginary * c2.imaginary, c1.real * c2.imaginary + c1.imaginary * c2.real);
}
/// <summary>
/// 复数的除法运算。
/// </summary>
/// <param name="c1">第一个复数。</param>
/// <param name="c2">第二个复数。</param>
/// <returns>两个复数的商。</returns>
public static Complex operator /(Complex c1, Complex c2)
{
double m = c2.real * c2.real + c2.imaginary * c2.imaginary;
return new Complex((c1.real * c2.real + c1.imaginary * c2.imaginary) / m, (c1.real * c2.imaginary - c1.imaginary * c2.real) / m);
}
}
• 打赏
• 举报

boci 2006-04-22

• 打赏
• 举报

libobby 2006-04-22

• 打赏
• 举报

C#

10.6w+

.NET技术 C#

2006-04-22 07:08