프로그래밍/Io

손으로 만드는 나눗셈 계산표 with Io

Scripter 2008. 5. 16. 10:54

다음은 초등학교에서 배우는 나눗셈 계산표를 만들어주는 Io 소스 코드이다.
Io는 한글 사용에 다소 문제가 있어 소스 코드에 한글을 전혀 사용하지 못하였다.
나눗셈 계산표를 완성하고 나서 약수, 배수 관계를 알려준다.

  1. /*
  2.  * Filename: makeDivisionTable.io
  3.  *
  4.  *  Purpose:  Make a division table in a handy written form.
  5.  *
  6.  *  Execute: io makeDivisionTable.io 12345 32
  7.  *                 io makeDivisionTable.io 500210 61
  8.  *
  9.  *      Date:  2008/05/15
  10.  *    Author:  PH Kim   [ pkim ((AT)) scripts.pe.kr ]
  11. */
  12. printUsage := method(
  13.     writeln("Using: io makeDivisionTable.io [numerator] [denominator]")
  14.     writeln("Make a division table in a handy written form.")
  15. )
  16. simplify := method(v, width,
  17.     t := "" .. v
  18.     if ((t size) > 2 and (t slice(t size - 2) == ".0")) then (
  19.         t = t silce(t size - 2)
  20.     )
  21.     len := t size
  22.     if (len < width) then (
  23.         t = "               " slice(0, width - len) .. t
  24.     )
  25.     t
  26. )
  27. makeTable:= method(numer, denom, quotient,
  28.     strNumer := "" .. numer
  29.     strDenom := "" .. denom
  30.     strQuotient := "" .. quotient
  31.     lenN := strNumer size
  32.     lenD := strDenom size
  33.     lenQ := strQuotient size
  34.     offsetLeft := 3 + lenD + 3
  35.     spaces := "                                                                                 "
  36.     uline  := "_"  repeated(lenN + 2)
  37.     sline  := "-" repeated(lenN)
  38.     bias := lenN - lenQ
  39.     writeln(spaces slice(0, offsetLeft) .. spaces slice(0, bias) .. quotient)
  40.     writeln(spaces slice(0, offsetLeft - 2) .. uline)
  41.     write("   " .. strDenom .. " ) " .. strNumer)
  42.     strTmpR := strNumer slice(0, bias + 1)
  43.     tmpR := strTmpR asNumber
  44.     tmpSub := 0
  45.     oneDigit := nil
  46.     for (i, 0, lenQ - 1,
  47.         if (strQuotient slice(i, i + 1) == "0") then (
  48.             if (i < lenQ - 1) then (
  49.                 oneDigit = strNumer slice(bias + i + 1, bias + i + 2)
  50.                 write(oneDigit)
  51.                 strTmpR = strTmpR .. oneDigit
  52.                 tmpR = strTmpR asNumber
  53.             )
  54.         ) else (
  55.             writeln("")
  56.             tmpSub = (strQuotient slice(i, i + 1)) asNumber * denom
  57.             writeln(spaces slice(0, offsetLeft ) .. simplify(tmpSub, bias + i + 1))
  58.             writeln(spaces slice(0, offsetLeft) .. sline)
  59.             tmpR = tmpR - tmpSub
  60.             if (tmpR == 0 and i < lenQ - 1) then (
  61.                 write(spaces slice(0, offsetLeft) .. spaces slice(0, bias + i + 1))
  62.             ) else (
  63.                 write(spaces slice(0, offsetLeft) .. simplify(tmpR, bias + i + 1))
  64.             )
  65.             strTmpR = tmpR asString
  66.             if (i < lenQ - 1) then (
  67.                 oneDigit = strNumer slice(bias + i + 1, bias + i + 2)
  68.                 write(oneDigit)
  69.                 strTmpR = strTmpR .. oneDigit
  70.                 tmpR = strTmpR asNumber
  71.             )
  72.         )
  73.     )
  74.     writeln("")
  75.     return tmpR
  76. )
  77. if (args size  < 3) then (
  78.     printUsage()
  79.     exit(1)
  80. )
  81. a := (args at(1)) asNumber
  82. b := (args at(2)) asNumber
  83. if (a <= 0) then (
  84.     writeln("Numerator: " .. a)
  85.     writeln("Numerator should be a positive integer.")
  86.     exit(1)
  87. ) elseif (b <= 0) then (
  88.     writeln("Denominator: " .. b)
  89.     writeln("Denominator should be a positive integer.")
  90.     exit(1)
  91. )
  92. q := (a / b) floor
  93. r := a % b
  94. write("The result of " .. a .. " / " .. b .. " is ")
  95. write("quotient " .. q .. ", and ")
  96. writeln("trmainder " .. r)
  97. writeln("")
  98. k := makeTable(a, b, q)
  99. if (k == r)  then (
  100.     writeln("\nThe remainder is " .. k)
  101. )
  102. if (k == 0) then (
  103.     writeln(a .. " = " .. b .. " x " .. q)
  104.     writeln(a .. " is a mupltiple of " .. b .. ".")
  105.     writeln(b .. " is a divisor of " .. a .. ".")
  106. ) else (
  107.     writeln(a .. " = " .. b .. " x " .. q .. " + " .. r)
  108.     writeln(a .. " is not a mupltiple of " .. b .. ".")
  109. )




실행> io makeDivisionTable.io 500210 61

The result of 500210 / 61 is quotient 8200, and trmainder 10
          8200
      ________
   61 ) 500210
        488
        ------
         122
         122
        ------
            10
The remainder is 10
500210 = 61 x 8200 + 10
500210 is not a mupltiple of 61.




Creative Commons License

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