當前位置:成語大全網 - 新華字典 - Delphi用拼音首字符序列實現檢索功能

Delphi用拼音首字符序列實現檢索功能

 在日常工作和生活中我們經常使用電子記事本查找個人通訊錄信息 或在單位的應用程序中查詢客戶檔案或業務資料 這個過程中往往需要輸入大量的漢字信息 對於熟悉計算機的人這已經是壹件頭疼的事 那些不太熟悉計算機或根本不懂漢字輸入的用戶簡直就望而生畏 作為對數據檢索技術的壹種新的嘗試 作者探索使用漢字拼音的首字符序列作為檢索關鍵字 這樣 用戶不必使用漢字 只須簡單地鍵入要查詢信息的每個漢字的拼音首字符即可 比如妳想查找關鍵字 中國人民銀行 妳只需要輸入 zgrmyh 作者希望通過下面的例子 為廣大計算機同行起壹個拋磚引玉的作用 讓我們開發的程序更加便捷 好用

 原理很簡單 找出漢字表中拼音首字符分別為 A 至 Z 的漢字內碼範圍 這樣 對於要檢索的漢字只需要檢查它的內碼位於哪壹個首字符的範圍內 就可以判斷出它的拼音首字符

 程序更簡單 包括 個控件 壹個列表存放著所有待檢索的信息 壹個列表用於存放檢索後的信息 壹個編輯框用於輸入檢索關鍵字(即拼音首字符序列) 詳細如下

  .進入Delphi創建壹個新工程 Project

  .在Form 上創建以下控件並填寫屬性

 控件類型? 屬性名稱? 屬性值    Edit? Name? Search    ListBox Name? SourceList    Items? 輸入壹些字符串 如姓名等 用於提供檢索數據    ListBox Name? ResultList?

  .鍵入以下兩個函數

 // 獲取指定漢字的拼音索引字母 如 漢 的索引字母是 H   function GetPYIndexChar( hzchar:string):char;    begin    case WORD(hzchar[ ]) shl + WORD(hzchar[ ]) of    $B A $B C : result := A ;    $B C $B C : result := B ;    $B C $B ED : result := C ;    $B EE $B E : result := D ;    $B EA $B A : result := E ;    $B A $B C : result := F ;    $B C $B FD : result := G ;    $B FE $BBF : result := H ;    $BBF $BFA : result := J ;    $BFA $C AB : result := K ;    $C AC $C E : result := L ;    $C E $C C : result := M ;    $C C $C B : result := N ;    $C B $C BD : result := O ;    $C BE $C D : result := P ;    $C DA $C BA : result := Q ;    $C BB $C F : result := R ;    $C F $CBF : result := S ;    $CBFA $CDD : result := T ;    $CDDA $CEF : result := W ;    $CEF $D : result := X ;    $D B $D D : result := Y ;    $D D $D F : result := Z ;    else    result := char( );    end;    end;

 // 在指定的字符串列表SourceStrs中檢索符合拼音索引字符串    PYIndexStr的所有字符串 並返回   function SearchByPYIndexStr    ( SourceStrs:TStrings;    PYIndexStr:string):string;    label NotFound;    var    i j? :integer;    hzchar :string;    begin    for i:= to SourceStrs Count do    begin    for j:= to Length(PYIndexStr) do    begin    hzchar:=SourceStrs[i][ *j ]    + SourceStrs[i][ *j];    if (PYIndexStr[j]<> ? ) and    (UpperCase(PYIndexStr[j]) <>    GetPYIndexChar(hzchar)) then goto NotFound;    end;    if result= then result := SourceStrs[i]    else result := result + Char    ( ) + SourceStrs[i];    NotFound:    end;    end;?

  .增加編輯框Search的OnChange事件

 procedure TForm SearchChange(Sender: TObject);    var ResultStr:string;    begin    ResultStr:= ;    ResultList Items Text := SearchByPYIndexStr    (Sourcelist Items Search Text);    end;?

  .編譯運行後

 在編輯框Search中輸入要查詢字符串的拼音首字符序列 檢索結果列表ResultList就會列出檢索到的信息 檢索中還支持 ? 通配符 對於難以確定的的文字使用 ? 替代位置 可以實現更復雜的檢索

lishixinzhi/Article/program/Delphi/201311/24877