C铉は人民元の大字変換例コードを実現します。



/// <summary>
///
/// </summary>
/// <param name="input"> </param>
/// <param name="prefix"> </param>
/// <exception cref="ArgumentException" />
/// <returns> </returns>
public static string ToCapital(this string input, bool prefix = false) {

    #region Step1

    if (!Regex.IsMatch(input, @"(?<=-|^)\d*\.?\d*$"))
        throw new ArgumentException(" !");

    if (Regex.IsMatch(input, @"^\d{25,}"))
        throw new ArgumentException(" !");

    #endregion

    #region Step2

    var positionDic = new Dictionary<int, string> {
        {0,"F"},{1,"J"},{2,"."},{3,"S"},{4,"B"},{5,"Q"},{6,"W"},{7,"SW"},{8,"BW"},{9,"QW"},
        {10,"Y"},{11,"SY"},{12,"BY"},{13,"QY"},{14,"WY"},{15,"SWY"},{16,"BWY"},{17,"QWY"},{18,"YY"},
        {19,"SYY"},{20,"BYY"},{21,"QYY"},{22,"WYY"},{23,"SWYY"},{24,"BWYY"},{25,"QWYY"}               
    };

    input = Regex.Replace(input, @"^\.", "0.");

    var integerPart = Regex.Replace(input, @"^-|\..*$", "");
    var _matchDecimal = Regex.Match(input, @"\.\d*$", RegexOptions.None);
    var decimalPart = Regex.Replace(_matchDecimal.Success ? Convert.ToDouble(_matchDecimal.Value).ToString("0.00") : "00", @"0\.", "");

    var processStack = new Stack<string>();
    var charsArray = (integerPart + decimalPart).Reverse<char>();
    for (int i = 0; i < charsArray.Count(); i++) {
        processStack.Push(string.Format("{0}{1}", charsArray.ElementAt(i), positionDic[i]));
    }

    // keleyi.com
    if (Regex.IsMatch(input, "^-", RegexOptions.None)) {
        processStack.Push("-");
    }
    if (prefix) {
        processStack.Push("¥");
    }

    var process = string.Empty;
    while (processStack.Count > 0) {
        process += processStack.Pop();
    }
    //
    Queue<Tuple<string, string, MatchEvaluator>> patterns = new Queue<Tuple<string, string, MatchEvaluator>>();
    var patternBuilder = new StringBuilder();
    for (int i = 3; i < positionDic.Count; i++) {
        patternBuilder.AppendFormat("{0}{1}", (i == 3 ? "(0(?:" : "") + positionDic[i], i == positionDic.Count - 1 ? ")+?)+" : "|");
    }
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(patternBuilder.ToString(), "0", null));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"(?:\d+(?:QW|BW|SW|W|Q|B|S)?\d?YY)+", null, m => m.Value.Replace("YY", "") + "YY"));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"(?:\d+(?:QW|BW|SW|W|Q|B|S)?\d?Y)+", null, m => m.Value.Replace("Y", "") + "Y"));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"(?:\d+(?:Q|B|S)?\d?W)+", null, m => m.Value.Replace("W", "") + "W"));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"(?!^)0+\.", ".", null));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"^0\.0J|^0\.", "", null));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>("0J|0F", "0", null));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>("J0?$", "JZ", null));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"\.$|\.0+$", ".Z", null));
    patterns.Enqueue(Tuple.Create<string, string, MatchEvaluator>(@"^0+$|^[^.]{0}$", "0.Z", null));

    while (patterns.Count > 0) {
        var pattern = patterns.Dequeue();
        if (pattern.Item3 != null) {
            process = Regex.Replace(process, pattern.Item1, pattern.Item3);
        }
        else {
            process = Regex.Replace(process, pattern.Item1, pattern.Item2);
        }
    }

    #endregion

    #region Step3
    StringBuilder result = new StringBuilder();
    var translatorDic = new Dictionary<char, string> {
        {'0'," "},{'1'," "},{'2'," "},{'3'," "},{'4'," "},{'5'," "},{'6'," "},{'7'," "},{'8'," "},{'9'," "},
        {'S'," "},{'B'," "},{'Q'," "},{'W',"f"},{'Y'," "},
        {'¥'," "},{'-'," "},{'.'," "},{'J'," "},{'F'," "},{'Z'," "}
    };
    for (int i = 0; i < process.Length; i++) {
        result.Append(translatorDic[process[i]]);
    }
    #endregion

    return result.ToString();
}