Windows程序(C# VS 2008)杂记之拼音转汉字
来源:互联网 发布:sar指标源码2 编辑:程序博客网 时间:2024/06/05 07:13
2014年3月7日星期五 北京海淀五路居 晴
前天尝试将汉字转化为拼音,昨天开始试着将拼音转化为汉字。
在文本框里输入拼音,然后下面显示对应的汉字;
如果汉字较多的话,进行分页显示,默认每页显示5个汉字;
一.先进行类的设计:
二.伪代码:
1.Form_Load
加载一个输入法的窗口
2.Form_KeyPress
输入拼音, 动态显示汉字
非拼音,不处理
三.编码:
1.设计窗口
Done
2.编写TypeWorkspace类
Done
3.编写QueryWords类
Done
4.编写Page类
Done
四.具体实现:
1.设计窗口
其代码如下:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms; namespace TypeWritting{ public partial class TypeWrittingFrm : Form { //输入操作间 private TypeWorkspace tWorkSpace= null; //分页工具 private Page page = null; //用于承载查询到的汉字 private char[] words = null; public TypeWrittingFrm() { InitializeComponent(); //实例化工作间 tWorkSpace = new TypeWorkspace(); } //查询汉字 private void queryWords(string spellStr) { //工作间接受输入的拼音 //进行查询 words = tWorkSpace.query(spellStr); //如果查询到汉字 if(words!= null) //初始化分页工具 page = new Page(words.Length); } //用于显示汉字 private void show() { string wordsToDisplay = ""; //查询出汉字进行显示 if (words!= null) { //如果不是最后一页,将向下翻页按钮设为可用,否则不可用 if (!page.IsEnd) this.btnPageDown.Enabled= true; else this.btnPageDown.Enabled= false; //如果不是第一页,将向上翻页按钮设为可用,否则不可用 if (!page.IsBegin) this.btnPageUp.Enabled= true; else this.btnPageUp.Enabled= false; //进行显示 for (int i = 0; i < page.NumOneLine;i++) { //如果有汉字可以显示 if ((page.StartIndex + i)< words.Length) { wordsToDisplay += (i + 1) + "."+ words[page.StartIndex + i].ToString() + " "; } else break; //没字可显示,跳出循环 } } lbShowWords.Text = wordsToDisplay; } private void tbInputSpell_TextChanged(object sender, EventArgs e) { queryWords(tbInputSpell.Text.ToString()); //查询汉字 show(); //显示汉字 } private void btnPageDown_Click(object sender, EventArgs e) { //向下翻页 page.PageDown(); show(); //翻页后显示 } private void btnPageUp_Click(object sender, EventArgs e) { //向上翻页 page.PageUp(); show(); //翻页后显示 } }}2.编写TypeWorkspace类
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace TypeWritting{ class TypeWorkspace { //输入的拼音 private stringinputSpell; public stringInputSpell { get{ return inputSpell;} set{ inputSpell = value;} } //返回查询出的汉字 public char[]query(stringinputSpell) { returnQueryWords.query(inputSpell); } }}3.编写QueryWords类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.International.Converters.PinYinConverter; namespace TypeWritting{ class QueryWords { //输入的拼音 private stringspellStr; public stringSpellStr { get{ return spellStr;} set{ spellStr = value;} } //查询到的汉字的数量 private staticint numOfWords(char[][] words) { inttheNum = 0; for(int i = 0; i < words.Length; i++) { //如果对应的声调,存在汉字,将数量累加 if(null != words[i]) theNum+= words[i].Length; } returntheNum; } //查询汉字 public static char[]query(stringspellStr) { //用来装载查询到的所有汉字的字符数组 char[]allWords = null; //在输入拼音不为空的情况下,进行查询 if(null != spellStr&& string.Empty!= spellStr) { //查询所有同音字 char[][]words = new char[4][]; words[0]= ChineseChar.GetChars(spellStr.ToUpper()+ "1"); //查询音调为1声的汉字 words[1]= ChineseChar.GetChars(spellStr.ToUpper()+ "2"); //查询音调为2声的汉字 words[2]= ChineseChar.GetChars(spellStr.ToUpper()+ "3"); //查询音调为3声的汉字 words[3]= ChineseChar.GetChars(spellStr.ToUpper()+ "4"); //查询音调为4声的汉字 //查询到的汉字的数量 intnumWords = numOfWords(words); //如果存在查询到的汉字 if(numWords > 0) { //建立一个可以容纳所有汉字的数组 allWords= new char[numWords]; //所有汉字放到一维数组中 intcount = -1; //计数器 ++count,所以设为-1 for (int i = 0; i < words.Length; i++) { //如果对应的声调,存在汉字 if (null != words[i]) { for (int j = 0; j < words[i].Length; j++) allWords[++count]= words[i][j]; } } } } returnallWords; } }}4.编写Page类
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace TypeWritting{ class Page { public Page(int allWords) { this.allWords = allWords; isBegin= true; //默认是第一页 isLastPage(allWords); //判断是否为最后一页 } //汉字总量 private intallWords; public intAllWords { get{ return allWords;} set{ allWords = value;} } //一行显示多少汉字,默认为5个 private intnumOneLine = 5; public intNumOneLine { get{ return numOneLine;} set{ numOneLine = value;} } //从哪儿开始显示,默认从第一个开始显示 private intstartIndex = 0; public intStartIndex { get{ return startIndex;} set{ startIndex = value;} } //是否是第一页 private boolisBegin; public boolIsBegin { get{ return isBegin;} set{ isBegin = value;} } //是否是最后一页 private boolisEnd; public boolIsEnd { get{ return isEnd;} set{ isEnd = value;} } //翻开上一页 public voidPageUp() { //如果不是第一页 if(!isFirstPage(startIndex)){ startIndex-= numOneLine; } //翻完页之后判断是否是第一页 isFirstPage(startIndex); //翻完页之后,还需要判断是否为最后一页 //保证最后一页时,向上翻页后,向下翻页的按钮能使用 isLastPage(allWords); } //翻开下一页 public voidPageDown() { //如果不是最后一页 if(!isLastPage(allWords)){ startIndex+= numOneLine; } //翻完页之后,还需要判断是否为最后一页,这样在显示的时候,向下翻页的按钮便不能再用 isLastPage(allWords); //翻完页之后判断是否是第一页 isFirstPage(startIndex); } //判断是否为第一页 public boolisFirstPage(intbeginIndex) { returnbeginIndex == 0 ? isBegin= true : isBegin= false; } //判断是否为最后一页 public boolisLastPage(intnumOfWords) { returnnumOfWords <= startIndex+ numOneLine ? isEnd= true : isEnd= false; } }}五.运行结果:
注:
还是用到了前天的插件MicrosoftVisual Studio International Pack 1.0 SR1
0 0
- Windows程序(C# VS 2008)杂记之拼音转汉字
- Windows程序(C# VS 2008)杂记 之string和拼音
- Windows程序(C# VS 2008)杂记之Java访问Webservice服务
- Windows程序(C# VS 2008)杂记之GDI+只能向右下奔跑的矩形
- Windows程序(C# VS 2008)杂记之智能设备项目打包CAB
- Windows程序(C# VS 2008)杂记之智能设备项目打包CAB
- Windows程序(C# VS 2008)杂记之重命名项目名、项目文件夹、命名空间
- c# 汉字转拼音
- c#汉字转拼音
- C# 汉字转拼音
- c#汉字转拼音
- C# 汉字转拼音
- c# 汉字转拼音
- C# 汉字转拼音
- C# 汉字转拼音
- 汉字转拼音程序
- C#汉字转拼音(全拼)
- C# 汉字转拼音(全拼)
- 腾讯与京东婚联
- App提交审核被拒的原因汇总(不断更新...)
- 用FileUpload控件实现文件上传
- 使用UMDH查找内存泄漏
- Codeforces Round #234 (Div. 2)
- Windows程序(C# VS 2008)杂记之拼音转汉字
- linux下采用lvm技术动态调整磁盘容量
- 每个极客都应该知道的Linux技巧
- java中string和int互相转化
- windows服务器您试图从目录中执行CGI、ISAPI 或其他可执行程序,但该目
- 微信POS的对手不止是阿里,还有银联
- java中TextArea和JTextArea的自动换行和滚动条
- 算法导论之桶排序
- 在ASP.NET中设置html文件的访问验证