Ordem decrescente por numero...

11/05/2003

0

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


Sparch

Sparch

Responder

Posts

11/05/2003

Adilsond

  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;



Responder

11/05/2003

Adilsond

  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


Responder

11/05/2003

Sparch

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;


Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar