■ 분수를 사용하는 방법을 보여준다.
▶ MathHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
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 } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
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 } } |