-- This example stacks a series of values and their running average
-- It shows the use of two stacks, one for integers and one for floats
with Ada.Text_io; use Ada.Text_io;
with Ada.Integer_Text_io; use Ada.Integer_Text_io;
with Ada.Float_Text_io; use Ada.Float_Text_io;
with StackPkg; -- Make the generic stack package available
procedure avgs is
-- Create the two stack packages that are needed
package intstkpkg is new stackpkg(1000, integer);
package fltstkpkg is new stackpkg(1000, float);
-- package FltStkPkg is new StackPkg(size => 1000, ItemType => Float);
-- Since we created the packages in this procedure,
-- a with statement is not needed
-- A use statement will simplify access to the package members
use IntStkPkg;
use FltStkPkg;
Value : Integer;
Count, Sum : Integer := 0;
Running_Average : Float;
-- Declare the two stack variables
IntStack : IntStkPkg.Stack; -- Must include package name here so that the
FltStack : FltStkPkg.Stack; -- compiler can tell which stack is needed
begin
while not end_of_file loop
get(Value);
Sum := Sum + Value;
Count := Count + 1;
Running_Average := float(Sum) / Float(Count);
IntStkPkg.push(Value, IntStack);
FltStkPkg.push(Running_Average, FltStack);
end loop;
while not IsEmpty(IntStack) loop
put(top(IntStack)); put(" ");
put(top(FltStack)); new_line;
pop(IntStack);
pop(FltStack);
end loop;
end avgs;