다음은 초등학교에서 배우는 나눗셈 계산표를 만들어주는 C# 소스 코드이다.
나눗셈 계산표를 완성하고 나서 약수, 배수 관계를 알려준다.


  1.  /*
  2.  *  Filename: MakeDivisionTableApp.cs
  3.  *
  4.  *  Purpose:  Make a division table in a handy written form.
  5.  *
  6.  *  Compile: csc MakeDivisionTableApp.cs BigInteger.cs
  7.  *
  8.  *  Execute: MakeDivisionTableApp 12345 32
  9.  *           MakeDivisionTableApp 500210 61
  10.  *
  11.  *     Date:  2009/01/24
  12.  *   Author:  PH Kim   [ pkim ((AT)) scripts.pe.kr ]
  13.  */
  14. using System;
  15. // using System.Numeric;     // only for C# 3.0 above
  16. using ScottGarland;          // for C# 2.0   Get at http://www.codeplex.com/biginteger/Release/ProjectReleases.aspx?ReleaseId=16762
  17. namespace MyTestApplication1 {
  18.     public class MakeDivisionTableApp {
  19.         public static BigInteger ZERO = new BigInteger("0");
  20.         public static void printUsage() {
  21.              // Console.WriteLine("Using: MakeDivisionTableApp [numerator] [denominator]");
  22.              // Console.WriteLine("Make a division table in a handy written form.");
  23.              Console.WriteLine("사용법: MakeDivisionTableApp [피제수] [제수]");
  24.              Console.WriteLine("손으로 작성한 형태의 나눗셈 표를 만들어준다.");
  25.         }
  26.         public static String simplify(double v) {
  27.             String t = "" + v;
  28.             if (t.EndsWith(".0"))
  29.                 t = t.Substring(0, t.Length - 2);
  30.             return t;
  31.         }
  32.         public static String simplify(BigInteger v, int width) {
  33.             String t = "" + v;
  34.             if (t.EndsWith(".0"))
  35.                 t = t.Substring(0, t.Length - 2);
  36.             int len = t.Length;
  37.             if (len < width)
  38.                 t = "                                                                                             ".Substring(0, width - len) + t;
  39.             return t;
  40.         }
  41.         public String getSuffix(BigInteger v) {
  42.             BigInteger t = BigInteger.Modulus(v, new BigInteger("10"));
  43.             String suffix = "은";
  44.             if ("2459".IndexOf("" + t) >= 0) {
  45.                 suffix = "는";
  46.             }
  47.             return suffix;
  48.         }
  49.         public BigInteger makeTable(BigInteger numer, BigInteger denom, BigInteger quotient) {
  50.             String strNumer = "" + numer;
  51.             String strDenom = "" + denom;
  52.             String strQuotient = "" + quotient;
  53.             int lenN = strNumer.Length;
  54.             int lenD = strDenom.Length;
  55.             int lenQ = strQuotient.Length;
  56.             int offsetLeft = 3 + lenD + 3;
  57.             String spaces = "                                                                                 ";
  58.             String uline  = "_________________________________________________________________________________".Substring(0, lenN + 2);
  59.             String sline  = "---------------------------------------------------------------------------------".Substring(0, lenN);
  60.             int bias = lenN - lenQ;
  61.             Console.WriteLine(spaces.Substring(0, offsetLeft) + spaces.Substring(0, bias) + quotient);
  62.             Console.WriteLine(spaces.Substring(0, offsetLeft - 2) + uline);
  63.             Console.Write("   " + strDenom + " ) " + strNumer);
  64.             String strTmpR = strNumer.Substring(0, bias + 1);
  65.             BigInteger tmpR = new BigInteger(strTmpR);
  66.             BigInteger tmpSub = ZERO;
  67.             String oneDigit = null;
  68.             for (int i = 0; i < lenQ; i++) {
  69.                 if (strQuotient.Substring(i, 1).Equals("0")) {
  70.                     if (i + 1 < lenQ) {
  71.                         oneDigit = strNumer.Substring(bias + i + 1, 1);
  72.                         Console.Write(oneDigit);
  73.                         strTmpR += oneDigit;
  74.                         tmpR = new BigInteger(strTmpR);
  75.                     }
  76.                 }
  77.                 else {
  78.                     Console.WriteLine();
  79.                     tmpSub = BigInteger.Multiply(new BigInteger(strQuotient.Substring(i, 1)), denom);
  80.                     Console.WriteLine(spaces.Substring(0, offsetLeft) + simplify(tmpSub, bias + i + 1));
  81.                     Console.WriteLine(spaces.Substring(0, offsetLeft) + sline);
  82.                     tmpR = BigInteger.Subtract(tmpR, tmpSub);
  83.                     if (tmpR.Equals(ZERO) && i + 1 < lenQ) {
  84.                         Console.Write(spaces.Substring(0, offsetLeft) + spaces.Substring(0, bias + i + 1));
  85.                     }
  86.                     else {
  87.                         Console.Write(spaces.Substring(0, offsetLeft) + simplify(tmpR, bias + i + 1));
  88.                     }
  89.                     strTmpR = "" + tmpR;
  90.                     if (i + 1 < lenQ) {
  91.                         oneDigit = strNumer.Substring(bias + i + 1, 1);
  92.                         Console.Write(oneDigit);
  93.                         strTmpR += oneDigit;
  94.                         tmpR = new BigInteger(strTmpR);
  95.                     }
  96.                 }
  97.             }
  98.             Console.WriteLine();
  99.             return tmpR;
  100.         }
  101.         public static void Main(String[] args) {
  102.             if (args.Length < 2) {
  103.                 printUsage();
  104.                 Environment.Exit(1);
  105.             }
  106.             BigInteger a = null;
  107.             BigInteger b = null;
  108.             try {
  109.                 a = new BigInteger(args[0]);
  110.                 b = new BigInteger(args[1]);
  111.             }
  112.             catch (ArgumentOutOfRangeException ex) {
  113.                 Console.WriteLine(ex);
  114.                 Console.WriteLine("피제수: " + args[0] + ", 제수: " + args[1]);
  115.                 Console.WriteLine("숫자 입력에 오류가 있습니다.");
  116.                 Environment.Exit(1);
  117.             }
  118.             catch (ArgumentNullException ex) {
  119.                 Console.WriteLine(ex);
  120.             }
  121.             catch (FormatException ex) {
  122.                 Console.WriteLine(ex);
  123.                 Console.WriteLine("피제수: " + args[0] + ", 제수: " + args[1]);
  124.                 Console.WriteLine("숫자 입력에 오류가 있습니다.");
  125.                 Environment.Exit(1);
  126.             }
  127.             if (BigInteger.Compare(a, ZERO) <= 0) {
  128.                 Console.WriteLine("피제수: " + a);
  129.                 Console.WriteLine("피제수는 양의 정수라야 합니다.");
  130.                 Environment.Exit(1);
  131.             }
  132.             else if (BigInteger.Compare(b, ZERO) <= 0) {
  133.                 Console.WriteLine("제수: " + b);
  134.                 Console.WriteLine("제수는 양의 정수라야 합니다.");
  135.                 Environment.Exit(1);
  136.             }
  137.             MakeDivisionTableApp app = new MakeDivisionTableApp();
  138.             BigInteger q = BigInteger.Divide(a, b);
  139.             BigInteger r = BigInteger.Modulus(a, b);
  140.             Console.Write("나눗셈 " + a + " ÷ " + b + " 의 결과: ");
  141.             Console.Write("몫: " + q + ", ");
  142.             Console.WriteLine("나머지: " + r);
  143.             Console.WriteLine();
  144.             BigInteger k = app.makeTable(a, b, q);
  145.             Console.WriteLine();
  146.             if (k.Equals(r)) {
  147.                 Console.WriteLine("나머지: " + k);
  148.             }
  149.             if (k.Equals(ZERO)) {
  150.                 Console.WriteLine(a + " = " + b + " x " + q);
  151.                 Console.WriteLine(a + app.getSuffix(a) + " " + b + "의 배수(mupltiple)이다.");
  152.                 Console.WriteLine(b + app.getSuffix(b) + " " + a + "의 약수(divisor)이다.");
  153.             }
  154.             else {
  155.                 Console.WriteLine(a + " = " + b + " x " + q + " + " + r);
  156.                 Console.WriteLine(a + app.getSuffix(a) + " " + b + "의 배수(mupltiple)가 아니다.");
  157.             }
  158.         }
  159.     }
  160. }





실행> MakeDivisionTableApp 500210 61

나눗셈 500210 ÷ 61 의 결과: 몫: 8200, 나머지: 10

          8200
      ________
   61 ) 500210
        488
        ------
         122
         122
        ------
            10

나머지: 10
500210 = 61 x 8200 + 10
500210은 61의 배수(mupltiple)가 아니다.




Creative Commons License

이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,