Skip to content

Instantly share code, notes, and snippets.

@xavierlopezpujol
Created May 25, 2021
Embed
What would you like to do?
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