马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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.
|