找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 3624|回复: 0

【分享】取汉字的拼音缩写函数

[复制链接]

1214

主题

352

回帖

11

精华

管理员

菜鸟

积分
93755

贡献奖关注奖人气王精英奖乐于助人勋章

发表于 2009-7-26 14:43:57 | 显示全部楼层 |阅读模式
<p><strong><font color="#000080">function</font></strong> GetPyChar(<font color="#000080"><strong>const</strong></font> HZ: AnsiString): <font color="#000080"><strong>string</strong></font>;<br/><font color="#000080"><strong>const</strong></font><br/>HZCode: <font color="#000080"><strong>array</strong></font>[<font color="#0000ff">0</font>..<font color="#0000ff">25</font>, <font color="#0000ff">0</font>..<font color="#0000ff">1</font>] <font color="#000080"><strong>of</strong></font> Integer = ((<font color="#0000ff">1601</font>, <font color="#0000ff">1636</font>), (<font color="#0000ff">1637</font>, <font color="#0000ff">1832</font>), (<font color="#0000ff">1833</font>, <font color="#0000ff">2077</font>),<br/>&nbsp;&nbsp;&nbsp; (<font color="#0000ff">2078</font>, <font color="#0000ff">2273</font>), (<font color="#0000ff">2274</font>, <font color="#0000ff">2301</font>), (<font color="#0000ff">2302</font>, <font color="#0000ff">2432</font>), (<font color="#0000ff">2433</font>, <font color="#0000ff">2593</font>), (<font color="#0000ff">2594</font>, <font color="#0000ff">2786</font>), (<font color="#0000ff">9999</font>, <font color="#0000ff">0000</font>),<br/>&nbsp;&nbsp;&nbsp; (<font color="#0000ff">2787</font>, <font color="#0000ff">3105</font>), (<font color="#0000ff">3106</font>, <font color="#0000ff">3211</font>), (<font color="#0000ff">3212</font>, <font color="#0000ff">3471</font>), (<font color="#0000ff">3472</font>, <font color="#0000ff">3634</font>), (<font color="#0000ff">3635</font>, <font color="#0000ff">3722</font>), (<font color="#0000ff">3723</font>, <font color="#0000ff">3729</font>),<br/>&nbsp;&nbsp;&nbsp; (<font color="#0000ff">3730</font>, <font color="#0000ff">3857</font>), (<font color="#0000ff">3858</font>, <font color="#0000ff">4026</font>), (<font color="#0000ff">4027</font>, <font color="#0000ff">4085</font>), (<font color="#0000ff">4086</font>, <font color="#0000ff">4389</font>), (<font color="#0000ff">4390</font>, <font color="#0000ff">4557</font>), (<font color="#0000ff">9999</font>, <font color="#0000ff">0000</font>),<br/>&nbsp;&nbsp;&nbsp; (<font color="#0000ff">9999</font>, <font color="#0000ff">0000</font>), (<font color="#0000ff">4558</font>, <font color="#0000ff">4683</font>), (<font color="#0000ff">4684</font>, <font color="#0000ff">4924</font>), (<font color="#0000ff">4925</font>, <font color="#0000ff">5248</font>), (<font color="#0000ff">5249</font>, <font color="#0000ff">5589</font>));<br/><font color="#000080"><strong>var</strong></font><br/>i,j,HzOrd: Integer;<br/><font color="#000080"><strong>begin</strong></font><br/>i := <font color="#0000ff">1</font>;<br/><font color="#000080"><strong>while</strong></font> i &lt;= Length(HZ) <font color="#000080"><strong>do</strong></font><br/>&nbsp;&nbsp;&nbsp; <font color="#000080"><strong>begin</strong></font><br/>&nbsp;&nbsp;&nbsp; <font color="#000080"><strong>if</strong></font> (HZ &gt;= <font color="#0000ff">#160</font>) <font color="#000080"><strong>and</strong></font> (HZ[i + <font color="#0000ff">1</font>] &gt;= <font color="#0000ff">#160</font>) <font color="#000080"><strong>then</strong></font><br/>&nbsp;&nbsp;&nbsp; <font color="#000080"><strong>begin</strong></font><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HzOrd := (Ord(HZ) - <font color="#0000ff">160</font>) * <font color="#0000ff">100</font> + Ord(HZ[i + <font color="#0000ff">1</font>]) - <font color="#0000ff">160</font>;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#000080"><strong>for</strong></font> j := <font color="#0000ff">0</font> <font color="#000080"><strong>to</strong></font> <font color="#0000ff">25</font> <font color="#000080"><strong>do</strong></font><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#000080"><strong>begin</strong></font><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#000080"><strong>if</strong></font> (HzOrd &gt;= HZCode[j][<font color="#0000ff">0</font>]) <font color="#000080"><strong>and</strong></font> (HzOrd &lt;= HZCode[j][<font color="#0000ff">1</font>]) <font color="#000080"><strong>then</strong></font><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#000080"><strong>begin</strong></font><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Result := Result + Char(Byte(<font color="#0000ff">'A'</font>) + j);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Break;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#000080"><strong>end</strong></font>;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#000080"><strong>end</strong></font>;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Inc(i);<br/>&nbsp;&nbsp;&nbsp; <font color="#000080"><strong>end</strong></font> <font color="#000080"><strong>else</strong></font> Result := Result + HZ;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Inc(i);<br/><font color="#000080"><strong>end</strong></font>;<br/><font color="#000080"><strong>end</strong></font>;<br/><br/><font color="#008000">//测试:<br/></font><font color="#000080"><strong>procedure</strong></font> TForm1.Button1Click(Sender: TObject);<br/><font color="#000080"><strong>begin</strong></font><br/>ShowMessage(GetPyChar(<font color="#0000ff">'龙在天</font>)); <font color="#008000">//结果:</font></p>
<p><font color="#000080"><strong>end</strong></font>;<br/><br/><br/><font color="#008000">//只单独汉字:<br/></font><font color="#000080"><strong>function</strong></font> GetPyFitstChar(HZ: AnsiString): Char;<br/><font color="#000080"><strong>begin</strong></font><br/><font color="#000080"><strong>case</strong></font> LoWord(HZ[<font color="#0000ff">1</font>]) <font color="#000080"><strong>shl</strong></font> <font color="#0000ff">8</font> + LoWord(HZ[<font color="#0000ff">2</font>]) <font color="#000080"><strong>of</strong></font><br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$B0A1</font>..<font color="#0000ff">$B0C4</font>: Result := <font color="#0000ff">'A'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$B0C5</font>..<font color="#0000ff">$B2C0</font>: Result := <font color="#0000ff">'B'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$B2C1</font>..<font color="#0000ff">$B4ED</font>: Result := <font color="#0000ff">'C'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$B4EE</font>..<font color="#0000ff">$B6E9</font>: Result := <font color="#0000ff">'D'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$B6EA</font>..<font color="#0000ff">$B7A1</font>: Result := <font color="#0000ff">'E'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$B7A2</font>..<font color="#0000ff">$B8C0</font>: Result := <font color="#0000ff">'F'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$B8C1</font>..<font color="#0000ff">$B9FD</font>: Result := <font color="#0000ff">'G'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$B9FE</font>..<font color="#0000ff">$BBF6</font>: Result := <font color="#0000ff">'H'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$BBF7</font>..<font color="#0000ff">$BFA5</font>: Result := <font color="#0000ff">'J'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$BFA6</font>..<font color="#0000ff">$C0AB</font>: Result := <font color="#0000ff">'K'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$C0AC</font>..<font color="#0000ff">$C2E7</font>: Result := <font color="#0000ff">'L'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$C2E8</font>..<font color="#0000ff">$C4C2</font>: Result := <font color="#0000ff">'M'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$C4C3</font>..<font color="#0000ff">$C5B5</font>: Result := <font color="#0000ff">'N'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$C5B6</font>..<font color="#0000ff">$C5BD</font>: Result := <font color="#0000ff">'O'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$C5BE</font>..<font color="#0000ff">$C6D9</font>: Result := <font color="#0000ff">'P'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$C6DA</font>..<font color="#0000ff">$C8BA</font>: Result := <font color="#0000ff">'Q'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$C8BB</font>..<font color="#0000ff">$C8F5</font>: Result := <font color="#0000ff">'R'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$C8F6</font>..<font color="#0000ff">$CBF9</font>: Result := <font color="#0000ff">'S'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$CBFA</font>..<font color="#0000ff">$CDD9</font>: Result := <font color="#0000ff">'T'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$CDDA</font>..<font color="#0000ff">$CEF3</font>: Result := <font color="#0000ff">'W'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$CEF4</font>..<font color="#0000ff">$D188</font>: Result := <font color="#0000ff">'X'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$D1B9</font>..<font color="#0000ff">$D4D0</font>: Result := <font color="#0000ff">'Y'</font>;<br/>&nbsp;&nbsp;&nbsp; <font color="#0000ff">$D4D1</font>..<font color="#0000ff">$D7F9</font>: Result := <font color="#0000ff">'Z'</font>;<br/><font color="#000080"><strong>else</strong></font><br/>&nbsp;&nbsp;&nbsp; Result := Char(<font color="#0000ff">0</font>);<br/><font color="#000080"><strong>end</strong></font>;<br/><font color="#000080"><strong>end</strong></font>;<br/><br/><font color="#008000">//测试:<br/></font><font color="#000080"><strong>procedure</strong></font> TForm1.Button1Click(Sender: TObject);<br/><font color="#000080"><strong>begin</strong></font><br/>ShowMessage(GetPyFitstChar'龙在天<font color="#0000ff">'</font>)); <font color="#008000">//结果:L</font></p>
<p><font color="#000080"><strong>end</strong></font>;</p>
【VB】QQ群:1422505加的请打上VB好友
【易语言】QQ群:9531809  或 177048
【FOXPRO】QQ群:6580324  或 33659603
【C/C++/VC】QQ群:3777552
【NiceBasic】QQ群:3703755
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表