첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MathHelper.cs

using System;

namespace TestProject
{
    /// <summary>
    /// 수학 헬퍼
    /// </summary>
    public static class MathHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 최대 공약수 구하기 - GetGreatestCommonDivisor(a, b)

        /// <summary>
        /// 최대 공약수 구하기
        /// </summary>
        /// <param name="a">A</param>
        /// <param name="b">B</param>
        /// <returns>최대 공약수</returns>
        public static long GetGreatestCommonDivisor(long a, long b)
        {
            a = Math.Abs(a);
            b = Math.Abs(b);

            if(a < b)
            {
                long temporary = a;

                a = b;
                b = temporary;
            }

            for(;;)
            {
                long remainder = a % b;

                if(remainder == 0)
                {
                    return b;
                }

                a = b;
                b = remainder;
            }
        }

        #endregion
        #region 최소 공배수 구하기 - GetLeastCommonMultiple(a, b)

        /// <summary>
        /// 최소 공배수 구하기
        /// </summary>
        /// <param name="a">A</param>
        /// <param name="b">B</param>
        /// <returns>최소 공배수</returns>
        public static long GetLeastCommonMultiple(long a, long b)
        {
            long gcd = GetGreatestCommonDivisor(a, b);

            return ((a / gcd) * (b / gcd)) * gcd;
        }

        #endregion
    }
}

 

728x90

 

▶ Fraction.cs

namespace TestProject
{
    /// <summary>
    /// 분수
    /// </summary>
    public class Fraction
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 분자
        /// </summary>
        public long Numerator;
        
        /// <summary>
        /// 분모
        /// </summary>
        public long Denominator;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - Fraction(text)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="text">텍스트</param>
        public Fraction(string text)
        {
            string[] valueArray = text.Split('/');

            Numerator   = long.Parse(valueArray[0]);
            Denominator = long.Parse(valueArray[1]);

            Simplify();
        }

        #endregion
        #region 생성자 - Fraction(numerator, denominator)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="numerator">분자</param>
        /// <param name="denominator">분모</param>
        public Fraction(long numerator, long denominator)
        {
            Numerator   = numerator;
            Denominator = denominator;

            Simplify();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

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

        /// <summary>
        /// - 연산자 재정의하기
        /// </summary>
        /// <param name="a">분수 A</param>
        /// <returns>분수</returns>
        public static Fraction operator -(Fraction a)
        {
            return new Fraction(-a.Numerator, a.Denominator);
        }

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

        /// <summary>
        /// + 연산자 재정의하기
        /// </summary>
        /// <param name="a">분수 A</param>
        /// <param name="b">분수 B</param>
        /// <returns>분수</returns>
        public static Fraction operator +(Fraction a, Fraction b)
        {
            long gcd = MathHelper.GetGreatestCommonDivisor(a.Denominator, b.Denominator);

            long numerator   = a.Numerator * (b.Denominator / gcd) + b.Numerator * (a.Denominator / gcd);
            long denominator = a.Denominator * (b.Denominator / gcd);

            return new Fraction(numerator, denominator);
        }

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

        /// <summary>
        /// - 연산자 재정의하기
        /// </summary>
        /// <param name="a">분수 A</param>
        /// <param name="b">분수 B</param>
        /// <returns>분수</returns>
        public static Fraction operator -(Fraction a, Fraction b)
        {
            return a + -b;
        }

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

        /// <summary>
        /// * 연산자 재정의하기
        /// </summary>
        /// <param name="a">분수 A</param>
        /// <param name="b">분수 B</param>
        /// <returns>분수</returns>
        public static Fraction operator *(Fraction a, Fraction b)
        {
            Fraction result1 = new Fraction(a.Numerator, b.Denominator);
            Fraction result2 = new Fraction(b.Numerator, a.Denominator);

            return new Fraction
            (
                result1.Numerator   * result2.Numerator,
                result1.Denominator * result2.Denominator
            );
        }

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

        /// <summary>
        /// / 연산자 재정의하기
        /// </summary>
        /// <param name="a">분수 A</param>
        /// <param name="b">분수 B</param>
        /// <returns>분수</returns>
        public static Fraction operator /(Fraction a, Fraction b)
        {
            return a * new Fraction(b.Denominator, b.Numerator);
        }

        #endregion

        #region 실수 암시적 변환 정의하기 - double(a)

        /// <summary>
        /// 실수 암시적 변환 정의하기
        /// </summary>
        /// <param name="a">분수 A</param>
        public static implicit operator double(Fraction a)
        {
            return (double)a.Numerator / a.Denominator;
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Instance
        //////////////////////////////////////////////////////////////////////////////// Public

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

        /// <summary>
        /// 문자열 구하기
        /// </summary>
        /// <returns>문자열</returns>
        public override string ToString()
        {
            return Numerator.ToString() + "/" + Denominator.ToString();
        }

        #endregion

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

        #region 단순화 하기 - Simplify()

        /// <summary>
        /// 단순화 하기
        /// </summary>
        private void Simplify()
        {
            if(Denominator < 0)
            {
                Numerator   = -Numerator;
                Denominator = -Denominator;
            }

            long gcd = MathHelper.GetGreatestCommonDivisor(Numerator, Denominator);

            Numerator   = Numerator   / gcd;
            Denominator = Denominator / gcd;
        }

        #endregion
    }
}

 

300x250

 

▶ 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)
        {
            this.aplusBTextBox.Clear();
            this.aMinusBTextBox.Clear();
            this.aMultiplyBTextBox.Clear();
            this.aDivideBTextBox.Clear();
            this.negativeATextBox.Clear();
            this.doubleATextBox.Clear();

            try
            {
                Fraction a = new Fraction(this.aTextBox.Text);
                Fraction b = new Fraction(this.bTextBox.Text);

                this.aplusBTextBox.Text     = (a + b).ToString();
                this.aMinusBTextBox.Text    = (a - b).ToString();
                this.aMultiplyBTextBox.Text = (a * b).ToString();
                this.aDivideBTextBox.Text   = (a / b).ToString();
                this.negativeATextBox.Text  = (-a).ToString();
                this.doubleATextBox.Text    = ((double)a).ToString();
            }
            catch(Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요