delphi冒泡排序随机&手动输入
根据小甲鱼delphi冒泡排序V2.0改编的一个既支持产生随机数排序,又支持手动输入排序。当在提示框中输入产生随机数列的个数为0时,可以进行手动输入的模式来进行冒泡排序,格式为:数字 空格 数字 空格 数字。点击点我进行输入后再点冒泡排序按钮进行排序显示。如果在提示框中输入的随机数列个数非零,那么可以随时产生该个数的随机数列,并可以点击冒泡排序进行排序显示。unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
edt1: TEdit;
btn3: TButton;
procedure FormCreate(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
num: Integer = 0;
arr: array of Integer;
flag: Boolean = True;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
num:= StrToInt(InputBox('提示', '请输入产生随机数列的个数: ', ''));
if (num = 0) then
begin
MessageBox(Handle, '随机数将不产生,需要手动输入', '提示', MB_OK or MB_ICONWARNING);
btn1.Caption:= btn1.Caption + '(0)';
btn1.Enabled:= False;
end
else
begin
btn1.Caption:= btn1.Caption + '(' + IntToStr(num) + ')';
SetLength(arr, num);
btn3.Enabled:= False;
edt1.ReadOnly:= True;
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
var
i: integer;
begin
Randomize;
if flag then
begin
edt1.Text:= '';
for i:= 0 to (num-1) do
begin
arr:= Random(100);
edt1.Text:= edt1.Text + ' ' + IntToStr(arr);
end;
flag:= False;
end
end;
procedure TForm1.btn2Click(Sender: TObject);
var
i,j,temp: Integer;
begin
temp:= 0;
flag:= True;
for i:= 1 to (num - 1) do
for j:= 0 to (num-i-1) do
begin
if (arr > arr) then
begin
temp:= arr;
arr:= arr;
arr:= temp;
end;
end;
edt1.Text:= '';
for i:= 0 to (num-1) do
begin
edt1.Text:= edt1.Text + ' ' + IntToStr(arr);
end;
end;
procedure TForm1.btn3Click(Sender: TObject);
var
i,j,len: Integer;
temp,str: string;
begin
len:= Length(edt1.Text);
SetLength(arr, len);
str:= Trim(edt1.Text);
j:= 0;
i:= 1;
while (i < len) do
begin
temp:= '';
while ((str <> #32) and (i <= len)) do
begin
temp:= temp + str;
i:= i + 1;
end;
arr:= StrToInt(temp);
j:= j + 1;
i:= i + 1;
end;
num:= j;
end;
end.
没学过delphi,路过支持下!
页:
[1]