Hallo ,
Code: Alles auswählen
Beitragvon Socke » 11. Sep 2018, 15:32 Re: Gibt es in FPC eine IntegerList?
Um Rundungsfehler zu vermeiden muss der Nachkommateil ebenfalls beachtet werden.
Ich würde die Zahlen hier einfach vergleichen:
Code:
if Item1 < Item2 then
Result := -1
else if Item1 > Item2 then
Result := 1
else // Item 1 = Item2
Result := 0;
Danke , Super , Einfach , Universell.
Habe alles noch mal überarbeitet.
Für die beiden Vergleichsoperationen nach Auf- und Absteigend sortieren habe ich zwei Funktionen mit Varianten-Input gebaut.
Ob das nachher Nachteile in Sachen Geschwindigkeit beim Sortieren gibt weis ich jetzt nicht , dafür sind hier im Beispiel zu wenige Listen-Einträge vorhanden.
Code: Alles auswählen
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
fgl;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
public
end;
T_Int64List = specialize TFPGList<int64>;
T_ExtendedList = specialize TFPGList<extended>;
var
Form1: TForm1;
i64_list:T_Int64List;
extended_list:T_ExtendedList;
// Up Down int64-Sortier-Funktionen
function SortMyList_int64_up(const Item1, Item2: int64):longint;
function SortMyList_int64_down(const Item1, Item2: int64):longint;
// Up Down extended-Sortier-Funktionen
function SortMyList_extended_up(const Item1, Item2: extended):longint;
function SortMyList_extended_down(const Item1, Item2: extended):longint;
// Vergleich per Variant , spart Code , universeller , Geschwindigkeit ??
function _up(const Item1, Item2:variant):shortint;
function _down(const Item1, Item2:variant):shortint;
implementation
{$R *.lfm}
{ TForm1 }
// Up Down - Test
function _up(const Item1, Item2:variant):shortint;
begin
if Item1 < Item2
then Result := -1
else if Item1 > Item2
then Result := 1
else Result := 0; // Item 1 = Item2
end;
function _down(const Item1, Item2:variant):shortint;
begin
if Item1 > Item2
then Result := -1
else if Item1 < Item2
then Result := 1
else Result := 0; // Item 1 = Item2
end;
// Int64 Sort-Funktionen
function SortMyList_int64_up(const Item1, Item2: int64):longint;
begin
result:=_up(item1,item2); // Test per Variant-Übergabe
end;
function SortMyList_int64_down(const Item1, Item2: int64):longint;
begin
result:=_down(item1,item2); // Test per Variant-Übergabe
end;
// Extended Sort-Funktionen
function SortMyList_extended_up(const Item1, Item2: extended):longint;
begin
result:=_up(item1,item2); // Test per Variant-Übergabe
end;
function SortMyList_extended_down(const Item1, Item2: extended):longint;
begin
result:=_down(item1,item2); // Test per Variant-Übergabe
end;
// Int64 - Beispiel
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
iz:int64;
begin
i64_list:=T_Int64List.Create;
i64_list.Clear;
i64_list.Add(5);
i64_list.Add(82898797);
i64_list.Add(995);
i64_list.Add(115);
i64_list.Add(445);
i64_list.Add(577777);
i64_list.Add(55555);
Memo1.Clear;
memo1.lines.add('Orginal Eingabedaten');
for i:=0 to i64_list.Count-1 do
begin
iz:=i64_list[i];
memo1.lines.add(floattostr(iz));
end;
if radiobutton1.Checked
then i64_list.Sort(@SortMyList_int64_up);
if radiobutton2.Checked
then i64_list.Sort(@SortMyList_int64_down);
memo1.lines.add('');
memo1.lines.add('Sortiert');
for i:=0 to i64_list.Count-1 do
begin
iz:=i64_list[i];
memo1.lines.add(floattostr(iz));
end;
i64_list.free;
end;
// Extended - Beispiel
procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
ez:extended;
begin
extended_list:=T_ExtendedList.Create;
extended_list.Clear;
extended_list.Add(5.33);
extended_list.Add(82898797.01);
extended_list.Add(995.99000);
extended_list.Add(995.98999);
extended_list.Add(445.45);
extended_list.Add(577777.001);
extended_list.Add(55555.999);
Memo1.Clear;
memo1.lines.add('Orginal Eingabedaten');
for i:=0 to extended_list.Count-1 do
begin
ez:=extended_list[i];
memo1.lines.add(floattostr(ez));
end;
if radiobutton1.Checked
then extended_list.Sort(@SortMyList_extended_up);
if radiobutton2.Checked
then extended_list.Sort(@SortMyList_extended_down);
memo1.lines.add('');
memo1.lines.add('Sortiert');
for i:=0 to extended_list.Count-1 do
begin
ez:=extended_list[i];
memo1.lines.add(floattostr(ez));
end;
extended_list.free;
end;
end.
Gruß
Frank