Ordem decrescente por numero...
Como posso fazer para testar a ordem de 3 numeros (quem eh maior que quem, e menor que quem) e coloca-los em ordem do maior para o menor em 3 TLabels.
Abaixo, como estou obtendo os numeros, agora, tenho que coloca-los em ordem decrescente.
var n1, n2, n3, nm, nt : integer;
begin
n1 := 0;
n2 := 0;
n3 := 0;
nm := 0;
nt := 0;
if (Trim(EditN1.Text) <> ´´) and (Trim(EditN2.Text) <> ´´)
and (Trim(EditN3.Text) <> ´´) then
begin
n1 := StrToInt(EditN1.Text);
n2 := StrToInt(EditN2.Text);
n3 := StrToInt(EditN3.Text);
nm := (n1 + n2 + n3) div 3;
nt := n1 + n2 + n3;
EditM.Text := IntToStr(nm);
EditT.Text := IntToStr(nt);
end
Esta funcionando tudo OK, agora preciso coloca-los em ordem descrescente, o n1 caso seja maior que n2 e n3 e assim por diante, tem como fazer testando um a um, mas isso gera muito codigo e trabalho, tem como fazer isso usando poucas linha de codigo?
----
Daniel
Abaixo, como estou obtendo os numeros, agora, tenho que coloca-los em ordem decrescente.
var n1, n2, n3, nm, nt : integer;
begin
n1 := 0;
n2 := 0;
n3 := 0;
nm := 0;
nt := 0;
if (Trim(EditN1.Text) <> ´´) and (Trim(EditN2.Text) <> ´´)
and (Trim(EditN3.Text) <> ´´) then
begin
n1 := StrToInt(EditN1.Text);
n2 := StrToInt(EditN2.Text);
n3 := StrToInt(EditN3.Text);
nm := (n1 + n2 + n3) div 3;
nt := n1 + n2 + n3;
EditM.Text := IntToStr(nm);
EditT.Text := IntToStr(nt);
end
Esta funcionando tudo OK, agora preciso coloca-los em ordem descrescente, o n1 caso seja maior que n2 e n3 e assim por diante, tem como fazer testando um a um, mas isso gera muito codigo e trabalho, tem como fazer isso usando poucas linha de codigo?
----
Daniel
Sparch
Curtidas 0
Respostas
Adilsond
11/05/2003
private { Private declarations } function Ordena(Value: array of TEdit): Boolean; public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} function TForm1.Ordena(Value: array of TEdit): Boolean; var I: Word; S: String; begin Result := False; for I := Low(Value) to High(Value) - 1 do begin if StrToInt(Value[I].Text) > StrToInt(Value[I + 1].Text) then begin S := Value[I + 1].Text; Value[I + 1].Text := Value[I].Text; Value[I].Text := S; Result := True; end; end; end; procedure TForm1.Button1Click(Sender: TObject); var n1, n2, n3, nm, nt: Integer; begin n1 := 0; n2 := 0; n3 := 0; nm := 0; nt := 0; if (Trim(EditN1.Text) <> ´´) and (Trim(EditN2.Text) <> ´´) and (Trim(EditN3.Text) <> ´´) then begin n1 := StrToInt(EditN1.Text); n2 := StrToInt(EditN2.Text); n3 := StrToInt(EditN3.Text); nm := (n1 + n2 + n3) div 3; nt := n1 + n2 + n3; EditM.Text := IntToStr(nm); EditT.Text := IntToStr(nt); while Ordena([EditN1,EditN2,EditN3]) do begin Continue; end; end; end;
GOSTEI 0
Adilsond
11/05/2003
private { Private declarations } function Ordena(Value: array of TEdit): Boolean; public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} function TForm1.Ordena(Value: array of TEdit): Boolean; var I: Word; S: String; begin Result := False; for I := Low(Value) to High(Value) - 1 do begin if StrToInt(Value[I].Text) > StrToInt(Value[I + 1].Text) then begin S := Value[I + 1].Text; Value[I + 1].Text := Value[I].Text; Value[I].Text := S; Result := True; end; end; end; procedure TForm1.Button1Click(Sender: TObject); var n1, n2, n3, nm, nt: Integer; begin n1 := 0; n2 := 0; n3 := 0; nm := 0; nt := 0; if (Trim(EditN1.Text) <> ´´) and (Trim(EditN2.Text) <> ´´) and (Trim(EditN3.Text) <> ´´) then begin n1 := StrToInt(EditN1.Text); n2 := StrToInt(EditN2.Text); n3 := StrToInt(EditN3.Text); nm := (n1 + n2 + n3) div 3; nt := n1 + n2 + n3; EditM.Text := IntToStr(nm); EditT.Text := IntToStr(nt); while Ordena([EditN1,EditN2,EditN3]) do begin Continue; end; end; end;
em ordem decrescente paramu a seuinte linha:
if StrToInt(Value[I].Text) > StrToInt(Value[I + 1].Text) then
para
if StrToInt(Value[I].Text) < StrToInt(Value[I + 1].Text) then
GOSTEI 0
Sparch
11/05/2003
Obrigado, era isso mesmo o que eu queria... olhe como ficou agora..
public
{ Public declarations }
function Ordena(Value: array of TLabel): Boolean;
function TForm1.Ordena(Value: array of TLabel): Boolean;
var
I: Word;
S: String;
begin
Result := False;
for I := Low(Value) to High(Value) - 1 do
begin
if StrToInt(Value[I].Caption) < StrToInt(Value[I + 1].Caption) then
begin
S := Value[I + 1].Caption;
Value[I + 1].Caption := Value[I].Caption;
Value[I].Caption := S;
Result := True;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var n1, n2, n3, nm, nt : integer;
begin
n1 := 0;
n2 := 0;
n3 := 0;
nm := 0;
nt := 0;
if (Trim(EditN1.Text) <> ´´) and
(Trim(EditN2.Text) <> ´´) and
(Trim(EditN3.Text) <> ´´) then
begin
n1 := StrToInt(EditN1.Text);
n2 := StrToInt(EditN2.Text);
n3 := StrToInt(EditN3.Text);
nm := (n1 + n2 + n3) div 3;
nt := n1 + n2 + n3;
EditM.Text := IntToStr(nm);
EditT.Text := IntToStr(nt);
LabelMaior.Caption := IntToStr(n1);
LabelMeio.Caption := IntToStr(n2);
LabelMenor.Caption := IntToStr(n3);
while Ordena([LabelMaior,LabelMeio,LabelMenor]) do
begin
Continue;
end;
end
else
begin
MessageDlg(´Preencha os números 1 a 3 por favor.´, mtInformation, [mbOK], 0);
end;
end;
public
{ Public declarations }
function Ordena(Value: array of TLabel): Boolean;
function TForm1.Ordena(Value: array of TLabel): Boolean;
var
I: Word;
S: String;
begin
Result := False;
for I := Low(Value) to High(Value) - 1 do
begin
if StrToInt(Value[I].Caption) < StrToInt(Value[I + 1].Caption) then
begin
S := Value[I + 1].Caption;
Value[I + 1].Caption := Value[I].Caption;
Value[I].Caption := S;
Result := True;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var n1, n2, n3, nm, nt : integer;
begin
n1 := 0;
n2 := 0;
n3 := 0;
nm := 0;
nt := 0;
if (Trim(EditN1.Text) <> ´´) and
(Trim(EditN2.Text) <> ´´) and
(Trim(EditN3.Text) <> ´´) then
begin
n1 := StrToInt(EditN1.Text);
n2 := StrToInt(EditN2.Text);
n3 := StrToInt(EditN3.Text);
nm := (n1 + n2 + n3) div 3;
nt := n1 + n2 + n3;
EditM.Text := IntToStr(nm);
EditT.Text := IntToStr(nt);
LabelMaior.Caption := IntToStr(n1);
LabelMeio.Caption := IntToStr(n2);
LabelMenor.Caption := IntToStr(n3);
while Ordena([LabelMaior,LabelMeio,LabelMenor]) do
begin
Continue;
end;
end
else
begin
MessageDlg(´Preencha os números 1 a 3 por favor.´, mtInformation, [mbOK], 0);
end;
end;
GOSTEI 0