Skip to content

Instantly share code, notes, and snippets.

@xavierlopezpujol
Created May 25, 2021 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xavierlopezpujol/25c0547bd38fd2a15412a7f1f7858026 to your computer and use it in GitHub Desktop.
Save xavierlopezpujol/25c0547bd38fd2a15412a7f1f7858026 to your computer and use it in GitHub Desktop.
uso de case con rango de floats
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
function IndexRango(const Valor : Float64):integer;
function Rango(const Valor , Min , Max:Float64):boolean;
begin
Result := (Valor>=Min) and (Valor<Max); //función que cumpla el rango
end;
begin
Result := -1;
if Rango(Valor ,0.5,1.5) then Exit(0); //rango1
if Rango(Valor ,1.5,2.5) then Exit(1); //rango2
if Rango(Valor ,2.5,3.5) then Exit(2); //rango3
//...aqui irán todos los rango que se quieran ir añadiendo
//...
end;
function PruebaDelCase(Valor:Float64):string;
begin
case IndexRango(Valor) of
0: Result := 'rango1';
1: Result := 'rango2';
2: Result := 'rango3';
else Result := 'fuera de rango';
end;
end;
begin
Assert(PruebaDelCase(1.01)='rango1');
Assert(PruebaDelCase(2.4)='rango2');
Assert(PruebaDelCase(2.555555)='rango3');
Assert(PruebaDelCase(-1.01)='fuera de rango');
Readln;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment