注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

信息 灵感 创新

I? =Information,Inspiration,Innovation

 
 
 

日志

 
 
关于我

we are 5. Mathematics, Computation, Programming, Engineering, and Making fun of life.

网易考拉推荐

C# 复数(Complex)类  

2012-06-20 22:19:08|  分类: C# & .NET |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
[StructLayout(LayoutKind.Sequential)]
public struct Complex : IFormattable
{
    private double _real;
    private double _imaginary;
    [DebuggerStepThrough]
    public Complex(double real, double imaginary)
    {
        this._real = real;
        this._imaginary = imaginary;
    }

    public double Real
    {
        get
        {
            return this._real;
        }
        set
        {
            this._real = value;
        }
    }
    public double Imaginary
    {
        get
        {
            return this._imaginary;
        }
        set
        {
            this._imaginary = value;
        }
    }
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public Complex Conjugate
    {
        [DebuggerStepThrough]
        get
        {
            return new Complex(this._real, -this._imaginary);
        }
    }
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public double Modulus
    {
        [DebuggerStepThrough]
        get
        {
            return Math.Sqrt((this._real * this._real) + (this._imaginary * this._imaginary));
        }
    }
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public double Argument
    {
        [DebuggerStepThrough]
        get
        {
            return Math.Atan2(this._imaginary, this._real);
        }
        set
        {
            double modulus = this.Modulus;
            this._real = Math.Cos(value) * modulus;
            this._imaginary = Math.Sin(value) * modulus;
        }
    }
    public static Complex operator +(Complex z1, Complex z2)
    {
        return new Complex(z1._real + z2._real, z1._imaginary + z2._imaginary);
    }

    public static Complex operator +(Complex c)
    {
        return c;
    }

    public static Complex operator -(Complex c)
    {
        return new Complex(-c.Real, -c.Imaginary);
    }

    public static Complex operator -(Complex z1, Complex z2)
    {
        return new Complex(z1._real - z2._real, z1._imaginary - z2._imaginary);
    }

    public static Complex operator *(Complex z1, Complex z2)
    {
        return new Complex((z1._real * z2._real) - (z1._imaginary * z2._imaginary), (z1._real * z2._imaginary) + (z1._imaginary * z2._real));
    }

    public static Complex operator *(double d1, Complex z2)
    {
        return new Complex(d1 * z2._real, d1 * z2._imaginary);
    }

    public static Complex operator *(Complex z1, double d2)
    {
        return (Complex) (d2 * z1);
    }

    public static Complex operator /(Complex z1, Complex z2)
    {
        double num = (z2._real * z2._real) + (z2._imaginary * z2._imaginary);
        return new Complex(((z1._real * z2._real) + (z1._imaginary * z2._imaginary)) / num, ((z1._imaginary * z2._real) - (z1._real * z2._imaginary)) / num);
    }

    public static bool operator ==(Complex z1, Complex z2)
    {
        return ((z1._real == z2._real) && (z1._imaginary == z2._imaginary));
    }

    public static bool operator !=(Complex z1, Complex z2)
    {
        if (z1._real == z2._real)
        {
            return (z1._imaginary != z2._imaginary);
        }
        return true;
    }

    public override bool Equals(object obj)
    {
        return base.Equals(obj);
    }

    public override int GetHashCode()
    {
        return (this._real.GetHashCode() ^ this._imaginary.GetHashCode());
    }

    public override string ToString()
    {
        if (this._imaginary < 0.0)
        {
            return (this._real.ToString() + " " + this._imaginary.ToString() + " i");
        }
        return (this._real.ToString() + " +" + this._imaginary.ToString() + " i");
    }

    public string ToString(string format)
    {
        if (this._imaginary < 0.0)
        {
            return (this._real.ToString(format) + " " + this._imaginary.ToString(format) + " i");
        }
        return (this._real.ToString(format) + " +" + this._imaginary.ToString(format) + " i");
    }

    public string ToString(string format, IFormatProvider formatProvider)
    {
        if (this._imaginary >= 0.0)
        {
            return (this._real.ToString(format, formatProvider) + " +" + this._imaginary.ToString(format, formatProvider) + " i");
        }
        return (this._real.ToString(format, formatProvider) + " " + this._imaginary.ToString(format, formatProvider) + " i");
    }
}
  评论这张
 
阅读(2294)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016