您的位置:首页 > 其它

使用拼音首字母序列实现检索功能

2007-07-09 20:10 961 查看
事实上,解决这个问题的思路很简单。那就是要找出汉字表中拼音首字母分别为“A”到“Z”的汉字内码范围,这样对于要检索的汉字,只需要检查它的内码位于哪一个首字母的范围内,就可以判断出它的拼音首字母。
下面完成一个示范程序,选择“File|New Application”菜单命令,在默认的窗体中添加1个Label组件、1个Bevel组件、1个Edit组件和2个ListBox组件。完成对各个组件的属性设置及布局之后,在Edit组件的OnChange事件中添加如下所示的代码。

程序代码

Procedure TForm1.Edit1Change(Sender: TObject);
var ResultStr:string;
begin
ResultStr:='';
listbox2.Items.Text := SearchByPYIndexStr(listbox1.Items, edit1.Text);
end;

这里所使用的SearchByPYIndexStr函数的代码如下所示,其功能是在字符串列表中检索符合拼音索引字符串的所有字符串。可将其添加到窗体单元文件的implementation部分。

程序代码

Function SearchByPYIndexStr( SourceStrs:TStrings; PYIndexStr:string):string;
label NotFound;
var
i, j :integer;
hzchar :string;
begin
for i:=0 to SourceStrs.Count-1 do
begin
for j:=1 to Length(PYIndexStr) do
begin
hzchar:=SourceStrs[i][2*j-1]+ SourceStrs[i][2*j];
if (PYIndexStr[j]<>'?') and (UpperCase(PYIndexStr[j]) <>
GetPYIndexChar(hzchar)) then
goto NotFound;
end;
if result='' then
result := SourceStrs[i]
else
result := result + Char(13) + SourceStrs[i];
NotFound:
end;
end;

上面程序中引用的GetPYIndexChar函数用于获取指定汉字的拼音索引字母,如“杜”的索引字母是“D”。该函数的代码如下所示,也将其添加到窗体单元文件的implementation部分。

程序代码

Function GetPYIndexChar( hzchar:string):char;
begin
case WORD(hzchar[1]) shl 8 + WORD(hzchar[2
]) of
$B0A1..$B0C4 : result := 'A'
;
$B0C5..$B2C0 : result := 'B'
;
$B2C1..$B4ED : result := 'C'
;
$B4EE..$B6E9 : result := 'D'
;
$B6EA..$B7A1 : result := 'E'
;
$B7A2..$B8C0 : result := 'F'
;
$B8C1..$B9FD : result := 'G'
;
$B9FE..$BBF6 : result := 'H'
;
$BBF7..$BFA5 : result := 'J'
;
$BFA6..$C0AB : result := 'K'
;
$C0AC..$C2E7 : result := 'L'
;
$C2E8..$C4C2 : result := 'M'
;
$C4C3..$C5B5 : result := 'N'
;
$C5B6..$C5BD : result := 'O'
;
$C5BE..$C6D9 : result := 'P'
;
$C6DA..$C8BA : result := 'Q'
;
$C8BB..$C8F5 : result := 'R'
;
$C8F6..$CBF9 : result := 'S'
;
$CBFA..$CDD9 : result := 'T'
;
$CDDA..$CEF3 : result := 'W'
;
$CEF4..$D188 : result := 'X'
;
$D1B9..$D4D0 : result := 'Y'
;
$D4D1..$D7F9 : result := 'Z'
;
else

result := char(0);
end;
end;


完成之后,按下F9键运行程序,用户在编辑框中输入人名的拼音首字母后,在ListBox2中会自动检索对应的姓名。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