分享

Java试题之阿拉伯数字转换成中文数字-数据结构和算法-Tech-Java...

 仙人不留果 2009-12-01
之前见过一道题觉着挺有意思
今天没什么事就回想着做了一把 拿出来和大家一起看看
希望各位能给点意见 一起讨论下


题目大概:
将一组阿拉伯数字转换成中文大写数字
52306 ==> 伍万贰千叁百零陆

我实现了将文件中的一组数字(每行为一个数)
形如:

Java代码 复制代码
  1. 25364  
  2. 466932300  
  3. 12350006  
  4. 100000  
  5. 66699553001  

这样的文件中的数字全转换成中文大写数字


只是算法没有架构设计的成分
可以自定义一个输入文件既可运行
代码如下(源文件见附件):

Java代码 复制代码
  1. import java.io.BufferedReader;   
  2. import java.io.FileReader;   
  3.   
  4. public class Numeric2ChineseStr   
  5. {   
  6.        
  7.     public static void main(String[] args)   
  8.         throws Exception   
  9.     {   
  10.         String fileName = "c:\\input.txt";   
  11.            
  12.         // 单位数组   
  13.         String[] units = new String[] {"十""百""千""万""十""百""千""亿"};   
  14.            
  15.         // 中文大写数字数组   
  16.         String[] numeric = new String[] {"零""壹""贰""叁""肆""伍""陆""柒""捌""玖"};   
  17.            
  18.         // 读文件   
  19.         BufferedReader br = new BufferedReader(new FileReader(fileName));   
  20.         String temp = null;   
  21.         temp = br.readLine();   
  22.         String res = "";   
  23.            
  24.         while (null != temp)   
  25.         {   
  26.             // 遍历一行中所有数字   
  27.             for (int k = -1; temp.length() > 0; k++)   
  28.             {   
  29.                 // 解析最后一位   
  30.                 int j = Integer.parseInt(temp.substring(temp.length() - 1, temp.length()));   
  31.                 String rtemp = numeric[j];   
  32.                    
  33.                 // 数值不是0且不是个位 或者是万位或者是亿位 则去取单位   
  34.                 if (j != 0 && k != -1 || k % 8 == 3 || k % 8 == 7)   
  35.                 {   
  36.                     rtemp += units[k % 8];   
  37.                 }   
  38.                    
  39.                 // 拼在之前的前面   
  40.                 res = rtemp + res;   
  41.                    
  42.                 // 去除最后一位   
  43.                 temp = temp.substring(0, temp.length() - 1);   
  44.             }   
  45.                
  46.             // 去除后面连续的零零..   
  47.             while (res.endsWith(numeric[0]))   
  48.             {   
  49.                 res = res.substring(0, res.lastIndexOf(numeric[0]));   
  50.             }   
  51.                
  52.             // 将零零替换成零   
  53.             while (res.indexOf(numeric[0] + numeric[0]) != -1)   
  54.             {   
  55.                 res = res.replaceAll(numeric[0] + numeric[0], numeric[0]);   
  56.             }   
  57.                
  58.             // 将 零+某个单位 这样的窜替换成 该单位 去掉单位前面的零   
  59.             for (int m = 1; m < units.length; m++)   
  60.             {   
  61.                 res = res.replaceAll(numeric[0] + units[m], units[m]);   
  62.             }   
  63.                
  64.             // 这里打印一下 可以改成写文件   
  65.             System.out.println(res);   
  66.                
  67.             // 读取下一个数   
  68.             res = "";   
  69.             temp = br.readLine();   
  70.         }   
  71.     }   
  72. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多