误入C#村的『Java Programmer』经验分享--LinQ

经典题目: 输入任数字介于 1~999999999 间,并将其转成中文样式

ex: 100 => 壹佰    999999999 => 玖亿玖仟玖佰玖拾玖万玖仟玖佰玖拾玖

先说明我的解题思路http://img2.58codes.com/2024/emoticon07.gif
Step 1: Function (int val)读到的 『整数值』转成字串

public static string GetAnswer(int val) {   if (val < 1 || val > 999999999)        return "输入数字超出範围 1 ~ 999,999,999";   else {        string val2str = val.ToString();        string val2ch = val2str                        .Replace("0", "零").Replace("1", "壹")                        .Replace("2", "贰").Replace("3", "参")                        .Replace("4", "肆").Replace("5", "伍")                        .Replace("6", "陆").Replace("7", "柒")                        .Replace("8", "捌").Replace("9", "玖");        return val2ch;                          }}   

现在已经可以取得任何数字的中文转换http://img2.58codes.com/2024/emoticon12.gif

ex: GetAnswer(int 60058905) => 陆零零伍捌玖零伍

Step 2: 需要一个方法依据字串长度塞入对应的『拾』、『佰』、 ... 、『亿』

static string ConvertLen2Dec(string str) {  switch (str.Length) {    case 2: return str.Substring(0, 1) + "拾" + str.Substring(1);     case 3: return str.Substring(0, 1) + "佰" + str.Substring(1, 1)                    + "拾" + str.Substring(2);    case 4: return str.Substring(0, 1) + "仟" + str.Substring(1, 1)                    + "佰" + str.Substring(2, 1)                    + "拾" + str.Substring(3);    case 5: return str.Substring(0, 1) + "万" + str.Substring(1, 1)                    + "仟" + str.Substring(2, 1)                    + "佰" + str.Substring(3, 1)                   + "拾" + str.Substring(4);    case 6: return str.Substring(0, 1) + "拾万" + str.Substring(1, 1)                    + "万" + str.Substring(2, 1)                    + "仟" + str.Substring(3, 1)                    + "佰" + str.Substring(4, 1)                    + "拾" + str.Substring(5);    case 7: return str.Substring(0, 1) + "佰万" + str.Substring(1, 1)                    + "拾万" + str.Substring(2, 1)                    + "万" + str.Substring(3, 1)                    + "仟" + str.Substring(4, 1)                    + "佰" + str.Substring(5, 1)                    + "拾" + str.Substring(6);    case 8: return str.Substring(0, 1) + "仟万" + str.Substring(1, 1)                   + "佰万" + str.Substring(2, 1)                   + "拾万" + str.Substring(3, 1)                    + "万" + str.Substring(4, 1)                    + "仟" + str.Substring(5, 1)                    + "佰" + str.Substring(6, 1)                    + "拾" + str.Substring(7);    case 9: return str.Substring(0, 1) + "亿" + str.Substring(1, 1)                    + "仟万" + str.Substring(2, 1)                    + "佰万" + str.Substring(3, 1)                    + "拾万" + str.Substring(4, 1)                    + "万" + str.Substring(5, 1)                    + "仟" + str.Substring(6, 1)                    + "佰" + str.Substring(7, 1)                    + "拾" + str.Substring(8);    default: return "数字大于 999,999,999";  }}

不过依照第一步的例子输入,会得到

ex: GetAnswer(int 60058905) => 陆仟万零佰万零拾万伍万捌仟玖佰零拾伍

恼人的『零』会造成 『零佰万』、『零拾万』、...、『零拾』这些结果http://img2.58codes.com/2024/emoticon04.gif
Step 3: 需要一个方法来处理『零』的问题

