鱼C论坛

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

[作品展示] delphi ”完美"冒泡程序~~~

[复制链接]
发表于 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.
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-10-27 03:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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