将下面的C程序改为DELPHI的代码(200分)

  • 主题发起人 主题发起人 小草
  • 开始时间 开始时间

小草

Unregistered / Unconfirmed
GUEST, unregistred user!
这是一段穷举法的代码?
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=''

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()

}
?>
 
程序可能有问题,自己再调试看看:

procedure createpassword;
const
passwordmax=8;
a:pchar='0123456789abcdefghijklmnopqrstuvwxyz';
var
ndictcount,nminl,nmaxl,nlength,i,j:integer;
cpass:array[0..passwordmax+2] of char;
bnext:Boolean;
cstdiofile:TextFile;
arr:array[0..passwordmax-1] of integer;
begin
ndictcount:=length(a);
nminl:=1;
nmaxl:=3;
IF (nminl>nmaxl) OR (nmaxl>passwordmax) then raise exception.Create('超界');
nlength:=nminl;

try
AssignFile(cstdiofile, 'c:/dict.txt');
Rewrite(cstdiofile);

while(nlength<=nmaxl) do
begin
for i:=0 to passwordmax do arr:=0;
bnext:=true;
while (bnext) do
begin
for i:=0 to nlength do
cpass:=a[arr ];
cpass[nlength+1]:=#0;
write(cstdiofile,cpass);
write(cstdiofile,'n')
/////// "/n" ?
for j:=nlength-1 downto 0 do
begin
inc(arr[j]);
if(arr[j]<>ndictcount-1) then break
else
begin
arr[j]:=0;
if(j=0) then bnext:=false;
end;
end;
end;
inc(nlength);
end;
finally
closeFile(cstdiofile);
end;
end;
 
用以下处理:

procedure createpassword;
const
passwordmax=8;
a:pchar='0123456789abcdefghijklmnopqrstuvwxyz';
var
n,i,j,nmin,nmax:integer;
cstdiofile:textfile;
nn:array[1..passwordmax] of integer;
s:string;
isend:boolean;
begin
n:=length(a);
nmin:=1;
nmax:=3;
IF (nmin>nmax) OR (nmax>passwordmax) then raise exception.Create('超界');

try
AssignFile(cstdiofile, 'c:/dict.txt');
Rewrite(cstdiofile);
for i:=nmin to nmax do
begin
for j:=1 to i do nn[j]:=0;
isend:=false;
while true do
begin
s:='';
for j:=1 to i do s:=s+a[nn[j]];
writeln(cstdiofile,s);
j:=i;
while true do
begin
inc(nn[j]);
if (j=1) and (nn[j]=n) then
begin
isend:=true;
break;
end;
if (nn[j]=n) then
begin
nn[j]:=0;
dec(j);
continue;
end;
break;
end;
if isend then break;
end;
end;
finally
closeFile(cstdiofile);
end;
end;


以下贴和你的意思一样:
http://www.delphibbs.com/delphibbs/DispQ.asp?LID=1431578
 
接受答案了.
 
后退
顶部