첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.01MB

▶ NumberToWordConverter.cs

using System;

namespace TestProject
{
    /// <summary>
    /// 숫자→단어 변환자
    /// </summary>
    public class NumberToWordConverter
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 숫자 집합 배열 1
        /// </summary>
        private string[] numberSetArray1 = new string[]
        {
            "Zero",
            "One",
            "Two",
            "Three",
            "Four",
            "Five",
            "Six",
            "Seven",
            "Eight",
            "Nine",
            "Ten",
            "Eleven",
            "Twelve",
            "Thirteen",
            "Fourteen",
            "Fifteen",
            "Sixteen",
            "Seventeen",
            "Eighteen",
            "Nineteen"
        };

        /// <summary>
        /// 숫자 집합 배열 2
        /// </summary>
        private string[] numberSetArray2 = new string[]
        {
            string.Empty,
            string.Empty,
            "Twenty",
            "Thirty",
            "Forty",
            "Fifty",
            "Sixty",
            "Seventy",
            "Eighty",
            "Ninety"
        };

        /// <summary>
        /// 숫자 집합 배열 3
        /// </summary>
        private string[] numberSetArray3 = new string[]
        {
            string.Empty,
            "Thousand",
            "Million",
            "Billion"
        };

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 변환하기 - Convert(number)

        /// <summary>
        /// 변환하기
        /// </summary>
        /// <param name="number">숫자</param>
        /// <returns>단어</returns>
        public string Convert(int number)
        {
            if(number == 0)
            {
                return this.numberSetArray1[0];
            }

            int[] digitGroupArray = new int[4];

            int positive = Math.Abs(number);

            for(int i = 0; i < 4; i++)
            {
                digitGroupArray[i] = positive % 1000;

                positive /= 1000;
            }

            string[] groupTextArray = new string[4];

            for(int i = 0; i < 4; i++)
            {
                groupTextArray[i] = GetGroupText(digitGroupArray[i]);
            }

            string combined = groupTextArray[0];

            bool appendAnd;

            appendAnd = (digitGroupArray[0] > 0) && (digitGroupArray[0] < 100);

            for(int i = 1; i < 4; i++)
            {
                if(digitGroupArray[i] != 0)
                {
                    string prefix = groupTextArray[i] + " " + numberSetArray3[i];

                    if(combined.Length != 0)
                    {
                        prefix += appendAnd ? " and " : ", ";
                    }

                    appendAnd = false;

                    combined = prefix + combined;
                }
            }

            if(number < 0)
            {
                combined = "Negative " + combined;
            }

            return combined;
        }

        #endregion

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

        #region 그룹 텍스트 구하기 - GetGroupText(threeDigit)

        /// <summary>
        /// 그룹 텍스트 구하기
        /// </summary>
        /// <param name="threeDigit">3자리 숫자</param>
        /// <returns>그룹 텍스트</returns>
        private string GetGroupText(int threeDigit)
        {
            string groupText = string.Empty;

            int hundred = threeDigit / 100;
            int tenUnit = threeDigit % 100;

            if(hundred != 0)
            {
                groupText += this.numberSetArray1[hundred] + " Hundred";

                if(tenUnit != 0)
                {
                    groupText += " and ";
                }
            }

            int ten  = tenUnit / 10;
            int unit = tenUnit % 10;

            if(ten >= 2)
            {
                groupText += this.numberSetArray2[ten];

                if(unit != 0)
                {
                    groupText += " " + this.numberSetArray1[unit];
                }
            }
            else if(tenUnit != 0)
            {
                groupText += this.numberSetArray1[tenUnit];
            }

            return groupText;
        }

        #endregion
    }
}

 

728x90

 

▶ MainForm.cs

using System;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 변환자
        /// </summary>
        private NumberToWordConverter converter = new NumberToWordConverter();

        #endregion

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

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.convertButton.Click += convertButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 변환 버튼 클릭시 처리하기 - convertButton_Click(sender, e)

        /// <summary>
        /// 변환 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void convertButton_Click(object sender, EventArgs e)
        {
            try
            {
                int number;

                if(int.TryParse(this.numberTextBox.Text, out number))
                {
                    this.resultTextBox.Text = converter.Convert(number);
                }
                else
                {
                    this.resultTextBox.Text = "유효한 정수를 입력해 주시기 바랍니다.";
                }
            }
            catch(Exception exception)
            {
                this.resultTextBox.Text = $"예외 : {exception.Message}";
            }
        }

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

댓글을 달아 주세요