Previous topic | Ada Home Page | Index

Enumeration example



------------------------------------------------------------
What colour is produced when primary colours are mixed?
------------------------------------------------------------

with TEXT_IO; use TEXT_IO;
procedure mix_colours is

  type colour is (red, yellow, blue, green, orange, purple);

  subtype primary_colour is colour range red .. blue;

  package colour_io is new enumeration_io (colour); 
  use colour_io;

  colour1, colour2 : primary_colour;   -- input colours
  colour_mix       : colour;           -- result of mixture

begin -- mix_colours

  PUT ("Enter two of the primary colours ");
  PUT_LINE ("(RED, YELLOW, BLUE)");
  GET (colour1);
  GET (colour2);

  if     ((colour1 = red)  and (colour2 = yellow)) or
         ((colour2 = red)  and (colour1 = yellow)) then
     colour_mix := orange;
  elsif  ((colour1 = red)  and (colour2 = blue)) or
         ((colour2 = red)  and (colour1 = blue)) then
     colour_mix := purple;
  elsif  ((colour1 = blue) and (colour2 = yellow)) or
         ((colour2 = blue) and (colour1 = yellow)) then
     colour_mix := green;
  else -- same colours
     colour_mix := colour1;
  end if;

  PUT ("The colour mixture will be ");
  PUT (colour_mix);
  NEW_LINE;
end mix_colours;



Previous topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 21 Feb 96