다음은  대화형 모드(interactive mode)에서 진법 변환(radix conversion)하는 Julia 소스 코드이다.
메뉴는 주메뉴 Command: (S)et radix, (A)bout, (Q)uit or E(x)it
와 부메뉴 SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
로 구성되어 있으며, 진법 변환의 핵심은 Julia 의 빌트인 함수 parse_int(string, radix)와 소스 코드에 자체 작성된 함수 itoa(number, radix)의 사용이다.

         val = parse_int(s, srcRdx)
         ret = itoa(val, destRdx)

지원되는 진법은 2진법에서 36진법까지이다.

  1. ##  Filename: convertRadix.jl
  2. ##            Convert radix in a interactive mode.
  3. ##
  4. ##  Execute: julia convertRadix.jl
  5. ##
  6. ##      Date:  2013. 3. 5.
  7. ##    Author:  pkim __AT__ scripts ((DOT)) pe ((DOT)) kr
  8. # The function println() is defined already in Julia language.
  9. # function println(s...)
  10. #     if length(s) > 0
  11. #         println(s[1])
  12. #     else
  13. #         println("")
  14. # end
  15. function printUsage()
  16.     println("Usage: julia convertRadix.jl")
  17.     println("Convert radix in a interactive mode, where the maximum radix is 36.")
  18. end
  19. function printAbout()
  20.     println("    About: Convert radix in a interactive mode.")
  21. end
  22. function printMainMenu()
  23.     println("  Command: (S)et radix, (A)bout, (Q)uit or E(x)it")
  24. end
  25. function printMainPrompt()
  26.     print("  Prompt> ")
  27. end
  28. function printSubMenu(srcRadix, destRadix)
  29.     println("    Convert Radix_" * string(srcRadix) * " to Radix_" * string(destRadix))
  30.     println("    SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit")
  31. end
  32. function printSubPrompt()
  33.     print("    Input Value>> ")
  34. end
  35. BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  36. function itoa(num, radix)
  37.    isNegative = false
  38.    if num < 0
  39.       isNegative = true
  40.       num = -num
  41.    end
  42.    arr = ""
  43.    q, r = num, 0
  44.    while q >= radix
  45.       # q, r = divmod(q, radix)
  46.       r = mod(q, radix)
  47.       q = div(q, radix)
  48.       arr *= BASE36[r+1]
  49.    end
  50.    arr *= BASE36[q + 1]
  51.    if isNegative
  52.       arr *= "-"
  53.    end
  54.    return reverse(arr)
  55. end
  56. function convertRadix(s, srcRdx, destRdx)
  57.     ret = ""
  58.     try
  59.         val = parse_int(s, srcRdx)
  60.         ret = itoa(val, destRdx)
  61.         return uppercase(ret)
  62.     catch x
  63.         println(x)
  64.         println("    Error: Cannot convert radix " * string(srcRdx))
  65.         return uppercase(ret)
  66.     end
  67. end
  68. function doConvert(srcRadix, destRadix)
  69.     line = ""
  70.     cmd = ""
  71.     srcStr = ""
  72.     destStr = ""
  73.     println("")
  74.     printSubMenu(srcRadix, destRadix)
  75.     try
  76.         while true
  77.             printSubPrompt()
  78.             cmd = ""
  79.             while length(cmd) == 0
  80.                 cmd = readline(STDIN)
  81.                 cmd = strip(cmd)
  82.             end
  83.             if "main()" == cmd
  84.                 return
  85.             elseif "exit()" == cmd || "quit()" == cmd
  86.                 exit(0)
  87.             end
  88.             try
  89.                 parse_int(cmd, srcRadix)
  90.                 srcStr = cmd
  91.                 destStr = convertRadix(srcStr, srcRadix, destRadix)
  92.                 println("        ( " * srcStr * " )_" * string(srcRadix) *  "   --->   ( " * destStr * " )_" * string(destRadix))
  93.                 println("")
  94.             catch x
  95.                  print ("")    # pass
  96.             end
  97.         end
  98.     catch x
  99.         println(x)
  100.     end
  101. end
  102. function doStart()
  103.     line = ""
  104.     cmd = ""
  105.     srcRadix = 10
  106.     destRadix = 10
  107.     srcStr = ""
  108.     destStr = ""
  109.     onlyOnce = true
  110.     try
  111.         while true
  112.             println()
  113.             if onlyOnce
  114.                 println("  The supported maximum radix is 36.")
  115.                 onlyOnce = false
  116.             end
  117.             printMainMenu()
  118.             printMainPrompt()
  119.             cmd = ""
  120.             while length(cmd) == 0
  121.                 cmd = readline(STDIN)
  122.                 cmd = strip(cmd)
  123.             end
  124.             if length(search("qQxX", cmd)) >= 1 && length(cmd) == 1
  125.                 exit(0)
  126.             elseif length(search("aA", cmd)) >= 1 && length(cmd) == 1
  127.                 printAbout()
  128.             elseif length(search("sS", cmd)) >= 1 && length(cmd) == 1
  129.                 print("  Input the source and target radices (say, 16 2): ")
  130.                 line = readline(STDIN)
  131.                 st = split(line, " ")
  132.                 while length(st) < 2
  133.                     print("  Input the source and target radices (say, 16 2): ")
  134.                     line = readline(STDIN)
  135.                     st = split(linem, " ")
  136.                 end
  137.                 try
  138.                     srcRadix = parse_int(st[1])
  139.                     destRadix = parse_int(st[2])
  140.                     doConvert(srcRadix, destRadix)
  141.                 catch ex
  142.                     println(ex)
  143.                     println("Try again!")
  144.                 end
  145.             end
  146.         end
  147.     catch x
  148.         println(x)
  149.     end
  150. end
  151. # Begin here
  152. if length(ARGS) > 0 && "-h" == ARGS[1]
  153.     printUsage()
  154.     exit(1)
  155. end
  156. doStart()




실행> julia convertRadix.jl

  The supported maximum radix is 36.
  Command: (S)et radix, (A)bout, (Q)uit or E(x)it
  Prompt> s
  Input the source and target radices (say, 16 2): 10 8

    Convert Radix_10 to Radix_8
    SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
    Input Value>> 200
        ( 200 )_10   --->   ( 310 )_8

    Input Value>> main()

  Command: (S)et radix, (A)bout, (Q)uit or E(x)it
  Prompt> S
  Input the source and target radices (say, 16 2): 8 10

    Convert Radix_8 to Radix_10
    SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
    Input Value>> 2666
        ( 2666 )_8   --->   ( 1462 )_10

    Input Value>> main()

  Command: (S)et radix, (A)bout, (Q)uit or E(x)it
  Prompt> x



Posted by Scripter
,