高手,请把此c函数改成delphi函数!一段小代码,先改先得分......(100分)

M

mlong

Unregistered / Unconfirmed
GUEST, unregistred user!
//*******************************************************************
//在许多情况下我们需要穷举组合的算法,比如密码词典。
//我在程序员大本营上不止一次看到有人需要穷举密码的算法,就写了一个。
//这个算法的关键是密码下标进位的问题。
//另外本例子中的写文件语句效率比较低,为了降低算法复杂度没有优化。
//如果要提高写文件的效率,可以使用缓冲区,分批写入。
//*********************************************breath.cnpick.com*****

void createpassword()
{
#define passwordmax 8//将生成密码的最大长度

char a[]="0123456789abcdefghijklmnopqrstuvwxyz";//可能的字符
long ndictcount=sizeof(a);//获得密码词典长度
char cpass[passwordmax+2];//将生成的密码
long nminl=1,nmaxl=3;//本例中密码长度从1-3
long array[passwordmax];//密码词典下标

assert(nminl<=nmaxl &amp;&amp
nmaxl<=passwordmax);//容错保证
long nlength=nminl;
register long j,i=0;
bool bnext;
cstdiofile file;
file.open("c://dict.txt",cfile::modecreate|cfile::modewrite);
while(nlength<=nmaxl)
{
for(i=0;i<passwordmax;i++)
array=0;
bnext=true;
while(bnext)
{
for(i=0;i<nlength;i++)
cpass=a[array];
cpass='/0';
file.writestring(cpass);
file.writestring("/n");
for(j=nlength-1;j>=0;j--)//密码指针进位
{
array[j]++;
if(array[j]!=ndictcount-1)break;
else
{
array[j]=0;
if(j==0)bnext=false;
}
}

}
nlength++;
}
file.close();
}




 
没人愿意写,那我只能自己想想办法了。
 
本来就应该自己去写,自己写了还可以学习呢
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Unit2;

type


TForm1 = class(TForm)
ListBox1: TListBox;
procedure FormClose(Sender: TObject
var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
MyThread:TScannerThread;

implementation

{$R *.dfm}

procedure TForm1.FormClose(Sender: TObject
var Action: TCloseAction);
begin
Listbox1.Items.SaveToFile('c:/zhang.txt');
// MyThread.Suspend;
// MyThread.Terminate;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
MyThread:=TScannerThread.Create(listbox1.Items);
end;

end.


unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TScannerThread = class(TThread)
private
protected
public
MyItems:TStrings;
constructor Create(Items:TStrings);
procedure Execute
override;
procedure CreatePassword;
end;

implementation


{ TScannerThread }

constructor TScannerThread.Create(Items:TStrings);
begin
inherited Create(False);
MyItems:=Items;
FreeOnTerminate := True;
end;

procedure TScannerThread.CreatePassword;
var
j,i:Integer;
bnext:boolean;
David:string;
ndictcount,nlength:integer;
nMinLen,nMaxLen:Integer;
cpass:array[1..5] of char
//将生成的密码
arrayindex:array[1..5] of byte
//密码词典下标
begin
nMinLen:=1;
nMaxLen:=5;
David:='12345';
ndictcount:=length(David);//获得密码词典长度
nlength:=nMinLen;
MyItems.Clear;
while(nlength<=nMaxLen) do
begin
for i:=1 to nlength do
arrayindex:=1;
bnext:=true;
while(bnext) do
begin
for i:=1 to nlength do
cpass:=David[arrayindex];
cpass[nlength+1]:=#0;
MyItems.Add(cpass);
for j:=nlength downto 1 do//密码指针进位
begin
inc(arrayindex[j]);
if(arrayindex[j]<>ndictcount+1) then break
else arrayindex[j]:=1;
if(j=1) then bnext:=false;
end;
end;
inc(nlength);
end;
end;

procedure TScannerThread.Execute;
begin
inherited;
CreatePassword;
end;


end.

 

Similar threads

I
回复
0
查看
611
import
I
I
回复
0
查看
731
import
I
I
回复
0
查看
632
import
I
I
回复
0
查看
501
import
I
顶部