다음은  대화형 모드(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진법 까지이다.



  1. /*
  2.  *  Filename: convertRadix.groovy
  3.  *            Convert radix in a interactive mode.
  4.  *
  5.  *  Execute: groovy convertRadix.groovy
  6.  *
  7.  *      Date:  2008/03/25
  8.  *    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
  9.  */
  10. import java.io.*
  11. def printUsage() {
  12.     println("Usage: groovy convertRadix.groovy")
  13.     println("Convert radix in a interactive mode, where the maximum radix is 36.")
  14. }
  15. def printAbout() {
  16.     println("    About: Convert radix in a interactive mode.")
  17. }
  18. def printMainMenu() {
  19.     println("  Command: (S)et radix, (A)bout, (Q)uit or E(x)it")
  20. }
  21. def printMainPrompt() {
  22.     print("  Prompt> ")
  23. }
  24. def printSubMenu(int srcRadix, int destRadix) {
  25.     println("    Convert Radix_" + srcRadix + " to Radix_" + destRadix)
  26.     println("    SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit")
  27. }
  28. def printSubPrompt() {
  29.     print("    Input Value>> ")
  30. }
  31. String convertRadix(String s,  int srcRdx, int destRdx) {
  32.     long val
  33.     String ret = ""
  34.     try {
  35.         val = Integer.parseInt(s, srcRdx)
  36.         ret = Long.toString(val, destRdx)
  37.         return ret.toUpperCase()
  38.     }
  39.     catch (NumberFormatException nfx) {
  40.         println("    Error: " + nfx.getMessage() + " cantains some invalid character.")
  41.         ret = "????"
  42.     }
  43.     finally {
  44.       return ret.toUpperCase()
  45.     }
  46. }
  47. def doConvert(BufferedReader r, int srcRadix, int destRadix) {
  48.     def line
  49.     def cmd
  50.     def srcStr = "", destStr = ""
  51.     println()
  52.     printSubMenu(srcRadix, destRadix)
  53.     try {
  54.         while (true) {
  55.             printSubPrompt()
  56.             while ((cmd = r.readLine()) == null) {
  57.             }
  58.             if ("main()".equals(cmd)) {
  59.                 return
  60.             }
  61.             else if ("exit()".equals(cmd) || "quit()".equals(cmd)) {
  62.                 System.exit(0)
  63.             }
  64.             try {
  65.                 Integer.parseInt(cmd, srcRadix)
  66.                 srcStr = cmd
  67.                 destStr = convertRadix(srcStr, srcRadix, destRadix)
  68.                 println("        ( " + srcStr + " )_" + srcRadix +  "   --->   ( " + destStr + " )_" + destRadix)
  69.                 println()
  70.             }
  71.             catch (NumberFormatException nfx) {
  72.             }
  73.         }
  74.     }
  75.     catch (IOException ex) {
  76.         ex.printStackTrace()
  77.     }
  78. }
  79. def doStart() {
  80.     def line
  81.     def cmd
  82.     def srcRadix = 10, destRadix = 10
  83.     def srcStr = "", destStr = ""
  84.     boolean onlyOnce = true
  85.     BufferedReader r = new BufferedReader(new InputStreamReader(System.in))
  86.     try {
  87.         while (true) {
  88.             println()
  89.             if (onlyOnce) {
  90.                 println("  The supported maximum radix is 36.")
  91.                 onlyOnce = false
  92.             }
  93.             printMainMenu()
  94.             printMainPrompt()
  95.             while ((cmd = r.readLine()) == null) {
  96.             }
  97.             if ("qQxX".contains(cmd) && cmd.length() == 1) {
  98.                 System.exit(0)
  99.             }
  100.             else if ("aA".contains(cmd) && cmd.length() == 1) {
  101.                 printAbout()
  102.             }
  103.             else if ("sS".contains(cmd) && cmd.length() == 1) {
  104.                 print("  Input the source and target radices (say, 16 2): ")
  105.                 line = r.readLine()
  106.                 StringTokenizer st = new StringTokenizer(line, " ,\t")
  107.                 while (st.countTokens() < 2) {
  108.                     print("  Input the source and target radices (say, 16 2): ")
  109.                     line = r.readLine()
  110.                     st = new StringTokenizer(line, " ,\t");
  111.                 }
  112.                 srcRadix = Integer.parseInt(st.nextToken())
  113.                 destRadix = Integer.parseInt(st.nextToken())
  114.                 doConvert(r, srcRadix, destRadix)
  115.             }
  116.         }
  117.     }
  118.     catch (IOException ex) {
  119.         ex.printStackTrace()
  120.     }
  121. }
  122. // Begin here
  123. if (args.length > 0 && "-h".equals(args[0])) {
  124.     printUsage()
  125.     System.exit(1)
  126. }
  127. 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




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

Posted by Scripter
,