■ 이집트 분수를 사용하는 방법을 보여준다.
▶ 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
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
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 |
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 } } |