鱼C论坛

 找回密码
 立即注册
查看: 3659|回复: 2

[原创] delphi ”完美"冒泡程序~~~

[复制链接]
发表于 2011-10-11 17:00:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 fky1989 于 2011-10-11 17:06 编辑






Project1.zip

(497.74 KB, 下载次数: 59)








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[1..10] 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[i] = ' ' 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[Length(edt1.Text)] <> ' ') then
begin
ShowMessage('多于10个数字!');
Exit;
end;


str := Trim(edt1.Text) + ' ';


for I := 1 to 10 do
begin
numtemp := Pos(' ',str);
numarr[i] := 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[loopa] > numarr[loopc] then
begin
temp2 := numarr[loopa];
numarr[loopa] := numarr[loopc];
numarr[loopc] := temp2;
end;
end;
end;

pass := False;
edt1.Clear;


for loopd := 1 to 10 do
edt1.Text := edt1.Text + IntToStr(numarr[loopd]) + ' ';

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[Length(edt1.Text)] <> ' ') then
temp := 10
else
if edt1.Text[Length(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.

评分

参与人数 2荣誉 +11 鱼币 +10 收起 理由
asky533 + 5 + 5 ^_^
小甲鱼 + 6 + 5 很认真的完成!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-10-12 17:28:28 | 显示全部楼层
相当不错!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-11-1 00:46:08 | 显示全部楼层
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[i] := StrToInt(NumStr[i])
  end;

  for j := 0 to L - 2 do
  begin
    for i := 0 to (L - 2 - j) do
    begin
      if Num[i] < Num[i + 1] then
      begin
        Temp := Num[i];
        Num[i] := Num[i + 1];
        Num[i + 1] := Temp;
      end
    end;
  end;
  for i := 0 to L - 1 do // 连接成字符串
    if i = L - 1 then
      result := result + inttostr(Num[i]) // 去除字符串最后的分割符号
    else
      result := result + inttostr(Num[i]) + Limiter
end;

procedure TForm1.cmdSortClick(Sender: TObject);
begin
  txtNew.Text := '';
  txtNew.Text := NumSort(txtNum.Text, '-');
end;

end.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-22 09:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表