# Board index » delphi » Algorithm

## Algorithm

In article <01bbb796.b385fa20\$2acaffc8@agape>
Thiago Conde S. Figueiro wrote:

##### Quote

>    Could any of you offer me some help? I'm writting a smallproggie in
>Pascal and I want to add a feature on it's database that doessomething
>like this:

>1 - Green
>2 - Blue
>4 - Yellow
>8 - Black
>16 - White

>    This way, when the program finds the number 20 on thedatabase, it will
>figure out that it means 16 + 4 = White + Yellow. Another eg: 7 -> 1 +2 +
>4 = Green + Blue + Yellow.

I assume from your example that all "colors" are a power of two,
therefore 4 could never be anything other than Yellow - in other
words it could never be considered as green + green + blue,
blue + blue, etc.

If the above is correct then you only have to harness the properties
of binary numbers.  The explanation could be quite long, so let's
look at an example instead -

CONST  MAX_COLOR = 5;
Color: Array[1..MAX_BITS] of String[6]
= ( 'Green', 'Blue', 'Yellow', 'Black', 'White');

FUNCTION ColorString(Number: Word): String;
CONST Plus: Array[Boolean] of String[3] = ('', ' + ');
VAR s: String;
i: Integer;
BEGIN
i := 0;
s := '';
While (Number <> 0) Do Begin
If i < MAX_COLOR Then Begin
Inc(i);
If Odd(Number) Then
s := s + Plus[s <> ''] + Color[i];
End Else
Runerror(201); { - number is too large - }
Number := Number shr 1;
End;
ColorString := s;
END;

You'll need 12 colors to cover your planned 1 to 1024 range, but if
you set MAX_COLOR to 16 and extend the Color array accordingly you
can omit the test to insure that Number is w/in range since you would
then have the full word range covered.

If you want to come from the other end and place the highest color
first then add a new word variable (maybe Mask?), initialize it to
\$8000 and replace "If Odd(Number)" with "If Number and Mask <> 0"
and replace "Number := Number shr 1" with "Mask := Mask shr 1;"

There is more that can be done, but the above should be enough to
get you started.

...red

## Re:Algorithm

Could any of you offer me some help? I'm writting a small proggie in
Pascal and I want to add a feature on it's database that does something
like this:

1 - Green
2 - Blue
4 - Yellow
8 - Black
16 - White

This way, when the program finds the number 20 on the database, it will
figure out that it means 16 + 4 = White + Yellow. Another eg: 7 -> 1 + 2 +
4 = Green + Blue + Yellow.

until 1024, and that mean a lot of combinations. And a good algorithm. :)

Any help welcome to my email: rha...@hexanet.com.br

## Re:Algorithm

"Thiago Conde S. Figueiro" <rha...@hexanet.com.br> wrote:

##### Quote
>    Could any of you offer me some help? I'm writting a small proggie in
>Pascal and I want to add a feature on it's database that does something
>like this:
>1 - Green
>2 - Blue
>4 - Yellow
>8 - Black
>16 - White
>    This way, when the program finds the number 20 on the database, it will
>figure out that it means 16 + 4 = White + Yellow. Another eg: 7 -> 1 + 2 +
>4 = Green + Blue + Yellow.
>    If this looks simple for any of you, then please help me. I plan to write
>until 1024, and that mean a lot of combinations. And a good algorithm. :)

yeah... here's one way of doing it:

const
numcolors = 5; { number of colors in the database }
colors : array[0..numcolors - 1] of string = ('Green', 'Blue',
'Yellow', 'Black', 'White); { names of the colors }

procedure PrintColor (color : byte);

var
i : byte;

begin
for i := 0 to numcolors - 1 do  { for each color in the database...}
if boolean(color and (1 shl i)) then  { if the bit corresponding
to that color is set...}
writeln (colors[i]);  { write the color's name }
end;

hope this works for you...

the basic premise behind this is that you AND the number with the
bitmask of the color you're checking... if the result <> 0 (a boolean
typecast of a non-zero integer is true) then the color matches.

##### Quote
>    Any help welcome to my email: rha...@hexanet.com.br

posted and cc:ed via email

##### Quote

---
quantum porcupine, coder, musician        |   that which is, is not
and porcupine.  mailto:jsha...@nmsu.edu     |  that which can, can not
http://infinity.beve.blacksburg.va.us/~porcpine | that which does, does not

## Re:Algorithm

##### Quote
>>        Could any of you offer me some help? I'm writting a smallproggie in
>>Pascal and I want to add a feature on it's database that doessomething
>>like this:

Easy way in {I know this works in borland, may not be standard pascal}:

Type
Color = (Blue, Green, Red, HiInt, BgBlue, BgGreen, BgRed, Blink);
Colorbyte = set of color;
Const
firstcolor = Blue;  { or color(0) }
lastcolor = Blink;  { or color(7) }
names : array[color] of string[20] = ('Blue', 'Green', 'Red',
'High Intensity', '<bg> Blue', '<bg> Green', '<bg> Red',

var
g: colorbyte;
c: color;

begin
byte(g) := \$55;
for c := firstcolor to lastcolor do if c in g then write(names[c],' ');
end;
--

##### Quote
>:-P

Sam Vilain, diony...@sans.vuw.ac.nz
...And on the seventh day, He connected to the WWW and did nothing...