728x90
반응형
728x170
▶ 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
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] SendKeys 클래스 : SendWait 정적 메소드를 사용해 메모장에 문자열 추가하기 (0) | 2021.08.30 |
---|---|
[C#/WINFORM] Control 클래스 : IsKeyLocked 정적 메소드를 사용해 CAPS LOCK 키 눌림 여부 구하기 (0) | 2021.08.27 |
[C#/WINFORM] 볼륨 제어하기 (0) | 2021.08.27 |
[C#/WINFORM] 사용자 비활성 감지하기 (0) | 2021.08.27 |
[C#/WINFORM] SystemInformation 클래스 : BootMode 정적 속성을 사용해 Windows 안전 모드 여부 구하기 (0) | 2021.08.26 |
[C#/WINFORM] Button 클래스 : 권한 상승 필요 표시 버튼 사용하기 (0) | 2021.08.26 |
[C#/WINFORM] Screen 클래스 : PrimaryScreen 정적 속성을 사용해 기본 디스플레이 해상도 구하기 (0) | 2021.08.26 |
[C#/WINFORM] Color 구조체 : 혼합 색상 구하기 (0) | 2021.08.23 |
[C#/WINFORM] Panel 클래스 : 둥근 패널 사용하기 (0) | 2021.08.22 |
[C#/WINFORM] Point 구조체 : 직선에 포함된 포인트 배열 구하기 (0) | 2021.08.22 |
댓글을 달아 주세요