电话号码对应的英文单词 手机数字短信翻译小工具 数字输入法初型 需求“ya tou”咋写程序实现翻译成“丫头”,有哪位有思路的请指教。...

在看“编程之美”的电话号码对应的英文单词那章时,想起了大学的时候有时候会收到数字短信如“43 43”,其实翻译成汉字就是“呵呵”的意思,但你得凭借着你的大脑去分析,所以我花了点时间写了个小程序,完成了“43 43”到“he he”的转化工作,至于更进一步的“he he”到“呵呵”的代码还没有写,觉得后面的程序会涉及到输入法的一些知识了,自己不是很熟悉了,现在把我的代码copy如下:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressions; namespace ExamExplainPhoneShortMessage { public partial class Form1 : Form { #region 属性 ///

/// 0-9 每个数字包含的字符数 /// int[] charCountOfNumber = { 0, 0, 3, 3, 3, 3, 3, 4, 3, 4 }; /// /// 有效的数字 /// int[] validNumber = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; /// /// 0-9 每个数字可以表示的字母 /// string[] charValueOfNumber = { "", "", "abc", "def", "ghi", "jki", "mno", "pqrs", "tuv", "wxyz" }; /// /// 所有 有效的拼音组合 /// IList pinyinTable = new List(); DataTable dt = new DataTable(); /// /// 测试用例 /// 结果为 "ya tou" /// string strTestText = "92 868"; /// /// 无效拼音 /// string blackStr = "null"; #endregion public Form1() { InitializeComponent(); initPinyinTable(); } /// /// Inits the pinyin table. /// private void initPinyinTable() { string filePath = Environment.CurrentDirectory + @"/pinyinTable.txt"; System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath, System.Text.Encoding.GetEncoding("gb2312")); while (true) { string strLine = streamReader.ReadLine(); if (string.IsNullOrEmpty(strLine)) { break; } string[] strs = strLine.Split("/t".ToCharArray()); foreach (string item in strs) { if (string.IsNullOrEmpty(item) == false && "".Equals(item) == false) { pinyinTable.Add(item); } } } } /// /// Gets the probable spell from number. /// /// The STR line. /// private IList GetProbableSpellFromNumber(string strLine) { IList probableResults = new List(); if (strLine.Contains("0") || strLine.Contains("1")) { return probableResults; } strLine.Replace(" ", " "); strLine.Replace("/t", " "); strLine.Replace(",", " "); // 正则表达式验证 字符串串中是否有非法字符(排除空格在外) // 暂时没写 string[] words = strLine.Split(" ".ToCharArray()); IList> allWordCharCollection = new List>(); foreach (string word in words) { IList wordCharCollection = getWordAllCharCollection(word); allWordCharCollection.Add(wordCharCollection); } int totalCombination = 1; foreach (IList item in allWordCharCollection) { if (item.Count > 0) { totalCombination = totalCombination * item.Count; } } StringBuilder[] args = new StringBuilder[totalCombination]; for (int i = 0; i < totalCombination; i++) { args[i] = new StringBuilder(""); } foreach (IList item in allWordCharCollection) { if (item.Count > 0) { for (int i = 0; i < totalCombination; i++) { int index = i % item.Count; args[i].Append(item[index]); args[i].Append(" "); } } } // 显示可能的结果组合 string[] strArgs = new string[totalCombination]; for (int i = 0; i < totalCombination; i++) { strArgs[i] = args[i].ToString(); } this.richTextBox.Lines = strArgs; return probableResults; } /// ///获得一个数字的 所有字母组合 /// /// The word. private IList getWordAllCharCollection(string word) { int n = word.Length; int[] number = new int[n]; for (int i = 0; i < n; i++) { number[i] = Int32.Parse(word[i].ToString()); } int[] answer = { 0, 0, 0, 0 }; // 返回的结果 IList wordCharCollection = new List(); while (true) { StringBuilder strBuilder = new StringBuilder(""); for (int i = 0; i < n; i++) { strBuilder.Append(charValueOfNumber[number[i]][answer[i]].ToString()); } // 一个有效的拼音 if (pinyinTable.Contains(strBuilder.ToString())) { wordCharCollection.Add(strBuilder.ToString()); } int index = n - 1; while (index >= 0) { if (answer[index] < charCountOfNumber[number[index]] - 1) { answer[index]++; break; } else { answer[index] = 0; index--; } } if (index < 0) break; } return wordCharCollection; } /// /// Gets the probable chinese chars from spells. /// /// The spells. /// private IList GetProbableChineseCharsFromSpells(IList spells) { IList ProbableChineseChars = new List(); return ProbableChineseChars; } /// /// Gets the probable chinese spell through filter all spells. /// /// The spells. /// private IList getProbableChineseSpellThroughFilterAllSpells(IList spells) { IList ProbableChineseChars = new List(); ProbableChineseChars = spells; return ProbableChineseChars; } /// /// Number-ChineseSpell /// /// The source of the event. /// The instance containing the event data. private void button3_Click(object sender, EventArgs e) { try { strTestText = this.txtNumber.Text.Trim(); IList allSpells = GetProbableSpellFromNumber(strTestText); } catch (Exception ex) { MessageBox.Show("程序出现异常:"+ex.Message, "提示",MessageBoxButtons.OK,MessageBoxIcon.Error); } } } }

实际运行效果如下:


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部