delphi ”完美"冒泡程序~~~
本帖最后由 fky1989 于 2011-10-11 17:06 编辑unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)
edt1: TEdit;
lbl1: TLabel;
btn1: TBitBtn;
lbl2: TLabel;
procedure btn1Click(Sender: TObject);
procedure edt1Change(Sender: TObject);
procedure edt1KeyPress(Sender: TObject; var Key: Char);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
numarr: array of Integer;
times: Integer = 0;
pass: boolean;
{$R *.dfm}
function spacecount(text: string): Integer;
// 计算edit.text里的空格数
var
I,temp: Integer;
begin
temp := 0;
if text <> '' then
for I := 0 to Length(text) do
if text = ' ' then
inc(temp);
Result := temp;
end;
procedure TForm1.btn1Click(Sender: TObject);
var
I,numtemp,temp1,temp2,loopa,loopc,loopd: Integer;
str:string;
begin
if times < 10 then
begin
ShowMessage('少于10个数字!');
Exit;
end;
temp1 := spacecount(edt1.Text);
if (temp1 >= 10) and (edt1.Text <> ' ') then
begin
ShowMessage('多于10个数字!');
Exit;
end;
str := Trim(edt1.Text) + ' ';
for I := 1 to 10 do
begin
numtemp := Pos(' ',str);
numarr := StrToInt(Copy(str,1,numtemp - 1));
Delete(str,1,numtemp);
end;
//将输入的每个数字放入数组中
for loopa := 1 to 10 do
begin
for loopc := loopa+1 to 10 do
begin
if numarr > numarr then
begin
temp2 := numarr;
numarr := numarr;
numarr := temp2;
end;
end;
end;
pass := False;
edt1.Clear;
for loopd := 1 to 10 do
edt1.Text := edt1.Text + IntToStr(numarr) + ' ';
pass := True;
end;
procedure TForm1.edt1Change(Sender: TObject);
//每当对edit进行各种操作的时候便动态的显示出已经输入了多少个数
var
temp: Integer;
begin
if pass then
begin
temp := spacecount(edt1.Text);
if (temp = 0) and (edt1.Text <> '') then
temp := 1
else
if (temp = 9) and (edt1.Text <> ' ') then
temp := 10
else
if edt1.Text <> ' ' then
inc(temp);
times := temp;
if times <= 10 then
lbl1.Caption := edt1.Text;
lbl2.Caption := IntToStr(times);
end;
end;
procedure TForm1.edt1KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in [#48..#57,#8,#32]) then
begin
Key := #0;
Exit;
end;
//只能按 数字键、空格、退格
if Key <> #8 then
begin
if (times >= 10) and (Key = #32) then
begin
ShowMessage('注意!只能输入10个数字!');
Exit;
end
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
pass := True;
end;
end.
相当不错! unit Sorting;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
txtNum: TEdit;
cmdSort: TButton;
txtNew: TEdit;
procedure cmdSortClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function NumSort(StrNum: string; Limiter: Char): string;
var
NumStr: TStringList;
L: Integer;
Temp: Integer;
i, j: Integer;
Num: array of Integer;
begin
NumStr := TStringList.Create;
NumStr.Delimiter := Limiter;
NumStr.DelimitedText := StrNum;
L := NumStr.Count;
SetLength(Num, L - 1);
for i := 0 to L - 1 do
begin
Num := StrToInt(NumStr)
end;
for j := 0 to L - 2 do
begin
for i := 0 to (L - 2 - j) do
begin
if Num < Num then
begin
Temp := Num;
Num := Num;
Num := Temp;
end
end;
end;
for i := 0 to L - 1 do // 连接成字符串
if i = L - 1 then
result := result + inttostr(Num) // 去除字符串最后的分割符号
else
result := result + inttostr(Num) + Limiter
end;
procedure TForm1.cmdSortClick(Sender: TObject);
begin
txtNew.Text := '';
txtNew.Text := NumSort(txtNum.Text, '-');
end;
end.
页:
[1]