■ 복소수 사용하기

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


TestProject.zip


Complex.cs

 

 

using System;

 

namespace TestProject

{

    /// <summary>

    /// 복소수

    /// </summary>

    public class Complex

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 실수

        /// </summary>

        public double Real = 0;

        

        /// <summary>

        /// 허수

        /// </summary>

        public double Imaginary = 0;

 

        #endregion

 

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

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

 

        #region 생성자 - Complex(real, imaginary)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="real">실수</param>

        /// <param name="imaginary">허수</param>

        public Complex(double real, double imaginary)

        {

            Real      = real;

            Imaginary = imaginary;

        }

 

        #endregion

        #region 생성자 - Complex(real)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="real">실수</param>

        public Complex(double real) : this(real, 0)

        {

        }

 

        #endregion

 

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

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

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

 

        #region 파싱하기 - Parse(text)

 

        /// <summary>

        /// 파싱하기

        /// </summary>

        /// <param name="text">텍스트</param>

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

        public static Complex Parse(string text)

        {

            text = text.Replace(" ", "");

            text = text.ToLower().Replace("i", "");

 

            char[] signArray = { '+', '-' };

 

            int position = text.IndexOfAny(signArray);

 

            if(position == 0)

            {

                position = text.IndexOfAny(signArray, 1);

            }

 

            double real      = double.Parse(text.Substring(0, position));

            double imaginary = double.Parse(text.Substring(position));

 

            return new Complex(real, imaginary);

        }

 

        #endregion

 

        #region - 연산자 재정의하기 - -(complex)

 

        /// <summary>

        /// - 연산자 재정의하기

        /// </summary>

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

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

        public static Complex operator -(Complex complex)

        {

            return new Complex(-complex.Real, -complex.Imaginary);

        }

 

        #endregion

        #region + 연산자 재정의하기 - +(a, b)

 

        /// <summary>

        /// + 연산자 재정의하기

        /// </summary>

        /// <param name="a">복소수 A</param>

        /// <param name="b">복소수 B</param>

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

        public static Complex operator +(Complex a, Complex b)

        {

            return new Complex(a.Real + b.Real, a.Imaginary + b.Imaginary);

        }

 

        #endregion

        #region - 연산자 재정의하기 - -(a, b)

 

        /// <summary>

        /// - 연산자 재정의하기

        /// </summary>

        /// <param name="a">복소수 A</param>

        /// <param name="b">복소수 B</param>

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

        public static Complex operator -(Complex a, Complex b)

        {

            return a + (-b);

        }

 

        #endregion

        #region * 연산자 재정의하기 - *(a, b)

 

        /// <summary>

        /// * 연산자 재정의하기

        /// </summary>

        /// <param name="a">복소수 A</param>

        /// <param name="b">복소수 B</param>

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

        public static Complex operator *(Complex a, Complex b)

        {

            return new Complex

            (

                a.Real * b.Real      - a.Imaginary * b.Imaginary,

                a.Real * b.Imaginary + a.Imaginary * b.Real

            );

        }

 

        #endregion

        #region / 연산자 재정의하기 - /(a, b)

 

        /// <summary>

        /// / 연산자 재정의하기

        /// </summary>

        /// <param name="a">복소수 A</param>

        /// <param name="b">복소수 B</param>

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

        public static Complex operator /(Complex a, Complex b)

        {

            Complex conjugate = new Complex(b.Real, -b.Imaginary);

 

            b *= conjugate;

 

            Complex numerator = a * conjugate;

 

            return new Complex

            (

                numerator.Real / b.Real,

                numerator.Imaginary / b.Real

            );

        }

 

        #endregion

 

        #region == 연산자 재정의하기 - ==(a, b)

 

        /// <summary>

        /// == 연산자 재정의하기

        /// </summary>

        /// <param name="a">복소수 A</param>

        /// <param name="b">복소수 B</param>

        /// <returns>비교 결과</returns>

        public static bool operator ==(Complex a, Complex b)

        {

            if(Object.ReferenceEquals(a, b))

            {

                return true;

            }

 

            if(((object)a == null) || ((object)b == null))

            {

                return false;

            }

 

            return a.Equals(b);

        }

 

        #endregion

        #region != 연산자 재정의하기 - !=(a, b)

 

        /// <summary>

        /// != 연산자 재정의하기

        /// </summary>

