输入一个汉字字符串'我爱中国',怎么返回他的首拼'WAZG',我觉得好难哦。请高手点拨啊,说十声谢谢,请详细讲解一下首拼查询的算法谢谢 谢谢 谢谢 谢谢 谢谢 谢谢 谢谢 谢谢 谢谢 谢谢 

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        function GetPYIndexChar( hzchar:string):char;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}{ TForm1 }function TForm1.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;procedure TForm1.Button1Click(Sender: TObject);
    var
      s,t:String;
    begin
      s:='我爱中国';//有一个假设,那就是字符串s里面只有中文
      t:='';
      While s>'' do
      begin
        t:=t+GetPYIndexChar(Copy(s,1,2));
        Delete(s,1,2);
      end;  Showmessage(t);
    end;end.
      

  2.   

    function Tform1.GetSpell(Str:string):String;
    var
      Tint_StrLength:Integer;
      Tint_I:Integer;
      Tstr_Spell:String;
      Tstr_Bg5:String;
    begin
      Tstr_Spell:='';
      Tint_StrLength:=Length(Str);
      if Tint_StrLength >0 then
      begin
        Tint_I:=1;
        while Tint_I<=Tint_StrLength do
        begin
          case Integer(Str[Tint_I])  of
            0..127:
                  begin
                    Tstr_Spell:=Tstr_Spell+(copy(Str,Tint_I,1));
                    Tint_I:=Tint_I+1;
                  end
            else
            begin
              Tstr_Bg5:= copy(Str,Tint_I,2);
              case WORD(Tstr_Bg5[1]) shl 8 + WORD(Tstr_Bg5[2]) of
                $B0A1..$B0C4 : Tstr_Spell:=Tstr_Spell+ 'A';
                $B0C5..$B2C0 : Tstr_Spell:=Tstr_Spell+ 'B';
                $B2C1..$B4ED : Tstr_Spell:=Tstr_Spell+ 'C';
                $B4EE..$B6E9 : Tstr_Spell:=Tstr_Spell+ 'D';
                $B6EA..$B7A1 : Tstr_Spell:=Tstr_Spell+ 'E';
                $B7A2..$B8C0 : Tstr_Spell:=Tstr_Spell+ 'F';
                $B8C1..$B9FD : Tstr_Spell:=Tstr_Spell+ 'G';
                $B9FE..$BBF6 : Tstr_Spell:=Tstr_Spell+ 'H';
                $BBF7..$BFA5 : Tstr_Spell:=Tstr_Spell+ 'J';
                $BFA6..$C0AB : Tstr_Spell:=Tstr_Spell+ 'K';
                $C0AC..$C2E7 : Tstr_Spell:=Tstr_Spell+ 'L';
                $C2E8..$C4C2 : Tstr_Spell:=Tstr_Spell+ 'M';
                $C4C3..$C5B5 : Tstr_Spell:=Tstr_Spell+ 'N';
                $C5B6..$C5BD : Tstr_Spell:=Tstr_Spell+ 'O';
                $C5BE..$C6D9 : Tstr_Spell:=Tstr_Spell+ 'P';
                $C6DA..$C8BA : Tstr_Spell:=Tstr_Spell+ 'Q';
                $C8BB..$C8F5 : Tstr_Spell:=Tstr_Spell+ 'R';
                $C8F6..$CBF9 : Tstr_Spell:=Tstr_Spell+ 'S';
                $CBFA..$CDD9 : Tstr_Spell:=Tstr_Spell+ 'T';
                $CDDA..$CEF3 : Tstr_Spell:=Tstr_Spell+ 'W';
                $CEF4..$D1B8 : Tstr_Spell:=Tstr_Spell+ 'X';
                $D1B9..$D4D0 : Tstr_Spell:=Tstr_Spell+ 'Y';
                $D4D1..$D7F9 : Tstr_Spell:=Tstr_Spell+ 'Z';
              else
                Tstr_Spell:=Tstr_Spell+ char(0);
              end;
              Tint_I:=Tint_I+2;
            end;
          end;
        end;
      end;
      Result:=Tstr_Spell;
    end;
      

  3.   

    在数据库-〉SQL中的精华帖子中找找。
      

  4.   

    ---- 在日常工作和生活中我们经常使用电子记事本查找个人通讯录信息,或在单位的应用程序中查询客户档案或业务资料,这个过程中往往需要输入大量的汉字信息,对于熟悉计算机的人这已经是一件头疼的事,那些不太熟悉计算机或根本不懂汉字输入的用户简直就望而生畏。作为对数据检索技术的一种新的尝试,作者探索使用汉字拼音的首字符序列作为检索关键字,这样,用户不必使用汉字,只须简单地键入要查询信息的每个汉字的拼音首字符即可。比如你想查找关键字“中国人民银行”,你只需要输入“zgrmyh”。作者希望通过下面的例子,为广大计算机同行起一个抛砖引玉的作用,让我们开发的程序更加便捷、好用。 ---- 原理很简单,找出汉字表中拼音首字符分别为“A”至“Z”的汉字内码范围,这样,对于要检索的汉字只需要检查它的内码位于哪一个首字符的范围内,就可以判断出它的拼音首字符。 ---- 程序更简单,包括3个控件:一个列表存放着所有待检索的信息;一个列表用于存放检索后的信息;一个编辑框用于输入检索关键字(即拼音首字符序列)。详细如下: ---- 1.进入Delphi创建一个新工程:Project1 ---- 2.在Form1上创建以下控件并填写属性: 控件类型      属性名称  属性值
    Edit           Name      Search
    ListBox        Name      SourceList
    Items      输入一些字符串,如姓名等,用于提供检索数据
    ListBox        Name      ResultList
     ---- 3.键入以下两个函数 // 获取指定汉字的拼音索引字母,如:“汉”的索引字母是“H”
    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;// 在指定的字符串列表SourceStrs中检索符合拼音索引字符串
    PYIndexStr的所有字符串,并返回。
    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;4.增加编辑框Search的OnChange事件:
    procedure TForm1.SearchChange(Sender: TObject);
    var ResultStr:string;
    begin
      ResultStr:='';
      ResultList.Items.Text := SearchByPYIndexStr
    (Sourcelist.Items, Search.Text);
    end; 
     ---- 5.编译运行后,在编辑框Search中输入要查询字符串的拼音首字符序列,检索结果列表ResultList就会列出检索到的信息,检索中还支持“?”通配符,对于难以确定的的文字使用“?”替代位置,可以实现更复杂的检索。 
      

  5.   

    \\\|///
                            \\  - -  //
                             (  @ @  )
    +---------------------oOOoo-(?)ooOOo---------------------+
    |                                                        |
    |          欢迎访问 http://www.coderpub.com 技术论坛     |
    |          delphi,asp.net,C#,j2me,软件工程,企业管理      |
    |                 http://www.coderpub.com                |
    |                                                        |
    |                               Ooooo                    |
    +-----------------------ooooO--(   )---------------------+
                            (   )   )|/
                             \|(   (_/
                              \_)
      

  6.   

    看一下我的DLL
    library Prgetwbpy;{ Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }
    //拼音,五笔   1: 拼音  2:五笔  3: 拼音简码  4:五笔简码
    uses
     ShareMem, SysUtils,Windows,
      Classes;{$R *.res}
    {$R wbtext.res}
    function GetWbPy(hzstr: string; pytype: integer):String;stdcall;
     var
      I: Integer;
      allstr: string;
      hh: THandle;
      pp: pointer;
      ss: TStringList;
      function retturn_wbpy(tempstr: string; tqtype: integer): String;
      var
       outstr, str: string;
       i: integer;
       begin
      //################### 汉字查询电位
        i := 0;
        while i <= ss.Count - 1 do
        begin
          str := ss.Strings[i];
          if (tempstr = trim(str[1] + str[2])) or (tempstr = trim(str[3] + str[4])) then
          begin
            str := ss.Strings[i];
            Break;
          end;
          i := i + 1;
        end;
      //###################    outstr := '';     //提取编码
        if tqtype = 1 then
        begin
          for i := pos('①', str) + 2 to pos('②', str) - 1 do
            if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
        end;
        if tqtype = 2 then
        begin
          for i := pos('②', str) + 2 to pos('③', str) - 1 do
            if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
        end;    if tqtype = 3 then
        begin
          for i := pos('③', str) + 2 to pos('④', str) - 1 do
            if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
        end;    if tqtype = 4 then
        begin
          for i := pos('④', str) + 2 to length(str) do
            if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
        end;
        Result := trim(outstr);
      end;
    begin
     //加载资源文件,将内容赋值给 s
      ss := TStringList.Create;
      hh := FindResource(hInstance, 'mywb', 'TXT');
      hh := LoadResource(hInstance, hh);
      pp := LockResource(hh);
      ss.Text := pchar(pp);
      UnLockResource(hh);
      FreeResource(hh);
      allstr := '';
      i := 0;
      while i <= length(hzstr) do   //提取汉字字符
      begin
        if (Ord(hzstr[I]) > 127) then
        begin
          if allstr = '' then allstr := retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype) else allstr := allstr + retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype);
          i := i + 2;
        end
        else
        begin
          if allstr = '' then allstr := hzstr[I] else allstr := allstr + hzstr[I];
          i := i + 1;
        end;
      end;
      ss.Free;
      Result := trim(allstr);
    end;
     exports
      GetWbPy;
    begin
    end.
    //调用方式
    //拼音,五笔   1: 拼音  2:五笔  3: 拼音简码  4:五笔简码
    //edit1.text=getwbpy(edit2.text,3)
    我在DELPHI6测试没问题,绝对可以
      

  7.   

    不过一定要有wbtext.res这个资源文件,不过我不知怎么给楼主,留个E_main吧