■ 복소수 구조체

----------------------------------------------------------------------------------------------------

using System;

 

/// <summary>

/// 복소수

/// </summary>

public struct Complex

{

    //////////////////////////////////////////////////////////////////////////////////////////////////// Field

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region Field

 

    /// <summary>

    /// 실수부

    /// </summary>

    private double real;

 

    /// <summary>

    /// 가수부

    /// </summary>

    private double imaginary;

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property

    ////////////////////////////////////////////////////////////////////////////////////////// Static

    //////////////////////////////////////////////////////////////////////////////// Public

 

    #region 0 - Zero

 

    /// <summary>

    /// 0

    /// </summary>

    public static Complex Zero

    {

        get

        {

            return new Complex(0d, 0d);

        }

    }

 

    #endregion

 

    ////////////////////////////////////////////////////////////////////////////////////////// Instance

    //////////////////////////////////////////////////////////////////////////////// Public

 

    #region 실수부 - Real

 

    /// <summary>

    /// 실수부

    /// </summary>

    public double Real

    {

        get

        {

            return this.real;

        }

        set

        {

            this.real = value;

        }

    }

 

    #endregion

 

    #region 가수부 - Imaginary

 

    /// <summary>

    /// 가수부

    /// </summary>

    public double Imaginary

    {

        get

        {

            return this.imaginary;

        }

        set

        {

            this.imaginary = value;

        }

    }

 

    #endregion

 

    #region 크기 - Magnitude

 

    /// <summary>

    /// 크기

    /// </summary>

    public double Magnitude

    {

        get

        {

            return Math.Sqrt(this.real * this.real + this.imaginary * this.imaginary);

        }

    }

 

    #endregion

 

    #region 위상 - Phase

 

    /// <summary>

    /// 위상

    /// </summary>

    public double Phase

    {

        get

        {

            return Math.Atan(this.imaginary / this.real);

        }

    }

 

    #endregion

 

    #region 제곱 크기 - SquaredMagnitude

 

    /// <summary>

    /// 제곱 크기

    /// </summary>

    public double SquaredMagnitude

    {

        get

        {

            return (this.real * this.real + this.imaginary * this.imaginary);

        }

    }

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region 생성자 - 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;

    }

 

    #endregion

 

    #region 생성자 - Complex(complex)

 

    /// <summary>

    /// 생성자

    /// </summary>

    /// <param name="complex">복소수</param>

    public Complex(Complex complex)

    {

        this.real      = complex.Real;

        this.imaginary = complex.Imaginary;

    }

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method

    ////////////////////////////////////////////////////////////////////////////////////////// Static

    //////////////////////////////////////////////////////////////////////////////// Public

 

    #region + 연산자 - operator+(complex1, complex2)

 

    /// <summary>

    /// + 연산자

    /// </summary>

    /// <param name="complex1">복소수 1</param>

    /// <param name="complex2">복소수 2</param>

    /// <returns>복소수</returns>

    public static Complex operator+(Complex complex1, Complex complex2)

    {

        return new Complex(complex1.Real + complex2.Real, complex1.Imaginary + complex2.Imaginary);

    }

 

    #endregion

 

    #region - 연산자 - operator-(complex1, complex2)

 

    /// <summary>

    /// 연산자 -

    /// </summary>

    /// <param name="complex1">복소수 1</param>

    /// <param name="complex2">복소수 2</param>

    /// <returns>복소수</returns>

    public static Complex operator-(Complex complex1, Complex complex2)

    {

        return new Complex(complex1.Real - complex2.Real, complex1.Imaginary - complex2.Imaginary);

    }

 

    #endregion

 

    #region * 연산자 - operator*(complex1, complex2)

 

    /// <summary>

    /// 연산자 *

    /// </summary>

    /// <param name="complex1">복소수 1</param>

    /// <param name="complex2">복소수 2</param>

    /// <returns>복소수</returns>

    public static Complex operator*(Complex complex1, Complex complex2)

    {

        return new Complex

        (

            complex1.Real * complex2.Real - complex1.Imaginary * complex2.Imaginary,

            complex1.Real * complex2.Imaginary + complex1.Imaginary * complex2.Real

        );

    }

 

    #endregion

 

    #region / 연산자 - operator/(complex1, complex2)

 

    /// <summary>

    /// / 연산자

    /// </summary>

    /// <param name="complex1">복소수 1</param>

    /// <param name="complex2">복소수 2</param>

    /// <returns>복소수</returns>

    public static Complex operator/(Complex complex1, Complex complex2)

    {

        double divider = complex2.Real * complex2.Real + complex2.Imaginary * complex2.Imaginary;

 

        if(divider == 0)

        {

            throw new DivideByZeroException();

        }

            

        return new Complex

        (

            (complex1.Real * complex2.Real + complex1.Imaginary * complex2.Imaginary) / divider,

            (complex1.Imaginary * complex2.Real - complex1.Real * complex2.Imaginary) / divider

        );

    }

 

    #endregion

 

    ////////////////////////////////////////////////////////////////////////////////////////// Instance

    //////////////////////////////////////////////////////////////////////////////// Public

 

    #region 문자열 구하기 - ToString()

 

    /// <summary>

    /// 문자열 구하기

    /// </summary>

    /// <returns>문자열</returns>

    public override string ToString()

    {

        return string.Format

        (

            "{0}{1}{2}i",

            this.real,

            ((this.imaginary < 0) ? '-' : '+'),

            Math.Abs(this.imaginary)

        );

    }

 

    #endregion

}

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요