다음은  대화형 모드(interactive mode)에서 진법 변환(radix conversion)하는 C# 소스 코드이다.

메뉴는 주메뉴 Command: (S)et radix, (A)bout, (Q)uit or E(x)it
와 부메뉴 SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
로 구성되어 있으며, 진법 변환의 핵심은

         public static long ConvertAtoI(string s, int radix)
         public static string ConvertItoA(long num, int radix)

의 구현과 이용이다. 지원되는 진법은 2진법부터 36진법까지이다.
C#에는 Java의 StringTokener가 구현되어 있지 않다.
그래서 Java의 StringTokener와 비슷한 C#용 StringTokener.Token이라는
클래스를 구현해 보았다.


  1. /*
  2.  *  Filename: ConvertRadixApp.cs
  3.  *            Convert radix in a interactive mode.
  4.  *
  5.  *  Compile: csc ConvertRadixApp.cs
  6.  *  Execute: ConvertRadixApp
  7.  *
  8.  *      Date:  2009/01/19
  9.  *    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
  10.  */
  11. using System;
  12. namespace StringTokenizer {
  13.     class Token {
  14.         private string data,delimeter;
  15.         private string[] tokens;
  16.         private int index;
  17.         public Token(string strdata) {
  18.             Init(strdata, " ");
  19.         }
  20.         public Token(string strdata, string delim) {
  21.             Init(strdata, delim);
  22.         }
  23.         private void Init(string strdata, string delim) {
  24.             data = strdata;
  25.             delimeter = delim;
  26.             string data2 = "";
  27.             bool isDelim = false;
  28.             String c;
  29.             for (int i = 0; i < data.Length; i++) {
  30.                 c = data.Substring(i, 1);
  31.                 if (delim.Contains(c)) {
  32.                     if (!isDelim) {
  33.                         isDelim = true;
  34.                         data2 += c;
  35.                     }
  36.                     continue;
  37.                 }
  38.                 data2 += c;
  39.             }
  40.             tokens = data2.Split(delimeter.ToCharArray());
  41.             index = 0;
  42.         }
  43.         public int Count() {
  44.             return tokens.Length;
  45.         }
  46.         public bool HasMoreTokens() {
  47.             return (index < (tokens.Length));
  48.         }
  49.         public string NextToken() {
  50.             if (index < tokens.Length) {
  51.                 return tokens[index++];
  52.             }
  53.             else {
  54.                 return "";
  55.             }
  56.         }
  57.     }
  58. }
  59. namespace MyTestApplication1 {
  60.     public class ConvertRadixApp2 {
  61.         public static void PrintUsage() {
  62.             Console.WriteLine("Usage: java ConvertRadixApp");
  63.             Console.WriteLine("Convert radix in a interactive mode, where the maximum radix is 36.");
  64.         }
  65.         public void PrintAbout() {
  66.             Console.WriteLine("    About: Convert radix in a interactive mode.");
  67.         }
  68.         public void PrintMainMenu() {
  69.             Console.WriteLine("  Command: (S)et radix, (A)bout, (Q)uit or E(x)it");
  70.         }
  71.         public void PrintMainPrompt() {
  72.             Console.Write("  Prompt> ");
  73.         }
  74.         public void PrintSubMenu(int srcRadix, int destRadix) {
  75.             Console.WriteLine("    Convert Radix_" + srcRadix + " to Radix_" + destRadix);
  76.             Console.WriteLine("    SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit");
  77.         }
  78.         public void PrintSubPrompt() {
  79.             Console.Write("    Input Value>> ");
  80.         }
  81.         static string BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  82.         public static string ConvertItoA(long num, int radix) {
  83.             string tmp;
  84.             string ret;
  85.             string arr;
  86.             long q, r;
  87.             bool isNegative = false;
  88.             if (num < 0L) {
  89.                 isNegative = true;
  90.                 num = -num;
  91.             }
  92.             arr = "";
  93.             q = num;
  94.             r = 0L;
  95.             while (q >= (long) radix) {
  96.                 r = q % (long) radix;
  97.                 q = q / (long) radix;
  98.                 tmp = BASE36.Substring((int)r, 1);
  99.                 arr += tmp;
  100.             }
  101.             tmp = BASE36.Substring((int)q, 1);
  102.             arr += tmp;
  103.             if (isNegative) {
  104.                 arr += "-";
  105.              }
  106.              ret = "";
  107.              for (int j = 0; j < arr.Length; j++) {
  108.                ret += arr.Substring(arr.Length - j - 1, 1);
  109.              }
  110.              return ret;
  111.         }
  112.         public static long ConvertAtoI(string s, int radix) {
  113.             long ret = 0L;
  114.             bool isNegative = false;
  115.             int len =s.Length;
  116.             char c;
  117.             int i;
  118.             long val = 0L;
  119.             c = s[0];
  120.             if (c == '-') {
  121.                 isNegative = true;
  122.             }
  123.             else if (c >= '0' && c <= '9') {
  124.                 ret = (long) (c - '0');
  125.             }
  126.             else if (c >= 'A' && c <= 'Z') {
  127.                 ret = (long) (c - 'A') + 10L;
  128.             }
  129.             else if (c >= 'a' && c <= 'z') {
  130.                 ret = (long) (c - 'a') + 10L;
  131.             }
  132.             if (ret >= (long) radix) {
  133.                 Console.WriteLine("        Invalid character!");
  134.                 return ret;
  135.             }
  136.             for (i = 1; i < len; i++) {
  137.                 c = s[i];
  138.                 ret *= radix;
  139.                 if (c >= '0' && c <= '9') {
  140.                     val = (long) (c - '0');
  141.                 }
  142.                 else if (c >= 'A' && c <= 'Z') {
  143.                     val = (long) (c - 'A') + 10L;
  144.                 }
  145.                 else if (c >= 'a' && c <= 'z') {
  146.                     val = (long) (c - 'a') + 10L;
  147.                 }
  148.                 if (val >= (long) radix) {
  149.                     Console.WriteLine("        Invalid character!");
  150.                     return ret;
  151.                 }
  152.                 ret += val;
  153.             }
  154.             if (isNegative)
  155.                 ret = -ret;
  156.             return ret;
  157.         }
  158.         public string ConvertRadix(String s, int srcRdx, int destRdx) {
  159.             long val;
  160.             string ret = "";
  161.             try {
  162.                 val = ConvertAtoI(s, srcRdx);
  163.                 ret = ConvertItoA(val, destRdx);
  164.                 return ret.ToUpper();
  165.             }
  166.             catch (Exception nfx) { ]
  167.                 Console.WriteLine("    Error: " + nfx + " cantains some invalid character.");
  168.                 ret = "????";
  169.                 return ret.ToUpper();
  170.             }
  171.         }
  172.         public void DoConvert(int srcRadix, int destRadix) {
  173.             string cmd;
  174.             string srcStr = "", destStr = "";
  175.             Console.WriteLine();
  176.             PrintSubMenu(srcRadix, destRadix);
  177.             try {
  178.                 do {
  179.                     PrintSubPrompt();
  180.                     while ((cmd = Console.ReadLine()) == null) {
  181.                     }
  182.                     if ("main()".Equals(cmd)) {
  183.                         return;
  184.                     }
  185.                     else if ("exit()".Equals(cmd) || "quit()".Equals(cmd)) {
  186.                         Environment.Exit(0);
  187.                     }
  188.                     try {
  189.                         Convert.ToInt32(cmd, srcRadix);
  190.                         srcStr = cmd;
  191.                         destStr = ConvertRadix(srcStr, srcRadix, destRadix);
  192.                         Console.WriteLine("        ( " + srcStr + " )_" + srcRadix +  "   --->   ( " + destStr + " )_" + destRadix);
  193.                         Console.WriteLine();
  194.                     }
  195.                     catch (FormatException ex) {
  196.                          Console.WriteLine(cmd + ": " + ex);
  197.                     }
  198.                 } while (true);
  199.             }
  200.             catch (Exception ex) {
  201.                 Console.WriteLine("" + ex);
  202.             }
  203.         }
  204.         public void DoStart() {
  205.          char[] seps = { ' ', ',', '\t' };
  206.             string line;
  207.             string cmd;
  208.             int srcRadix = 10, destRadix = 10;
  209.             bool onlyOnce = true;
  210.             try {
  211.                 do {
  212.                     Console.WriteLine();
  213.                     if (onlyOnce) {
  214.                         Console.WriteLine("  The supported maximum radix is 36.");
  215.                         onlyOnce = false;
  216.                     }
  217.                     PrintMainMenu();
  218.                     PrintMainPrompt();
  219.                     while ((cmd = Console.ReadLine()) == null) {
  220.                     }
  221.                     if ("qQxX".Contains(cmd) && cmd.Length == 1) {
  222.                         Environment.Exit(0);
  223.                     }
  224.                     else if ("aA".Contains(cmd) && cmd.Length == 1) {
  225.                         PrintAbout();
  226.                     }
  227.                     else if ("sS".Contains(cmd) && cmd.Length == 1) {
  228.                         Console.Write("  Input the source and target radices (say, 16 2): ");
  229.                         line = Console.ReadLine();
  230.                         StringTokenizer.Token tok = new StringTokenizer.Token(line, " , \t");
  231.                         while (tok.Count() != 2) {
  232.                             Console.Write("  Input the source and target radices (say, 16 2): ");
  233.                             line = Console.ReadLine();
  234.                             tok = new StringTokenizer.Token(line, " , \t");
  235.                         }
  236.                         try {
  237.                             srcRadix = Convert.ToInt32(tok.NextToken());
  238.                             destRadix = Convert.ToInt32(tok.NextToken());
  239.                             DoConvert(srcRadix, destRadix);
  240.                         }
  241.                         catch (FormatException ex) {
  242.                             Console.WriteLine("" + ex);
  243.                         }
  244.                     }
  245.                 } while (true);
  246.             }
  247.             catch (Exception ex) {
  248.                 Console.WriteLine("" + ex);
  249.             }
  250.         }
  251.         // Java 언어의 main 메소드에 해당하는 C# 언어의 Main 메소드
  252.         public static void Main(string[] args) {
  253.             if (args.Length > 0 && "-h".Equals(args[0])) {
  254.                 PrintUsage();
  255.                 Environment.Exit(1);
  256.             }
  257.             ConvertRadixApp2 app = new ConvertRadixApp2();
  258.             app.DoStart();
  259.         }
  260.     }
  261. }



컴파일> csc ConvertRadixApp.cs

실행> ConvertRadixApp

  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 16

    Convert Radix_10 to Radix_16
    SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
    Input Value>> 256
        ( 256 )_10   --->   ( 100 )_16

    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): 16 10

    Convert Radix_16 to Radix_10
    SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit
    Input Value>> FFFF
        ( FFFF )_16   --->   ( 65535 )_10

    Input Value>> exit()



 

Posted by Scripter
,