delphi2007 教程

delphi2007 教程

首页 新随笔 联系 聚合 管理
  1013 Posts :: 0 Stories :: 28 Comments :: 0 Trackbacks
如何写这算法 Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiBase/html/delphi_20061206162055262.html
有这样一个对应关系  
  1-A  
  2-B  
  3-C  
  ...  
  26-Z  
  27-AA  
  28-AB  
  ...  
  想输入数字后,显示对应的字母,代码要如何写?  
   
 

以下是我给你写的一个函数,可以实现你要求的算法(实际上是一个10进制整型转“26进制”字串的函数)  
   
  function   IntTo26(iInt:integer):string;  
  var  
        iTemp,iIndex,I:integer;  
        arr:array[1..2521]   of   char;  
        str:string;  
  begin  
        iTemp:=iInt;  
        str:='';  
        iIndex:=1;  
        repeat  
              arr[iIndex]:=char((iTemp   mod   26)+64);  
              if   (iTemp   mod   26)=0   then   arr[iIndex]:=char(90);  
              inc(iIndex);  
              iTemp:=iTemp   div   26   ;  
        until   iTemp   <   1;  
        for   i:=iIndex-1   downto   1   do  
                str:=str+arr[i];  
        result:=str;  
  end;  
   
  procedure   TForm1.Button1Click(Sender:   TObject);//测试数据  
  begin  
      showmessage('1       -->   '+IntTo26(   1)+#10  
                          +   '2       -->   '+IntTo26(   2)+#10  
                          +   '26     -->   '+IntTo26(26)+#10  
                          +   '27     -->   '+IntTo26(27)+#10  
                          +   '53     -->   '+IntTo26(53)+#10  
                          +   '54     -->   '+IntTo26(54)+#10  
                          +   '78     -->   '+IntTo26(78)+#10  
                          +   '79     -->   '+IntTo26(79)+#10  
                          +   '888   -->   '+IntTo26(888));  
  end;

测试结果为:  
  1       -->   A  
  2       -->   B  
  26     -->   AZ  
  27     -->   AA  
  53     -->   BA  
  54     -->   BB  
  78     -->   CZ  
  79     -->   CA  
  888   -->   AHD  
 

不好意思,上面的算法有点小小的问题,再等一下我改一改。

?

晕。连续回复不能超三次。晕死了。下面是修改后的,请测试:     (上面的作废)  
   
  function   IntTo26(iInt:integer):string;  
  var  
        iTemp,iIndex,I:integer;  
        arr:array[0..2521]   of   char;  
        str:string;  
  begin  
        iTemp:=iInt;  
        str:='';  
        iIndex:=1;  
        repeat  
              arr[iIndex]:=char((iTemp   mod   26)+64);  
              if   iTemp   mod   26   =   0   then  
                    begin  
                    arr[iIndex]:='Z';  
                    arr[iIndex+1]:=char(integer(arr[iIndex+1])-1);  
                    iTemp:=iTemp   -   26;  
                    end;  
              inc(iIndex);  
              iTemp:=iTemp   div   26   ;  
        until   iTemp   <1;  
        for   i:=iIndex-1   downto   1   do  
                str:=str+arr[i];  
        result:=str;  
  end;  
   
  procedure   TForm1.Button1Click(Sender:   TObject);       //     测试  
  begin  
          showmessage     ('1         -->   '+IntTo26(         1)+#10  
                                  +   '2         -->   '+IntTo26(         2)+#10  
                                  +   '26       -->   '+IntTo26(       26)+#10  
                                  +   '27       -->   '+IntTo26(       27)+#10  
                                  +   '52       -->   '+IntTo26(       52)+#10  
                                  +   '53       -->   '+IntTo26(       53)+#10  
                                  +   '54       -->   '+IntTo26(       54)+#10  
                                  +   '78       -->   '+IntTo26(       78)+#10  
                                  +   '79       -->   '+IntTo26(       79)+#10  
                                  +   '676     -->   '+IntTo26(     676)+#10  
                                  +   '17576-->   '+IntTo26(17576));  
  end;  
   
  //测试结果如下:  
  1         -->   A  
  2         -->   B  
  26       -->   Z  
  27       -->   AA  
  52       -->   AZ  
  53       -->   BA  
  54       -->   BB  
  78       -->   BZ  
  79       -->   CA  
  676     -->   YZ  
  17576-->   YYZ

posted on 2009-04-22 10:44 delphi2007 阅读(105) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。