다음은 대화형 모드(interactive mode)에서 진법 변환(radix conversion)하는 Groovy 소스 코드이다.
메뉴는 주메뉴 Command: (S)et radix, (A)bout, (Q)uit or E(x)it
와 부메뉴 SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
로 구성되어 있으며, 진법 변환의 핵심은 Integer 클래스와 Long 클래스의 정적 메소드
Integer.parseInt(String, int);
Long.toString(long, int);
울 이용하였으며, 지원되는 진법은 2진법에서 36진법 까지이다.
- /*
- * Filename: convertRadix.groovy
- * Convert radix in a interactive mode.
- *
- * Execute: groovy convertRadix.groovy
- *
- * Date: 2008/03/25
- * Author: PH Kim [ pkim (AT) scripts.pe.kr ]
- */
- import java.io.*
- def printUsage() {
- println("Usage: groovy convertRadix.groovy")
- println("Convert radix in a interactive mode, where the maximum radix is 36.")
- }
- def printAbout() {
- println(" About: Convert radix in a interactive mode.")
- }
- def printMainMenu() {
- println(" Command: (S)et radix, (A)bout, (Q)uit or E(x)it")
- }
- def printMainPrompt() {
- print(" Prompt> ")
- }
- def printSubMenu(int srcRadix, int destRadix) {
- println(" Convert Radix_" + srcRadix + " to Radix_" + destRadix)
- println(" SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit")
- }
- def printSubPrompt() {
- print(" Input Value>> ")
- }
- String convertRadix(String s, int srcRdx, int destRdx) {
- long val
- String ret = ""
- try {
- val = Integer.parseInt(s, srcRdx)
- ret = Long.toString(val, destRdx)
- return ret.toUpperCase()
- }
- catch (NumberFormatException nfx) {
- println(" Error: " + nfx.getMessage() + " cantains some invalid character.")
- ret = "????"
- }
- finally {
- return ret.toUpperCase()
- }
- }
- def doConvert(BufferedReader r, int srcRadix, int destRadix) {
- def line
- def cmd
- def srcStr = "", destStr = ""
- println()
- printSubMenu(srcRadix, destRadix)
- try {
- while (true) {
- printSubPrompt()
- while ((cmd = r.readLine()) == null) {
- }
- if ("main()".equals(cmd)) {
- return
- }
- else if ("exit()".equals(cmd) || "quit()".equals(cmd)) {
- System.exit(0)
- }
- try {
- Integer.parseInt(cmd, srcRadix)
- srcStr = cmd
- destStr = convertRadix(srcStr, srcRadix, destRadix)
- println(" ( " + srcStr + " )_" + srcRadix + " ---> ( " + destStr + " )_" + destRadix)
- println()
- }
- catch (NumberFormatException nfx) {
- }
- }
- }
- catch (IOException ex) {
- ex.printStackTrace()
- }
- }
- def doStart() {
- def line
- def cmd
- def srcRadix = 10, destRadix = 10
- def srcStr = "", destStr = ""
- boolean onlyOnce = true
- BufferedReader r = new BufferedReader(new InputStreamReader(System.in))
- try {
- while (true) {
- println()
- if (onlyOnce) {
- println(" The supported maximum radix is 36.")
- onlyOnce = false
- }
- printMainMenu()
- printMainPrompt()
- while ((cmd = r.readLine()) == null) {
- }
- if ("qQxX".contains(cmd) && cmd.length() == 1) {
- System.exit(0)
- }
- else if ("aA".contains(cmd) && cmd.length() == 1) {
- printAbout()
- }
- else if ("sS".contains(cmd) && cmd.length() == 1) {
- print(" Input the source and target radices (say, 16 2): ")
- line = r.readLine()
- StringTokenizer st = new StringTokenizer(line, " ,\t")
- while (st.countTokens() < 2) {
- print(" Input the source and target radices (say, 16 2): ")
- line = r.readLine()
- st = new StringTokenizer(line, " ,\t");
- }
- srcRadix = Integer.parseInt(st.nextToken())
- destRadix = Integer.parseInt(st.nextToken())
- doConvert(r, srcRadix, destRadix)
- }
- }
- }
- catch (IOException ex) {
- ex.printStackTrace()
- }
- }
- // Begin here
- if (args.length > 0 && "-h".equals(args[0])) {
- printUsage()
- System.exit(1)
- }
- doStart()
실행> groovy convertRadix.groovy
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>> 1FF
( 1FF )_16 ---> ( 111111111 )_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 8
Convert Radix_2 to Radix_8
SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
Input Value>> 1011001
( 1011001 )_2 ---> ( 131 )_8
Input Value>> main()
Command: (S)et radix, (A)bout, (Q)uit or E(x)it
Prompt> x
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
'프로그래밍 > Groovy' 카테고리의 다른 글
7비트 ASCII 코드표 만들기 예제 with Groovy (0) | 2008.03.31 |
---|---|
진법(radix) 표 만들기 예제 with Groovy (0) | 2008.03.29 |
황금비율(golden ratio) 구하기 with Groovy (0) | 2008.03.24 |
현재 시각 알아내기 for Groovy (0) | 2008.03.24 |
조립제법(Horner의 방법) 예제 (2) for Groovy (0) | 2008.03.14 |