鱼C论坛

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

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

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

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

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

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


  1. Project1.zip (497.74 KB, 下载次数: 59)



  2. unit Unit1;

  3. interface

  4. uses
  5. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  6. Dialogs, StdCtrls, Buttons;

  7. type
  8. TForm1 = class(TForm)
  9. edt1: TEdit;
  10. lbl1: TLabel;
  11. btn1: TBitBtn;
  12. lbl2: TLabel;
  13. procedure btn1Click(Sender: TObject);
  14. procedure edt1Change(Sender: TObject);
  15. procedure edt1KeyPress(Sender: TObject; var Key: Char);
  16. procedure FormCreate(Sender: TObject);
  17. private
  18. { Private declarations }
  19. public
  20. { Public declarations }
  21. end;

  22. var
  23. Form1: TForm1;

  24. implementation
  25. var
  26. numarr: array[1..10] of Integer;
  27. times: Integer = 0;
  28. pass: boolean;
  29. {$R *.dfm}

  30. function spacecount(text: string): Integer;
  31. // 计算edit.text里的空格数
  32. var
  33. I,temp: Integer;
  34. begin
  35. temp := 0;
  36. if text <> '' then
  37. for I := 0 to Length(text) do
  38. if text[i] = ' ' then
  39. inc(temp);
  40. Result := temp;
  41. end;




  42. procedure TForm1.btn1Click(Sender: TObject);
  43. var
  44. I,numtemp,temp1,temp2,loopa,loopc,loopd: Integer;
  45. str:string;
  46. begin
  47. if times < 10 then
  48. begin
  49. ShowMessage('少于10个数字!');
  50. Exit;
  51. end;


  52. temp1 := spacecount(edt1.Text);


  53. if (temp1 >= 10) and (edt1.Text[Length(edt1.Text)] <> ' ') then
  54. begin
  55. ShowMessage('多于10个数字!');
  56. Exit;
  57. end;


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


  59. for I := 1 to 10 do
  60. begin
  61. numtemp := Pos(' ',str);
  62. numarr[i] := StrToInt(Copy(str,1,numtemp - 1));
  63. Delete(str,1,numtemp);
  64. end;
  65. //将输入的每个数字放入数组中


  66. for loopa := 1 to 10 do
  67. begin
  68. for loopc := loopa+1 to 10 do
  69. begin
  70. if numarr[loopa] > numarr[loopc] then
  71. begin
  72. temp2 := numarr[loopa];
  73. numarr[loopa] := numarr[loopc];
  74. numarr[loopc] := temp2;
  75. end;
  76. end;
  77. end;

  78. pass := False;
  79. edt1.Clear;


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

  82. pass := True;

  83. end;

  84. procedure TForm1.edt1Change(Sender: TObject);
  85. //每当对edit进行各种操作的时候便动态的显示出已经输入了多少个数
  86. var
  87. temp: Integer;
  88. begin
  89. if pass then
  90. begin

  91. temp := spacecount(edt1.Text);

  92. if (temp = 0) and (edt1.Text <> '') then
  93. temp := 1
  94. else
  95. if (temp = 9) and (edt1.Text[Length(edt1.Text)] <> ' ') then
  96. temp := 10
  97. else
  98. if edt1.Text[Length(edt1.Text)] <> ' ' then
  99. inc(temp);


  100. times := temp;
  101. if times <= 10 then
  102. lbl1.Caption := edt1.Text;
  103. lbl2.Caption := IntToStr(times);
  104. end;
  105. end;




  106. procedure TForm1.edt1KeyPress(Sender: TObject; var Key: Char);
  107. begin
  108. if not (Key in [#48..#57,#8,#32]) then
  109. begin
  110. Key := #0;
  111. Exit;
  112. end;
  113. //只能按 数字键、空格、退格

  114. if Key <> #8 then
  115. begin
  116. if (times >= 10) and (Key = #32) then
  117. begin
  118. ShowMessage('注意!只能输入10个数字!');
  119. Exit;
  120. end
  121. end;
  122. end;





  123. procedure TForm1.FormCreate(Sender: TObject);
  124. begin
  125. pass := True;
  126. end;

  127. end.
复制代码

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-10-12 17:28:28 | 显示全部楼层
相当不错!
小甲鱼最新课程 -> https://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.
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-5-30 17:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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