[Unified-mailman] CP code for recitation 5

Kristina Lundqvist kristina at MIT.EDU
Mon May 17 17:03:56 EDT 2004


Attached is the code used in Recitation 5.

/Kristina Lundqvist

---
I. Kristina Lundqvist, Ph.D.
Charles S. Draper Assistant Professor of Aeronautics and Astronautics


Massachusetts Institute of Technology
77 Massachusetts Avenue, 33-406
Cambridge, MA 02139


phone: +1 617-452-2550    fax: +1 617-253-7397
kristina at mit.edu

http://www.mit.edu/~kristina
http://esl.mit.edu/index.html




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.mit.edu/pipermail/unified-mailman/attachments/20040517/92c788fc/attachment.htm
-------------- next part --------------
-- Recitation 5
-- demo_infix_evaluation.adb
-- demonstrate generic integer stack 
-- instantiation and usage

with Ada.Text_Io;
with Ada.Integer_Text_Io;
with Ada.Strings;

use Ada.Integer_Text_Io;
use Ada.Text_Io;
use Ada.Strings;
with Stack;

-- assume a FULLY paranthesised expression

procedure demo_infix_evaluation is 

   package Stack_is new Stack(
      Size => 10,     
      Item => Integer);
   
   package Stack_Character is new Stack(
      Size => 10,     
      Item => Character);
      
   My_Expression : String(1..20);
   My_Expression_Length : integer;

   Op1, Op2 : Integer;
   Operand : Character;
   
   function Is_Operand(Op : Character) return Boolean is
   begin
      if Op in '1'..'9' then
         return True;
      else
         return False;
      end if;
   end Is_Operand;
   
   function Is_Operator (Op : Character) return Boolean is
   begin
      if Op = '+' or Op = '-' or Op =  '*' or Op =  '/' then
         return True;
      else
         return False;
      end if;
   end Is_Operator;
   
