Java试题之阿拉伯数字转换成中文数字-数据结构和算法-Tech-Java...
之前见过一道题觉着挺有意思
今天没什么事就回想着做了一把 拿出来和大家一起看看
希望各位能给点意见 一起讨论下 题目大概:
将一组阿拉伯数字转换成中文大写数字
52306 ==> 伍万贰千叁百零陆
我实现了将文件中的一组数字(每行为一个数)
形如: - 25364
- 466932300
- 12350006
- 100000
- 66699553001
253644669323001235000610000066699553001
这样的文件中的数字全转换成中文大写数字 只是算法没有架构设计的成分
可以自定义一个输入文件既可运行
代码如下(源文件见附件): - import java.io.BufferedReader;
- import java.io.FileReader;
-
- public class Numeric2ChineseStr
- {
-
- public static void main(String[] args)
- throws Exception
- {
- String fileName = "c:\\input.txt";
-
-
- String[] units = new String[] {"十", "百", "千", "万", "十", "百", "千", "亿"};
-
-
- String[] numeric = new String[] {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
-
-
- BufferedReader br = new BufferedReader(new FileReader(fileName));
- String temp = null;
- temp = br.readLine();
- String res = "";
-
- while (null != temp)
- {
-
- for (int k = -1; temp.length() > 0; k++)
- {
-
- int j = Integer.parseInt(temp.substring(temp.length() - 1, temp.length()));
- String rtemp = numeric[j];
-
-
- if (j != 0 && k != -1 || k % 8 == 3 || k % 8 == 7)
- {
- rtemp += units[k % 8];
- }
-
-
- res = rtemp + res;
-
-
- temp = temp.substring(0, temp.length() - 1);
- }
-
-
- while (res.endsWith(numeric[0]))
- {
- res = res.substring(0, res.lastIndexOf(numeric[0]));
- }
-
-
- while (res.indexOf(numeric[0] + numeric[0]) != -1)
- {
- res = res.replaceAll(numeric[0] + numeric[0], numeric[0]);
- }
-
-
- for (int m = 1; m < units.length; m++)
- {
- res = res.replaceAll(numeric[0] + units[m], units[m]);
- }
-
-
- System.out.println(res);
-
-
- res = "";
- temp = br.readLine();
- }
- }
- }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。