static string DelZero(string num, string ch) {   return num.Equals("零") ? num : num+ch;}// 修正后的 ConvertLen2Dec()static string ConvertLen2Dec(string str) {  switch (str.Length) {    case 2: return str.Substring(0, 1) + "拾" + str.Substring(1);     case 3: return str.Substring(0, 1) + "佰" +                    DelZero(str.Substring(1, 1), "拾") +                   str.Substring(2);    case 4: return str.Substring(0, 1) + "仟" +                    DelZero(str.Substring(1, 1), "佰") +                   DelZero(str.Substring(2, 1), "拾") +                   str.Substring(3);    case 5: return str.Substring(0, 1) + "万" +                    DelZero(str.Substring(1, 1), "仟") +                   DelZero(str.Substring(2, 1), "佰") +                    DelZero(str.Substring(3, 1), "拾") +                    str.Substring(4);    case 6: return str.Substring(0, 1) + "拾万" +                    DelZero(str.Substring(1, 1), "万") +                   DelZero(str.Substring(2, 1), "仟") +                    DelZero(str.Substring(3, 1), "佰") +                   DelZero(str.Substring(4, 1), "拾") +                    str.Substring(5);    case 7: return str.Substring(0, 1) + "佰万" +                    DelZero(str.Substring(1, 1), "拾万") +                   DelZero(str.Substring(2, 1), "万") +                    DelZero(str.Substring(3, 1), "仟") +                   DelZero(str.Substring(4, 1), "佰") +                    DelZero(str.Substring(5, 1), "拾") +                    str.Substring(6);    case 8: return str.Substring(0, 1) + "仟万" +                    DelZero(str.Substring(1, 1), "佰万") +                   DelZero(str.Substring(2, 1), "拾万") +                    DelZero(str.Substring(3, 1), "万") +                   DelZero(str.Substring(4, 1), "仟") +                    DelZero(str.Substring(5, 1), "佰") +                   DelZero(str.Substring(6, 1), "拾") +                    str.Substring(7);    case 9: return str.Substring(0, 1) + "亿" +                    DelZero(str.Substring(1, 1), "仟万") +                   DelZero(str.Substring(2, 1), "佰万") +                    DelZero(str.Substring(3, 1), "拾万") +                   DelZero(str.Substring(4, 1), "万") +                    DelZero(str.Substring(5, 1), "仟") +                   DelZero(str.Substring(6, 1), "佰") +                   DelZero(str.Substring(7, 1), "拾") +                    str.Substring(8);     default: return "数字大于 999,999,999";  }}

同样输入 60058905 经过第三步后会得到

ex: GetAnswer(int 60058905) => 陆仟万零零伍万捌仟玖佰零伍

Step 4: 对于『零零零...』的问题,只要再对字串做

ConvertLen2Dec(val2ch).Replace("零零零零零零零", "")                      .Replace("零零零零零零", "零")                      ...;

类似的处理就能处理掉http://img2.58codes.com/2024/emoticon01.gif
所以完成 1~4 步后,输入 60058905 会得到

ex: GetAnswer(int 60058905) => 陆仟万零伍万捌仟玖佰零伍

以下开始进入本篇主题
破万时,如何解决『万』重複的问题呢 ?http://img2.58codes.com/2024/emoticon19.gif
ex: 陆仟万零伍万

分析问题:999990000 => 玖亿玖仟万玖佰万玖拾万玖万 : 4个『万』正确值: 玖亿玖仟玖佰玖拾玖万         => 『仟万』变『仟』,『佰万』变『佰』,『拾万』变『拾』999900000 => 玖亿玖仟万玖佰万玖拾万 : 3个『万』正确值: 玖亿玖仟玖佰玖拾玖万          => 『仟万』变『仟』,『佰万』变『佰』999000000 => 玖亿玖仟万玖佰万 : 2个『万』正确值: 玖亿玖仟玖佰万 => 『仟万』变『仟』990000000 => 玖亿玖仟万 (正确值)归纳结果:当『万』字有两个以上时,就必须滤掉至 1 个,才能得到正确值

从归纳结果设计Step 5http://img2.58codes.com/2024/emoticon34.gif
Step 5: 计数字串中『万』的个数,如果个数超过 1 就将字串做『Replace("仟万", "仟")...』处理

static string DelMuti10Thousand(string str) {   /*** 我们的 LinQ 在这里 ***/   var query = from c in str.ToCharArray() where c == '万' select c;   /************************/      if (query.Count() > 1)      return str.Replace("仟万", "仟")                .Replace("佰万", "佰")                .Replace("拾万", "拾");   else      return str;}

Language-Integrated Query(LinQ)有点类似 SparkSQL 提供我们使用一些熟悉的SQL语法来对
collection, List, (k, v)List, ...进行一些排序、撷取、分类、...操作。
只不过之前用 java 写的 SparkSQL 应用必须在 Spark 环境下才能运作

var query = from c in str.ToCharArray() where c == '万' select c;这段是将 转换成『字元阵列』中的『万』字元捞出来变成一个新的集合

只要知道把捞出来的『万』字元阵列做 Count() 计次,2次以上就
替换『仟万』=> 『仟』,『佰万』=> 『佰』,『拾万』=> 『拾』
这题就解决了!

结论:
本篇的精神只是要分享 LinQ 的应用,并不是说一定要跟资料库结合才能用。
wiki的说明:『...是微软的一项技术,新增一种自然查询的SQL语法到.NET Framework的程式语言中...』
反而会让一些入门者误会成只有在处理资料库的情境下才会用到 LinQ。
本篇中举例用 LinQ 来分析字串,并没有与任何资料库连结。
http://img2.58codes.com/2024/emoticon41.gif


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章