When I look at the English word corresponding to the phone number of "beautiful programming", I think that sometimes I receive digital text messages such as "43" when I think of college ", in fact, Translation into Chinese characters means "Haha", but you have to rely on your brain for analysis, so I spent some time writing a small program, after converting "43" to "he", the code from "he" to "ha" has not been written yet, I think the subsequent program will involve some input method knowledge and I am not very familiar with it. Now I copy my Code as follows:
Using system; <br/> using system. collections. generic; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. drawing; <br/> using system. text; <br/> using system. windows. forms; <br/> using system. text. regularexpressions; <br/> namespace examexplainphoneshortmessage <br/>{< br/> Public partial class form1: form <br/> {<br/> # region attributes <br/> // <summary> <br/> // number of characters contained in each number from 0 to 9 <br/> // </Summary> <br/> int [] charcountofnumber = {0, 0, 3, 3, 3, 3, 3, 4, 3, 4 }; <br/> // <summary> <br/> // valid number <br/> /// </Summary> <br/> int [] validnumber = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; <br/> /// <summary> <br/> // 0-9 letters that each digit can represent <br/> /// </Summary> <br/> string [] charvalueofnumber = {"", "", "ABC", "def", "Ghi", "jki", "MnO", "pqrs", "TUV", "wxyz "}; </P> <p> // <summary> <br/> // all valid pinyin combinations <br/> /// </Summary> <br/> ilist <string> pinyintable = new list <string> (); <br/> datatable dt = new datatable (); <br/> // <summary> <br/> // test case <br/> // The result is "Ya TOU" <br/> // </Summary> <br/> string strtesttext = "92 868 "; <br/> /// <summary> <br/> // invalid pinyin <br/> /// </Summary> <br/> string blackstr = "null"; <br/> # endregion <br/> Public form1 () <br/> {<br/> initializecomponent (); <br/> initpinyintable (); <br/>}< br/> /// <summary> <br/> // inits the pinyin table. <br/> // </Summary> <br/> private void initpinyintable () <br/>{< br/> string filepath = environment. currentdirectory + @ "/pinyintable.txt"; <br/> system. io. streamreader = new system. io. streamreader (filepath, system. text. encoding. getencoding ("gb2312"); <br/> while (true) <br/>{< br/> string strline = streamreader. readline (); <br/> If (string. isnullorempty (strline) <br/>{< br/> break; <br/>}< br/> string [] STRs = strline. split ("/t ". tochararray (); <br/> foreach (string item in STRs) <br/>{< br/> If (string. isnullorempty (item) = false &&"". equals (item) = false) <br/>{< br/> pinyintable. add (item ); <br/>}< br/> /// <summary> <br/> // gets the probable spell from number. <br/> /// </Summary> <br/> /// <Param name = "strline"> the STR line. </param> <br/> // <returns> </returns> <br/> private ilist <string> getprobablespellfromnumber (string strline) <br/>{< br/> ilist <string> probableresults = new list <string> (); <br/> If (strline. contains ("0") | strline. contains ("1") <br/>{< br/> return probableresults; <br/>}< br/> strline. replace ("", ""); <br/> strline. replace ("/T", ""); <br/> strline. replace (",", ""); <br/> // regular expression to verify whether the string contains invalid characters (excluding spaces) <br/> // not yet written <br/> string [] words = strline. split ("". tochararray (); <br/> ilist <string> allwordcharcollection = new list <ilist <string> (); <br/> foreach (string word in words) <br/>{< br/> ilist <string> wordcharcollection = getwordallcharcollection (Word); <br/> allwordcharcollection. add (wordcharcollection); <br/>}< br/> int totalcombination = 1; <br/> foreach (ilist <string> item in allwordcharcollection) <br/>{< br/> If (item. count> 0) <br/>{< br/> totalcombination = totalcombination * item. count; <br/>}< br/> stringbuilder [] ARGs = new stringbuilder [totalcombination]; <br/> for (INT I = 0; I <totalcombination; I ++) <br/>{< br/> ARGs [I] = new stringbuilder (""); <br/>}< br/> foreach (ilist <string> item in allwordcharcollection) <br/>{< br/> If (item. count> 0) <br/>{< br/> for (INT I = 0; I <totalcombination; I ++) <br/>{< br/> int Index = I % item. count; <br/> ARGs [I]. append (item [Index]); <br/> ARGs [I]. append (""); <br/>}< br/> // display possible result combinations <br/> string [] strargs = new string [totalcombination]; <br/> for (INT I = 0; I <totalcombination; I ++) <br/> {<br/> strargs [I] = ARGs [I]. tostring (); <br/>}< br/> This. richTextBox. lines = strargs; <br/> return probableresults; <br/>}< br/> /// <summary> <br/> // obtain all letters and combinations of a number. <br/> /// </Summary> <br/> // <Param name = "word"> the word. </param> <br/> private ilist <string> getwordallcharcollection (string word) <br/>{< br/> int n = word. length; <br/> int [] Number = new int [N]; <br/> for (INT I = 0; I <n; I ++) <br/> {<br/> Number [I] = int32.parse (word [I]. tostring (); <br/>}< br/> int [] Answer = {0, 0, 0, 0 }; <br/> // returned result <br/> ilist <string> wordcharcollection = new list <string> (); <br/> while (true) <br/> {<br/> stringbuilder strbuilder = new stringbuilder (""); <br/> for (INT I = 0; I <n; I ++) <br/>{< br/> strbuilder. append (charvalueofnumber [number [I] [answer [I]. tostring (); <br/>}< br/> // a valid pinyin <br/> If (pinyintable. contains (strbuilder. tostring () <br/>{< br/> wordcharcollection. add (strbuilder. tostring (); <br/>}< br/> int Index = n-1; <br/> while (index> = 0) <br/> {<br/> If (answer [Index] <charcountofnumber [number [Index]-1) <br/>{< br/> answer [Index] ++; <br/> break; <br/>}< br/> else <br/> {<br/> answer [Index] = 0; <br/> index --; <br/>}< br/> If (index <0) <br/> break; <br/>}</P> <p> return wordcharcollection; <br/>}< br/> /// <summary> <br/> // gets the probable Chinese chars from spells. <br/> /// </Summary> <br/> /// <Param name = "spells"> the spells. </param> <br/> // <returns> </returns> <br/> private ilist <string> getprobablechinesecharsfromspells (ilist <string> spells) <br/>{< br/> ilist <string> probablechinesechars = new list <string> (); <br/> return probablechinesechars; <br/>}< br/> /// <summary> <br/> // gets the probable Chinese spell Through filter all spells. <br/> /// </Summary> <br/> /// <Param name = "spells"> the spells. </param> <br/> // <returns> </returns> <br/> private ilist <string> getprobablechinesespellthroughfilterallspells (ilist <string> spells) <br/>{< br/> ilist <string> probablechinesechars = new list <string> (); </P> <p> probablechinesechars = spells; <br/> return probablechinesechars; <br/>}< br/> /// <summary> <br/> // number-chinesespell <br/> /// </Summary> <br/> /// <Param name = "sender"> the source of the event. </param> <br/> // <Param name = "E"> the <see CREF = "system. eventargs "/> instance containing the event data. </param> <br/> private void button3_click (Object sender, eventargs E) <br/>{< br/> try <br/> {<br/> strtesttext = this.txt number. text. trim (); <br/> ilist <string> allspells = getprobablespellfromnumber (strtesttext); <br/>}< br/> catch (exception ex) <br/>{< br/> MessageBox. show ("program exception:" + ex. message, "prompt", messageboxbuttons. OK, messageboxicon. error); <br/>}< br/>}
The actual running effect is as follows: