原創文章如轉載請註明:轉自¥忘%風 {http://www.cnblogs.com/slave_wc}
本文地址: 我的第一個C#程式,中文與整數互譯
測試情況:
1:輸入異常判斷:
2:簡單資料測試
代碼說明概要:
數字轉中文基本思路:
即進行分解
1:計算最基本的[0,9999]的數字翻譯
2:通過 '萬' 字串連兩個相鄰的由(1)式計算所得的結果,得到[0,99999999]範圍的翻譯。
3:通過 '億' 字串連,得到大數範圍內的翻譯。
中文轉數字基本思路:
主要問題:1:在哪裡進行乘法?2:乘法的作用範圍呢?
容易進入的誤區:
1:只考慮在哪裡進行乘法,如一千萬
(容易想到如,當計算到'萬'字時,離它最近的單位為'千',這種情況下進行乘法,而除乘法外,則進行順序計算)。
**測試資料:一億三千萬。由該演算法得到運算式為:1e8 + 3000 * 10000。由於是順序計算,實際計算時為 (1e8 + 3000) * 10000
得到了錯誤答案。
2:考慮到了(1)中遇到的問題,但對乘法作用範圍判斷出錯。如資料一億三千萬,正確運算式為1e8 + (3000 * 10000)
誤以為進行類似只有 '+' 與 '*' 的算式運算式的計算即可,即考慮了 '*' 與 '+' 的優先順序。
如此如“一億三千萬”能得到正確答案了。
**測試資料:一億零一百二十萬。按(2)的演算法,則為:1e 8 + 100 + (20 * 10000)。
而實際結果應為:1e8 + (100 + 20) * 10000,得到了錯誤答案。
總結出的解法:即考慮1:在哪裡進行乘法?2:乘法的作用範圍呢?
**如一億零一百二十萬。當遇到‘萬’時,由前面的‘百’<‘萬’決定了當前需要進行乘法。
**再由‘萬’之前遇到的第一個 > ‘萬’的單位(此處為億),決定了 乘法的作用範圍在兩者之間。
得到該翻譯的正確運算式:1e8 + (100 + 20) * 10000。
路過看到有異議歡迎來拍磚。
代碼簡要架構:
private class num2Chinese{private static readonly string unit = "個十百千";private static readonly string ala = "0123456789";private static readonly string ch = "零一二三四五六七八九";private static string transSmall(string num){/*[0,一萬)區間的翻譯*/}private static string trans(string num, int range, char ss){/*遞迴求解。[0,千]通過'萬'字可拼接成[0,千萬],同理[0,千萬]通過億字串連。參數含義:將num字串按每range作為一組進行分解,並通過ss字元進行串連。(4位一組或八位一組)*/}public static string transBigInteger(string ss){/*調用trans進行翻譯與異常處理,支援大數*/}}private class ChineseNumConvert{/*整數轉數字類,不支援大數 關鍵點:在哪裡進行乘法?計算乘法的範圍?*/private static readonly string Digit = "零一二三四五六七八九";private static readonly string Unit = "個十百千萬億";private static readonly int[] num = { 1, 10, 100, 1000, 10000, 100000000 };private static bool checkString(string word){/*簡單判斷輸入是否合法 1;其他字元。 2:有非零數字相鄰*/}public static long ChineseNum2Int(string word){} }
代碼及必要注釋如下:
private class num2Chinese { private static readonly string unit = "個十百千"; private static readonly string ala = "0123456789"; private static readonly string ch = "零一二三四五六七八九"; private static string transSmall(string num) {/*[0,一萬)區間的翻譯*/ string str = ""; string nn = num.TrimStart('0'); bool flag = false; int j = 0; for (int i = nn.Length - 1; i >= 0; i--, j++) { int no = ala.IndexOf(nn[i]); if (no != 0) flag = true; if (flag) { if (j != 0 && no != 0) str += unit[j]; if (!(no == 0 && str[str.Length - 1] == '零')) str += ch[no]; } } char[] s = str.ToCharArray(); Array.Reverse(s); str = new string(s); return str; } private static string trans(string num, int range, char ss) {/*遞迴求解。[0,千]通過'萬'字可拼接成[0,千萬],同理[0,千萬]通過億字串連。參數含義:將num字串按每range作為一組進行分解,並通過ss字元進行串連。(4位一組或八位一組)*/ string ret = ""; string input = num.TrimStart('0'); for (int i = input.Length - range; i > -range; i -= range) { int st = i, len = range;/*st與len分別表示每組串的起始位置及長度*/ if (i < 0) { st = 0; len = range + i; } string tmp = input.Substring(st, len); long nn = long.Parse(tmp); string cur = ""; if (nn == 0) { ret = ss + ret; continue; } if (ss == '萬') { cur = transSmall(tmp) + ss; } else { cur = trans(tmp, 4, '萬') + ss; }/*計算得到每組的值後,拼接ss字元到末尾*/ if (nn % 10 == 0 && ret.Length > 0 && ret[0] != '零') {/*當前組末尾有零時判斷是否+零*/ int ll = num.Length - st - len; ll = ll > 8 ? 8 : ll;//*最大單位為億,則當前組之後8位範圍內無零則不輸出零例:一億零五,零需要輸出,一億億零五(而非一億零億零五)*/ string tt = num.Substring(st + len, ll); if (tt.Trim('0') != "")//後八位存在非0值 cur += '零'; } if (ss == '萬' && nn < 1000 || ss == '億' && nn < (long)1e7) {/*當前組前位空缺時,前位補零*/ cur = '零' + cur; } ret = cur + ret; } int s = 0, _len = ret.Length - 1; if (ret[0] == '零') { s = 1; _len--; } if (ret[ret.Length - 2] == '零') { _len--; }/*去掉多餘的前位零和後位的ss單位符之後的子串*/ ret = ret.Substring(s, _len); return ret; } public static string transBigInteger(string ss) {/*調用trans進行翻譯與異常處理,支援大數*/ string ret = ss.TrimStart('0'); string tt = "0123456789"; char[] ch = tt.ToCharArray(); ret = ret.Trim(' '); if (ret.Trim(ch) != "") { MessageBox.Show("輸入資料非法!"); return ""; } if (ret == "") { return "零"; } ret = trans(ret, 8, '億'); return ret; } } private class ChineseNumConvert {/*整數轉數字類,不支援大數 關鍵點:在哪裡進行乘法?計算乘法的範圍?*/ private static readonly string Digit = "零一二三四五六七八九"; private static readonly string Unit = "個十百千萬億"; private static readonly int[] num = { 1, 10, 100, 1000, 10000, 100000000 }; private static bool checkString(string word) {/*簡單判斷輸入是否合法 1;其他字元。 2:有非零數字相鄰*/ word = word.Trim(' '); string tt = Digit + Unit; string tmp = word.Trim(tt.ToCharArray()); if (tmp != "") return false; for (int i = 0; i < word.Length - 1; i++) { if (Digit.IndexOf(word[i]) >= 1 && Digit.IndexOf(word[i + 1]) >= 1) { return false; } } return true; } public static long ChineseNum2Int(string word) { if (!checkString(word)) { MessageBox.Show("輸入資料非法!"); return 0; } long sum = 0; long tmp = 0; long[] tt = new long[150]; int dig = -1, unit0 = -1, unit1 = -1, unit2 = -1; if (Unit.IndexOf(word[0]) >= 0) { word = "一" + word; } if (Unit.IndexOf(word[word.Length - 1]) < 0) { if (word.Length >= 2 && (tmp = Unit.IndexOf(word[word.Length - 2])) > 0) { word = word + Unit.ToCharArray()[tmp - 1]; } else { word += '個'; } } for (int i = 1; i < word.Length; i++) { dig = Digit.IndexOf(word[i - 1]); unit0 = Unit.IndexOf(word[i]); if (unit0 >= 0) { unit1 = -1; unit2 = -1; int idx = -1; for (int j = i - 1; j >= 0; j--) { unit1 = Unit.IndexOf(word[j]); if (unit1 >= 0) break; }//是否需要做乘法 for (int j = i - 1; j >= 0; j--) { unit2 = Unit.IndexOf(word[j]); if (unit2 > unit0) { idx = j; break; } }//決定乘法的作用範圍 if (unit1 >= 0 && unit0 >= unit1) { if (dig >= 0) sum += dig; if (unit2 == -1) { sum *= num[unit0]; } else { sum = (sum - tt[idx]) * num[unit0] + tt[idx]; } } else { if (dig < 0) dig = 1; sum += dig * num[unit0]; } } tt[i] = sum; } return sum; }
}