Conjuntos en Delphi
Un kit de reparación de bicicleta como set of TTool, que se edita y luego se consulta con in.
program SetsMembership;
type
TTool = (TireLever, Pump, ChainTool, Patch, Wrench);
TKit = set of TTool; // a set holds any combination of those values
var
Kit: TKit;
begin
Kit := [TireLever, Patch];
Include(Kit, Pump); // Include adds one value, Exclude removes one
Exclude(Kit, Patch);
Kit := Kit + [Wrench]; // + unions two sets, - subtracts one
if Pump in Kit then // in asks whether a value is a member
WriteLn('can reinflate');
if not (ChainTool in Kit) then
WriteLn('no chain repairs');
end.
Cómo funciona
TKit = set of TTool;construye un tipo conjunto sobre la enumeración de arriba.Include(Kit, Pump)agrega un valor,Exclude(Kit, Patch)quita uno y+une dos conjuntos.if Pump in Kit thenynot (ChainTool in Kit)deciden qué se imprime.
Palabras clave y builtins usados aquí
ExcludeIncludeWriteLnbeginendifinnotofprogramsetthentypevar
El intento, en números
- Líneas
- 16
- Caracteres a escribir
- 520
- Tokens
- 95
- Ritmo de tres estrellas
- 70 tpm
Al ritmo de tres estrellas de 70 tokens por minuto, este intento toma unos 81 segundos.
Paso 2 de 3 en Enums y conjuntos; paso 17 de 27 en Fundamentos del lenguaje.