다음은 대화형 모드(interactive mode)에서 진법 변환(radix conversion)하는 Lua 소스 코드이다.
메뉴는 주메뉴 Command: (S)et radix, (A)bout, (Q)uit or E(x)it
와 부메뉴 SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
로 구성되어 있으며, 진법 변환의 핵심은 Lua 의 빌트인 함수 tonumber(string, radix)와 소스 코드에 자체 작성된 함수 itoa(number, radix)의 사용이다.
val = tonumber(s, srcRdx)
ret = itoa(val, destRdx)
지원되는 진법은 2진법에서 36진법 까지이다.
- -- Filename: convertRadix.lua
- -- Convert radix in a interactive mode.
- --
- -- Execute: lua convertRadix.lua
- --
- -- Date: 2008/03/26
- -- Author: PH Kim [ pkim (AT) scripts.pe.kr ]
- function println(s)
- if s ~= nil then
- print(s)
- else
- print("")
- end
- end
- function printUsage()
- println("Usage: lua convertRadix.lua")
- 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()
- io.write(" Prompt> ")
- end
- function printSubMenu(srcRadix, destRadix)
- println(" Convert Radix_" .. srcRadix .. " to Radix_" .. destRadix)
- println(" SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit")
- end
- function printSubPrompt()
- io.write(" Input Value>> ")
- end
- local BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- function itoa(num, radix)
- local isNegative = false
- if num < 0 then
- isNegative = true
- num = -num
- end
- local arr = {}
- local q, r = num, 0
- while q >= radix do
- r = math.fmod(q, radix)
- q = math.floor(q / radix)
- table.insert(arr, string.sub(BASE36, r+1, r+1))
- end
- table.insert(arr, string.sub(BASE36, q+1, q+1))
- if isNegative then
- table.insert(arr, "-")
- end
- local ret = ""
- for i = #arr, 1, -1 do
- ret = ret .. arr[i]
- end
- return ret
- end
- function convertRadix(s, srcRdx, destRdx)
- local val = tonumber(s, srcRdx)
- local ret = itoa(val, destRdx)
- return string.upper(ret)
- end
- function doConvert(srcRadix, destRadix)
- local line = ""
- local cmd = ""
- local srcStr = ""
- local destStr = ""
- printSubMenu(srcRadix, destRadix)
- while true do
- printSubPrompt()
- cmd = io.read()
- while string.len(cmd) < 1 do
- cmd = io.read()
- end
- if "main()" == cmd then
- return
- elseif "exit()" == cmd or "quit()" == cmd then
- os.exit(0)
- end
- local flagContinue = true
- if tonumber(cmd, srcRadix) == nil then
- flagContinue = false
- end
- if flagContinue then
- srcStr = cmd
- destStr = convertRadix(srcStr, srcRadix, destRadix)
- println(" ( " .. srcStr .. " )_" .. srcRadix .. " ---> ( " .. destStr .. " )_" .. destRadix)
- println("")
- end
- end
- end
- function doStart()
- local line = ""
- local cmd = ""
- local srcRadix = 10
- local destRadix = 10
- local srcStr = ""
- local destStr = ""
- local onlyOnce = true
- while true do
- println()
- if onlyOnce then
- println(" The supported maximum radix is 36.")
- onlyOnce = false
- end
- printMainMenu()
- printMainPrompt()
- cmd = io.read()
- while string.len(cmd) < 1 do
- cmd = io.read()
- end
- if string.find("qQxX", cmd) ~= nil and string.len(cmd) == 1 then
- os.exit(0)
- elseif string.find("aA", cmd) ~= nil and string.len(cmd) == 1 then
- printAbout()
- elseif string.find("sS", cmd) ~= nil and string.len(cmd) == 1 then
- io.write(" Input the source and target radices (say, 16 2): ")
- line = io.read()
- while string.len(line) < 1 do
- line = io.read()
- end
- st = {}
- for w in string.gmatch(line, "[0-9]*") do
- if string.len(w) > 0 then
- table.insert(st, w)
- end
- end
- while #st < 2 do
- io.write(" Input the source and target radices (say, 16 2): ")
- line = io.read()
- while string.len(line) < 1 do
- line = io.read()
- end
- st = {}
- for w in string.gmatch(line, "%a+") do
- if string.len(w) > 0 then
- table.insert(st, w)
- end
- end
- end
- println("")
- srcRadix = tonumber(st[1])
- destRadix = tonumber(st[2])
- doConvert(srcRadix, destRadix)
- end
- end
- end
- -- Begin here
- if #arg > 0 and "-h" == arg[1] then
- printUsage()
- os.exit(1)
- end
- doStart()
실행> lua convertRadix.lua
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): 16 2
Convert Radix_16 to Radix_2
SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
Input Value>> 7F
( 7F )_16 ---> ( 1111111 )_2
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): 2 16
Convert Radix_2 to Radix_16
SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
Input Value>> 1111111
( 1111111 )_2 ---> ( 7F )_16
Input Value>> exit()
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
'프로그래밍 > Lua' 카테고리의 다른 글
7비트 ASCII 코드표 만들기 예제 with Lua (0) | 2008.03.31 |
---|---|
진법(radix) 표 만들기 예제 with Lua (0) | 2008.03.29 |
황금비율(golden ratio) 구하기 with Lua (0) | 2008.03.24 |
현재 시각 알아내기 for Lua (0) | 2008.03.24 |
조립제법(Horner의 방법) 예제 for Lua (0) | 2008.03.14 |