unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TConfusion=class
private
FInputStr:string;
FOutputList:TStringList;
class procedure Swap(var a:char ;
var b:char);overload;
procedure Perm(s:string;first,last:LongInt);
function GetOutputList:TStringList;
public
Constructor Create(str:string='');
destructor Destroy;
override;
published
property InputStr:string read FInputStr write FInputstr;
property OutputList:TStringList read GetOutputList write FOutputList;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
class procedure TConfusion.Swap(var a:char ;
var b:char);
var
t:char;
begin
t:=a;
a:=b;
b:=t;
end;
procedure TConfusion.Perm(s:string;first,last:LongInt);
var
i:integer;
begin
if first=last then
begin
if FOutputList.IndexOf(s)=-1 then
begin
FOutputList.Add(s);
end;
end
else
begin
for i:=first to lastdo
begin
Swap(s[first],s);
Perm(s,first+1,last);
Swap(s[first],s);
end;
end;
end;
Constructor TConfusion.Create(str:string='');
begin
FOutputList:=TStringList.Create;
FInputStr:=str;
end;
destructor TConfusion.Destroy;
begin
FOutputList.Free;
end;
function TConfusion.GetOutputList:TStringList;
begin
if FInputStr='' then
Raise Exception.Create('字符串为空');
Perm(FInputStr,1,Length(FInputStr));
Result:=FOutputList;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
cf:TConfusion;
begin
cf:=TConfusion.Create(edit1.Text);
memo1.Text:= cf.OutputList.Text;
cf.Free;
end;
end.