Java試題之阿拉伯?dāng)?shù)字轉(zhuǎn)換成中文數(shù)字-數(shù)據(jù)結(jié)構(gòu)和算法-Tech-Java...
之前見過一道題覺著挺有意思
今天沒什么事就回想著做了一把 拿出來和大家一起看看
希望各位能給點(diǎn)意見 一起討論下 題目大概:
將一組阿拉伯?dāng)?shù)字轉(zhuǎn)換成中文大寫數(shù)字
52306 ==> 伍萬(wàn)貳千叁百零陸
我實(shí)現(xiàn)了將文件中的一組數(shù)字(每行為一個(gè)數(shù))
形如: - 25364
- 466932300
- 12350006
- 100000
- 66699553001
253644669323001235000610000066699553001
這樣的文件中的數(shù)字全轉(zhuǎn)換成中文大寫數(shù)字 只是算法沒有架構(gòu)設(shè)計(jì)的成分
可以自定義一個(gè)輸入文件既可運(yùn)行
代碼如下(源文件見附件): - 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[] {"十", "百", "千", "萬(wàn)", "十", "百", "千", "億"};
-
-
- 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();
- }
- }
- }
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。