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

한글 출력을 정상적으로 볼려면 소스파일을 UTF-8 인코딩으로 저장해야 한다.


  1. #  Filename: makeDivisionTable.boo
  2. #
  3. #  Purpose:  Make a division table in a handy written form.
  4. #
  5. #  Execute: booi makeDivisionTable.boo 12345 32
  6. #           booi makeDivisionTable.boo 500210 61
  7. #
  8. #     Date:  2009/04/01
  9. #   Author:  PH Kim   [ pkim ((AT)) scripts.pe.kr ]
  10. import System
  11. def printUsage():
  12.     # print("Using: booi makeDivisionTable.boo [numerator] [denominator]")
  13.     # print("Make a division table in a handy written form.")
  14.     print("사용법: booi makeDivisionTable.boo [피제수] [제수]")
  15.     print("손으로 작성한 형태의 나눗셈 표를 만들어준다.")
  16. def simplify(v as double, width as int):
  17.     t = "${v}"
  18.     if t[-2:] == ".0":
  19.         t = t[0:len(t) - 2]
  20.     slen = len(t)
  21.     if slen < width:
  22.         t = " " * (width - slen) + t
  23.     return t
  24. def getSuffix(v as long):
  25.     t = v % 10L
  26.     suffix = "은"
  27.     if "2459".IndexOf("${t}") >= 0:
  28.         suffix = "는"
  29.     return suffix
  30. def makeTable(numer as long, denom as long, quotient as long):
  31.     strNumer = "${numer}"
  32.     strDenom = "${denom}"
  33.     strQuotient = "${quotient}"
  34.     lenN = len(strNumer)
  35.     lenD = len(strDenom)
  36.     lenQ = len(strQuotient)
  37.     offsetLeft = 3 + lenD + 3
  38.     spaces = "                                                                                 "
  39.     uline  = "_" * (lenN + 2)
  40.     sline  = "-" * lenN
  41.     bias = lenN - lenQ
  42.     print(spaces[0:offsetLeft] + spaces[0:bias] + "${quotient}")
  43.     print(spaces[0:offsetLeft - 2] + uline)
  44.     write("   " + strDenom + " ) " + strNumer)
  45.     strTmpR = strNumer[0: bias + 1]
  46.     tmpR as long = Convert.ToInt64(strTmpR)
  47.     tmpSub as long = 0L
  48.     oneDigit = null
  49.     for i in range(0, lenQ):
  50.         if strQuotient[i: i + 1] == "0":
  51.             if i + 1 < lenQ:
  52.                 oneDigit = strNumer[bias + i + 1: bias + i + 2]
  53.                 write(oneDigit)
  54.                 strTmpR = strTmpR + oneDigit
  55.                 tmpR = Convert.ToInt64(strTmpR)
  56.         else:
  57.             print
  58.             tmpSub = Convert.ToInt64(strQuotient[i: i + 1]) * denom
  59.             print(spaces[0: offsetLeft] + simplify(tmpSub, bias + i + 1))
  60.             print(spaces[0: offsetLeft] + sline)
  61.             tmpR = tmpR - tmpSub
  62.             if tmpR == 0L and i + 1 < lenQ:
  63.                 write(spaces[0: offsetLeft] + spaces[0: bias + i + 1])
  64.             else:
  65.                 write(spaces[0: offsetLeft] + simplify(tmpR, bias + i + 1))
  66.             strTmpR = "${tmpR}"
  67.             if i + 1 < lenQ:
  68.                 oneDigit = strNumer[bias + i + 1: bias + i + 2]
  69.             write(oneDigit)
  70.             strTmpR = strTmpR + oneDigit
  71.             tmpR = Convert.ToInt64(strTmpR)
  72.     print
  73.     return tmpR
  74. def write(s):
  75.     Console.Write(s)
  76. if len(argv) < 2:
  77.     printUsage()
  78.     Environment.Exit(1)
  79. a as long
  80. b as long
  81. try:
  82.     a = Convert.ToInt64(argv[0])
  83.     b = Convert.ToInt64(argv[1])
  84. except ValueError:
  85.     print("피제수: ${argv[0]}, 제수: ${argv[1]}")
  86.     print("숫자 입력에 오류가 있습니다.")
  87.     Environment.Exit(1)
  88. if a <= 0L:
  89.     print("피제수: ${a}")
  90.     print("피제수는 양의 정수라야 합니다.")
  91.     Environment.Exit(1)
  92. elif b <= 0L:
  93.     print("제수: ${b}")
  94.     print("제수는 양의 정수라야 합니다.");
  95.     Environment.Exit(1)
  96. q as long = a / b
  97. r as long = a % b
  98. write("나눗셈 ${a} ÷ ${b} 의 결과: ")
  99. write("몫: ${q}, ")
  100. print("나머지: ${r}")
  101. print
  102. k as long = makeTable(a, b, q)
  103. if k == r:
  104.     print("\n나머지: ${k}")
  105. if k == 0L:
  106.     print("${a} = ${b} x ${q}")
  107.     print("${a}${getSuffix(a)} ${b}의 배수(mupltiple)이다.")
  108.     print("${b}${getSuffix(b)} ${a}의 약수(divisor)이다.")
  109. else:
  110.     print("${a} = ${b} x ${q} + ${r}" % (a, b, q, r)) 
  111.     print("${a}${getSuffix(a)} ${b}의 배수(mupltiple)가 아니다.")




실행> booi makeDivisionTable.boo 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



Posted by Scripter
,