■ 이집트 분수 사용하기

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


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

    }

}

 

 

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 < 연산자 재정의하기 - <(a, b)

 

        /// <summary>

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

        /// </summary>

        /// <param name="a">분수 A</param>

        /// <param name="b">분수 B</param>

        /// <returns>처리 결과</returns>

        public static bool operator <(Fraction a, Fraction b)

        {

            return (double)a < (double)b;

        }

 

        #endregion

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

 

        /// <summary>

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

        /// </summary>

        /// <param name="a">분수 A</param>

        /// <param name="b">분수 B</param>

        /// <returns>처리 결과</returns>

        public static bool operator >(Fraction a, Fraction b)

        {

            return (double)a > (double)b;

        }

 

        #endregion

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

 

        /// <summary>

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

        /// </summary>

        /// <param name="a">분수 A</param>

        /// <param name="b">분수 B</param>

        /// <returns>처리 결과</returns>

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

        {

            return ((a.Numerator == b.Numerator) && (a.Denominator == b.Denominator));

        }

 

        #endregion

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

 

        /// <summary>

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

        /// </summary>

        /// <param name="a">분수 A</param>

        /// <param name="b">분수 B</param>

        /// <returns>처리 결과</returns>

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

        {

            return (!(a == b));

        }

 

        #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

        #region 분수 암시적 변환 정의하기 - Fraction(value)

 

        /// <summary>

        /// 분수 암시적 변환 정의하기

        /// </summary>

        /// <param name="value"></param>

        public static implicit operator Fraction(long value)

        {

            return new Fraction(value, 1);

        }

 

        #endregion

 

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

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

 

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

 

        /// <summary>

        /// 해시 코드 구하기

        /// </summary>

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

        public override int GetHashCode()

        {

            return Numerator.GetHashCode() ^ Denominator.GetHashCode();

        }

 

        #endregion

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

 

        /// <summary>

        /// 동일 여부 구하기

        /// </summary>

        /// <param name="source">소스</param>

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

        public override bool Equals(object source)

        {

            if(!(source is Fraction))

            {

                return false;

            }

 

            Fraction b = source as Fraction;

 

            return (this == b);

        }

 

        #endregion

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

 

        /// <summary>

        /// 문자열 구하기

        /// </summary>

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

        public override string ToString()

        {

            if(Denominator == 1)

            {

                return Numerator.ToString();

            }

 

            return Numerator.ToString() + "/" + Denominator.ToString();

        }

 

        #endregion

 

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

 

        #region 단순화 하기 - Simplify()

 

        /// <summary>

        /// 단순화 하기

        /// </summary>

        private void Simplify()

        {

            if(Denominator < 0)

            {

                Numerator   = -Numerator;

                Denominator = -Denominator;

            }

 

            if(Numerator == 0)

            {

                return;

            }

 

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

 

            Numerator   = Numerator   / gcd;

            Denominator = Denominator / gcd;

        }

 

        #endregion

    }

}

 

 

MainForm.cs

 

 

using System;

using System.Collections.Generic;

using System.Text;

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)

        {

            Fraction sourceFraction = new Fraction(this.fractionTextBox.Text);

 

            if(sourceFraction < 0)

            {

                sourceFraction = -sourceFraction;

            }

 

            List<Fraction> fractionList = GetEgyptianFraction(sourceFraction);

 

            StringBuilder stringBuilder = new StringBuilder();

 

            foreach(Fraction fraction in fractionList)

            {

                if(stringBuilder.Length > 0)

                {

                    stringBuilder.Append(" + ");

                }

 

                stringBuilder.Append(fraction.ToString());

            }

 

            this.resultTextBox.Text = stringBuilder.ToString();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 이집트 분수 구하기 - GetEgyptianFraction(sourceFraction)

 

        /// <summary>

        /// 이집트 분수 구하기

        /// </summary>

        /// <param name="sourceFraction">소스 분수</param>

        /// <returns>분수 리스트</returns>

        private List<Fraction> GetEgyptianFraction(Fraction sourceFraction)

        {

            List<Fraction> resultFractionList = new List<Fraction>();

 

            int wholePart = (int)(sourceFraction.Numerator / sourceFraction.Denominator);

 

            if(wholePart > 0)

            {

                resultFractionList.Add(wholePart);

 

                sourceFraction = sourceFraction - wholePart;

            }

 

            long denominator = 2;

 

            while(sourceFraction > 0)

            {

                Fraction fraction = new Fraction(1, denominator);

 

                while(fraction > sourceFraction)

                {

                    denominator++;

 

                    fraction = new Fraction(1, denominator);

                }

 

                resultFractionList.Add(fraction);

 

                sourceFraction -= fraction;

 

                denominator++;

            }

 

            return resultFractionList;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요