begin
   Ada.Text_Io.Put("Please enter an expression: ");
   Ada.Text_Io.Get_Line(My_Expression, My_Expression_Length);
   
   for I in 1 .. My_Expression_Length loop
      if Is_Operand(My_Expression(I)) then
         Stack_Integer.Push(Character'Pos(My_Expression(I))-Character'Pos('0'));
         end if;
          if Is_Operator(My_Expression(I)) then
               Stack_Character.Push(My_Expression(I));
         end if;
         
               if My_Expression(I) = ')' then
                  Stack_Integer.Pop(Op2);
                  Stack_Integer.Pop(Op1);
                  Stack_Character.Pop(Operand);
                  case Operand is
                     when '+'=> Stack_Integer.Push(Op1+Op2);
                     when '-'=> Stack_Integer.Push(Op1-Op2);
                     when '*'=> Stack_Integer.Push(Op1*Op2);
            when '/'=> Stack_Integer.Push(Op1/Op2);
            when others => null;
                  end case;
               end if;
   end loop;
   
                 
                     
   Ada.Text_Io.Put("The result is :");
   Stack_Integer.Pop(op1);
      Ada.Text_Io.Put_Line(Integer'Image(op1));

end demo_infix_evaluation;
-------------- next part --------------
-- Recitation 5
-- exception_example_one.adb

with Ada.Text_Io;
use Ada.Text_Io;
with Ada.Float_Text_Io;
use Ada.Float_Text_Io;

package body Exeception_Example_One is

   package Week_Day_Io is new Enumeration_Io (Week_Days);
   use Week_Day_Io;

   procedure Safe_Get_Day (
         Out_Day :    out Week_Days;                    
         Min     : in     Week_Days := Work_Days'First; 
         Max     : in     Week_Days := Work_Days'Last   ) is 

      -- procedure for the safe input of enumeration values 

      Local_Day : Week_Days;          -- local input var     
      Good_Day  : Boolean   := False; -- loop control     


   begin -- safe_get_day 
      while not Good_Day loop
         begin -- while block 
            Put("Enter an day between ");
            Put( Min ); Put(" and "); Put( Max ); Put(" ");
            Get( Local_Day );
            -- this point is reached only when input is a day code 
            if (Local_Day < Min) or (Local_Day > Max) then
               raise Data_Error;
            else
               Good_Day := True;
            end if;
            -- this point is reached if input is a valid day code 
            -- between min and max 
         exception
            when Data_Error =>
               Put_Line("Invalid day!. Good days are ");
               for This_Day in Week_Days range Min .. Max loop
                  Put( This_Day );
                  Put(" ");
               end loop;
               New_Line;
               Skip_Line; -- tidy up terminal 
         end; -- protected while block 
      end loop;
      -- this point can only be reached when valid value input 
      Skip_Line; -- tidy up terminal handling 
      Out_Day := Local_Day; -- export input value 
   end Safe_Get_Day;



   procedure Safe_Get_Float(
         Out_Float :    out Float; 
         Min, Max  : in     Float  ) is
            
      Local_Float : Float;            -- local input var    
      Good_One    : Boolean := False; -- loop control    
         
   begin -- Safe_Get_Float
      while not Good_One loop
         begin -- protected block of code 
            Put("Enter a float in range ");
            Put( Min, Exp => 0 ); Put( " to "); Put( Max, Exp => 0 );
            Put( " ");
            Get( Local_Float );
            -- this point can only be reached if the get 
            -- did not raise the exception 

            -- now tested against limits specified 
            Good_One:=((Local_Float>=Min) and (Local_Float<=Max));

            if not Good_One then
               raise My_Error;  -- Loal_Float < Min OR Local_Float > Max
            end if;
         exception
            when Data_Error =>
               Put_Line("DATA ERROR. Invalid input, pls try again ");
               new_line;
               Skip_Line;
            when My_Error =>
               Put_Line("MY ERROR. Invalid input, pls try again");
               new_line;
               Skip_Line;
               
         end; -- protected block of code 
      end loop;
      -- this point can only be reached when valid value input 
      Skip_Line; -- tidy up terminal handling 

      Out_Float := Local_Float; -- export input value 
   end Safe_Get_Float;

end Exeception_Example_One;
-------------- next part --------------
-- Recitation 5
-- exception_example_two.adb

with Ada.Text_Io;
use Ada.Text_Io;
with Ada.Float_Text_Io;
use Ada.Float_Text_Io;

package body Exeception_Example_Two is

   package Week_Day_Io is new Enumeration_Io (Week_Days);
   use Week_Day_Io;

   procedure Safe_Get_Day (
         Out_Day :    out Week_Days;                    
         Min     : in     Week_Days := Work_Days'First; 
         Max     : in     Week_Days := Work_Days'Last   ) is 

      -- procedure for the safe input of enumeration values 

      Local_Day : Week_Days;          -- local input var     
      Good_Day  : Boolean   := False; -- loop control     


   begin -- safe_get_day 
      while not Good_Day loop
         begin -- while block 
            Put("Enter an day between ");
            Put( Min ); Put(" and "); Put( Max ); Put(" ");
            Get( Local_Day );
            -- this point is reached only when input is a day code 
            if (Local_Day < Min) or (Local_Day > Max) then
               raise Data_Error;
            else
               Good_Day := True;
            end if;
            -- this point is reached if input is a valid day code 
            -- between min and max 
         exception
            when Data_Error =>
               Put_Line("Invalid day!. Good days are ");
               for This_Day in Week_Days range Min .. Max loop
                  Put( This_Day );
                  Put(" ");
               end loop;
               New_Line;
               Skip_Line; -- tidy up terminal 
         end; -- protected while block 
      end loop;
      -- this point can only be reached when valid value input 
      Skip_Line; -- tidy up terminal handling 
      Out_Day := Local_Day; -- export input value 
   end Safe_Get_Day;



   procedure Safe_Get_Float(
         Out_Float :    out Float; 
         Min, Max  : in     Float  ) is
            
      Local_Float : Float;            -- local input var    
      Good_One    : Boolean := False; -- loop control    
         
   begin -- Safe_Get_Float
      while not Good_One loop
         begin -- protected block of code 
            Put("Enter a float in range ");
            Put( Min, Exp => 0 ); Put( " to "); Put( Max, Exp => 0 );
            Put( " ");
            Get( Local_Float );
            -- this point can only be reached if the get 
            -- did not raise the exception 

            -- now tested against limits specified 
            Good_One:=((Local_Float>=Min) and (Local_Float<=Max));

            if not Good_One then
               raise My_Error;  -- Loal_Float < Min OR Local_Float > Max
            end if;
         exception
            when Data_Error =>
               Put_Line("DATA ERROR. Invalid input, pls try again ");
               new_line;
               Skip_Line;
            when My_Error =>
               Put_Line("MY ERROR.");
               new_line;
               Skip_Line;
               raise My_Error;  -- re-raise the exception!
               
         end; -- protected block of code 
      end loop;
      -- this point can only be reached when valid value input 
      Skip_Line; -- tidy up terminal handling 

      Out_Float := Local_Float; -- export input value 
   end Safe_Get_Float;

end Exeception_Example_Two;
-------------- next part --------------
-- Recitation 5
-- stack.adb
-- package body for stack implementation

package body Stack is
   type Table is array (Positive range <>) of Item; 
   Space : Table (1 .. Size);  
   Index : Natural           := 0;  

   procedure Push (E : in     Item ) is 
   begin
      if Index >= Size then
         raise Overflow;
      end if;
      Index := Index + 1;
      Space(Index) := E;
   end Push;

   procedure Pop (E :    out Item ) is 
   begin
      if Index = 0 then
         raise Underflow;
      end if;
      E := Space(Index);
      Index := Index - 1;
   end Pop;
end Stack;



-------------- next part --------------
-- Recitation 5
-- test_example_one.adb

with Exeception_Example_One, Ada.Text_Io;
use Exeception_Example_One, Ada.Text_Io;

procedure Test_Example_One is 
   My_Day : Exeception_Example_One.Week_Days;  
   My_Num : Float;  

begin
   Safe_Get_Day (My_Day);

   New_Line(3);

   Safe_Get_Float(My_Num, 1.0, 19.2);

end Test_Example_One;

-------------- next part --------------
-- Recitation 5
-- test_example_two.adb, tests exception_example_two.ad[bs]

with Ada.Text_Io;
with Exeception_Example_Two;
use Exeception_Example_Two;

procedure Test_Example_Two is 
   My_Day : Exeception_Example_Two.Week_Days;  
   My_Num : Float;  

begin
   Safe_Get_Day (My_Day);

   Ada.Text_Io.New_Line(2);

   Safe_Get_Float(My_Num, 1.0, 19.2);

   Ada.Text_Io.New_Line(2);

   Ada.Text_Io.Put("Will this line be printed if My_Error takes place?");

--exception
  --when My_Error =>
    --Ada.Text_Io.Put_Line("MY ERROR. Now Handled in Calling Program!");
end Test_Example_Two;

-------------- next part --------------
-- Recitation 5
-- exception_example_one.ads
---------------------------------------------
-- package specification to demo exceptions
-------------------------------------------------

with Ada.Exceptions;
use Ada.Exceptions;

package Exeception_Example_One is
   
   My_Error : Exception;

   type Week_Days is  (Sat, Mon, Tue, Wed, Thu, Fri, Sun);
   
   subtype Work_Days is Week_Days range Mon .. Fri;
   
   subtype Weekend_Days is Week_Days range Sat .. Sun;


   procedure Safe_Get_Day (
         Out_Day :    out Week_Days;                    
         Min     : in     Week_Days := Work_Days'First; 
         Max     : in     Week_Days := Work_Days'Last   ); 

   procedure Safe_Get_Float(
         Out_Float :    out Float; 
         Min, Max  : in     Float  ); 

end Exeception_Example_One;
 
    

-------------- next part --------------
-- Recitation 5
-- exception_example_two.ads
---------------------------------------------
-- package specification to demo exceptions
-------------------------------------------------

with Ada.Exceptions;
use Ada.Exceptions;

package Exeception_Example_Two is
   
   My_Error : Exception;

   type Week_Days is  (Sat, Mon, Tue, Wed, Thu, Fri, Sun);
   
   subtype Work_Days is Week_Days range Mon .. Fri;
   
   subtype Weekend_Days is Week_Days range Sat .. Sun;


   procedure Safe_Get_Day (
         Out_Day :    out Week_Days;                    
         Min     : in     Week_Days := Work_Days'First; 
         Max     : in     Week_Days := Work_Days'Last   ); 

   procedure Safe_Get_Float(
         Out_Float :    out Float; 
         Min, Max  : in     Float  ); 

end Exeception_Example_Two;
-------------- next part --------------
-- Recitation 5
-- stack.ads

generic
   Size : Positive;  
   type Item is private; 

package Stack is

      procedure Push (E : in     Item );

      procedure Pop  (E :    out Item ); 

      Overflow, Underflow : exception;

end Stack;


More information about the Unified-mailman mailing list