My First Ada Program

Sep 10, 2008 04:04

Just finished writing my first Ada program for Programming Languages class. I also took some extra time to add an easter egg.


-----------------------------------------------------------------------------
-- FILE : homework01.adb
-- PROGRAMMER : Jeffrey Gee
-- LAST REVISION : 2008-09-09
-- SUBJECT : This program counts the number of times a character is
-- used from the input and then displays the ten most used
-- characters along with the count.
--
--
-- TEST VARIABLES: abcdefgabca, abcdefgabcaGEE, GaEE, GEaE, GEEa, GEEGEE, gee
-----------------------------------------------------------------------------

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Homework01 is
Letter : Character;
Letter_Integer_Value : Integer;
Temp : Integer;
Temp2 : Character;
Easter_Egg : Integer;
Activate_Easter_Egg : Integer;
type Character_Count_Record is record
Ch : Character;
Count : Natural;
end record;
subtype Array_Index is Natural range 0 .. 255;
type Alphabet_Array is array(Array_Index) of Character_Count_Record;
Count : Alphabet_Array;
Letter_Count : Integer := 0;

-- Bubble sort to place the most used letter last.
procedure Bubble_Sort(Alphabet_Count_Array : in out Alphabet_Array) is
begin
for I in 0 .. 255 loop
for I in 0 .. 254 loop
if Alphabet_Count_Array(I).Count > Alphabet_Count_Array(I+1).Count then
-- Swaps the count value.
Temp := Alphabet_Count_Array(I+1).Count;
Alphabet_Count_Array(I+1).Count := Alphabet_Count_Array(I).Count;
Alphabet_Count_Array(I).Count := Temp;

-- Swaps the letter value.
Temp2 := Alphabet_Count_Array(I+1).Ch;
Alphabet_Count_Array(I+1).Ch := Alphabet_Count_Array(I).Ch;
Alphabet_Count_Array(I).Ch := Temp2;
end if;
end loop;
end loop;
end Bubble_Sort;

begin
-- Initializes all characters separately.
for I in 0 .. 255 loop
Count(I).Ch := Character'Val(I);

-- Initializes all counts to zero.
Count(I).Count := 0;
end loop;

-- Initializes Easter Egg.
Easter_Egg := 0;
Activate_Easter_Egg := 0;

-- Gets all characters from input.
while not End_Of_File loop
Get(Letter);
-- Converts retrieved letter to its integer value.
Letter_Integer_Value := Character'POS(Letter);

-- Increments the specified letter by one.
Count(Letter_Integer_Value).Count := Count(Letter_Integer_Value).Count + 1;

-- Easter Egg - It is activated by type GEE
if Letter = 'G' then
Easter_Egg := Easter_Egg + 1;
elsif Letter = 'E' and Easter_Egg = 1 then
Easter_Egg := 2;
elsif Letter = 'E' and Easter_Egg = 2 then
Activate_Easter_Egg := 1;
else
Easter_Egg := 0;
end if;

end loop;

-- Calls Bubble_Sort procedure and returns the results when finished.
Bubble_Sort(Count);

-- Clears the screen before display results.
New_Line(24); -- Ada.Text_IO.New_Page; doesn't work.

-- Prints out program title.
Put("*** Alphabet-Count-Ta-Nator 1.0 ***"); New_Line;

-- Prints out programmer credits.
Put(" By Jeffrey Gee"); New_Line; New_Line; New_Line;

-- Prints a description out.
Put("Top Ten Most Used Characters"); New_Line; New_Line;

-- Prints out results of the ten most used characters.
for I in 246 .. 255 loop
Put(Count(I).Ch); Put(" = "); Put(Count(I).Count); New_Line;
end loop;

-- Prints results if user activates Easter Egg.
if Activate_Easter_Egg = 1 then
New_Line; New_Line; Put("EASTER EGG: The Secret to Ada is: If you take the time to learn it, it's fun!"); New_Line; New_Line;
end if;

-- Adds some vertical spacing to make it more aesthetically pleasing.
New_Line; New_Line; New_Line;
end Homework01;

Previous post Next post
Up