        /// <param name="a">복소수 A</param>

        /// <param name="b">복소수 B</param>

        /// <returns>비교 결과</returns>

        public static bool operator !=(Complex a, Complex b)

        {

            return !(a == b);

        }

 

        #endregion

 

        #region 묵시적 변환하기 - Complex(real)

 

        /// <summary>

        /// 묵시적 변환하기

        /// </summary>

        /// <param name="real">실수</param>

        public static implicit operator Complex(double real)

        {

            return new Complex(real);

        }

 

        #endregion

 

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

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

 

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

 

        /// <summary>

        /// 문자열 구하기

        /// </summary>

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

        public override string ToString()

        {

            if(Imaginary < 0)

            {

                return Real.ToString() + " - " + Math.Abs(Imaginary).ToString() + "i";

            }

            else

            {

                return Real.ToString() + " + " + Imaginary.ToString() + "i";

            }

        }

 

        #endregion

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

 

        /// <summary>

        /// 문자열 구하기

        /// </summary>

        /// <param name="format">포맷 문자열</param>

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

        public string ToString(string format)

        {

            if(Imaginary < 0)

            {

                return Real.ToString(format) + " - " + Math.Abs(Imaginary).ToString(format) + "i";

            }

            else

            {

                return Real.ToString(format) + " + " + Imaginary.ToString(format) + "i";

            }

        }

 

        #endregion

 

        #region 동일 여부 구하기 - Equals(b)

 

        /// <summary>

        /// 동일 여부 구하기

        /// </summary>

        /// <param name="b">복소수 B</param>

        /// <returns>동일 여부</returns>

        public bool Equals(Complex b)

        {

            if((object)b == null)

            {

                return false;

            }

 

            return ((this.Real == b.Real) && (this.Imaginary == b.Imaginary));

        }

 

        #endregion

        #region 동일 여부 구하기 - Equals(sourceObject)

 

        /// <summary>

        /// 동일 여부 구하기

        /// </summary>

        /// <param name="sourceObject">소스 객체</param>

        /// <returns>동일 여부</returns>

        public override bool Equals(object sourceObject)

        {

            if(sourceObject == null)

            {

                return false;

            }

 

            if(!(sourceObject is Complex))

            {

                return false;

            }

 

            return this.Equals(sourceObject as Complex);

        }

 

        #endregion

 

        #region 해시 코드 구하기 - GetHashCode()

 

        /// <summary>

        /// 해시 코드 구하기

        /// </summary>

        /// <returns>해시 코드</returns>

        public override int GetHashCode()

        {

            return (Real.GetHashCode() + Imaginary).GetHashCode();

        }

 

        #endregion

    }

}

 

 

MainForm.cs

 

 

using System;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            this.calculateButton.Click += calculateButton_Click;

 

            #endregion

        }

 

        #endregion

 

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

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 계산하기 버튼 클릭시 처리하기 - calculateButton_Click(sender, e)

 

        /// <summary>

        /// 계산하기 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void calculateButton_Click(object sender, EventArgs e)

        {

            Complex complexA = Complex.Parse(this.complexATextBox.Text);

            Complex complexB = Complex.Parse(this.complexBTextBox.Text);

            double  real     = double.Parse(this.realTextBox.Text);

 

            this.complexAPlusRealTextBox.Text     = (complexA + real).ToString();

            this.realPlusComplexATextBox.Text     = (real + complexA).ToString();

            this.complexAMultiplyRealTextBox.Text = (complexA * real).ToString();

            this.realMultiplyComplexATextBox.Text = (real * complexA).ToString();

            this.complexAMinusRealTextBox.Text    = (complexA - real).ToString();

            this.realMinusComplexATextBox.Text    = (real - complexA).ToString();

 

            this.complexADivideRealTextBox.Text = (complexA / real).ToString("0.0000");

 

            Complex complexC = (complexA / real) * real;

 

            this.complexADivideRealMultiplyRealTextBox.Text = complexC.ToString();

 

            this.realDivideComplexATextBox.Text = (real / complexA).ToString("0.0000");

 

            Complex complexD = (real / complexA) * complexA;

 

            this.realDivideComplexAMultiplyComplexATextBox.Text = complexD.ToString();

 

            Complex complexE = (Complex)real;

 

            this.castRealTextBox.Text = complexE.ToString();

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG