다음은 대화형 모드(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진법까지이다.
- ## Filename: convertRadix.jl
- ## Convert radix in a interactive mode.
- ##
- ## Execute: julia convertRadix.jl
- ##
- ## Date: 2013. 3. 5.
- ## Author: pkim __AT__ scripts ((DOT)) pe ((DOT)) kr
- # The function println() is defined already in Julia language.
- # function println(s...)
- # if length(s) > 0
- # println(s[1])
- # else
- # println("")
- # end
- function printUsage()
- println("Usage: julia convertRadix.jl")
- println("Convert radix in a interactive mode, where the maximum radix is 36.")
- end
- function printAbout()
- println(" About: Convert radix in a interactive mode.")
- end
- function printMainMenu()
- println(" Command: (S)et radix, (A)bout, (Q)uit or E(x)it")
- end
- function printMainPrompt()
- print(" Prompt> ")
- end
- function printSubMenu(srcRadix, destRadix)
- println(" Convert Radix_" * string(srcRadix) * " to Radix_" * string(destRadix))
- println(" SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit")
- end
- function printSubPrompt()
- print(" Input Value>> ")
- end
- BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- function itoa(num, radix)
- isNegative = false
- if num < 0
- isNegative = true
- num = -num
- end
- arr = ""
- q, r = num, 0
- while q >= radix
- # q, r = divmod(q, radix)
- r = mod(q, radix)
- q = div(q, radix)
- arr *= BASE36[r+1]
- end
- arr *= BASE36[q + 1]
- if isNegative
- arr *= "-"
- end
- return reverse(arr)
- end
- function convertRadix(s, srcRdx, destRdx)
- ret = ""
- try
- val = parse_int(s, srcRdx)
- ret = itoa(val, destRdx)
- return uppercase(ret)
- catch x
- println(x)
- println(" Error: Cannot convert radix " * string(srcRdx))
- return uppercase(ret)
- end
- end
- function doConvert(srcRadix, destRadix)
- line = ""
- cmd = ""
- srcStr = ""
- destStr = ""
- println("")
- printSubMenu(srcRadix, destRadix)
- try
- while true
- printSubPrompt()
- cmd = ""
- while length(cmd) == 0
- cmd = readline(STDIN)
- cmd = strip(cmd)
- end
- if "main()" == cmd
- return
- elseif "exit()" == cmd || "quit()" == cmd
- exit(0)
- end
- try
- parse_int(cmd, srcRadix)
- srcStr = cmd
- destStr = convertRadix(srcStr, srcRadix, destRadix)
- println(" ( " * srcStr * " )_" * string(srcRadix) * " ---> ( " * destStr * " )_" * string(destRadix))
- println("")
- catch x
- print ("") # pass
- end
- end
- catch x
- println(x)
- end
- end
- function doStart()
- line = ""
- cmd = ""
- srcRadix = 10
- destRadix = 10
- srcStr = ""
- destStr = ""
- onlyOnce = true
- try
- while true
- println()
- if onlyOnce
- println(" The supported maximum radix is 36.")
- onlyOnce = false
- end
- printMainMenu()
- printMainPrompt()
- cmd = ""
- while length(cmd) == 0
- cmd = readline(STDIN)
- cmd = strip(cmd)
- end
- if length(search("qQxX", cmd)) >= 1 && length(cmd) == 1
- exit(0)
- elseif length(search("aA", cmd)) >= 1 && length(cmd) == 1
- printAbout()
- elseif length(search("sS", cmd)) >= 1 && length(cmd) == 1
- print(" Input the source and target radices (say, 16 2): ")
- line = readline(STDIN)
- st = split(line, " ")
- while length(st) < 2
- print(" Input the source and target radices (say, 16 2): ")
- line = readline(STDIN)
- st = split(linem, " ")
- end
- try
- srcRadix = parse_int(st[1])
- destRadix = parse_int(st[2])
- doConvert(srcRadix, destRadix)
- catch ex
- println(ex)
- println("Try again!")
- end
- end
- end
- catch x
- println(x)
- end
- end
- # Begin here
- if length(ARGS) > 0 && "-h" == ARGS[1]
- printUsage()
- exit(1)
- end
- 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
'프로그래밍 > Julia' 카테고리의 다른 글
7비트 ASCII 코드표 만들기 예제 with Julia (0) | 2013.03.05 |
---|---|
진법(radix) 표 만들기 예제 with Julia (0) | 2013.03.05 |
황금비율(golden ratio) 구하기 with Julia (0) | 2013.03.05 |
조립제법(Horner의 방법) 예제 for Julia (0) | 2013.03.03 |
80컬럼 컨솔에 19단표 출력하기 예제 for Julia (0) | 2013.03.03 |