Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chap-7 Robust_Input. A Package for Robust Input Read input values by calls to procedure Get overloading “Get” procedure for Integer and Float values There.

Similar presentations


Presentation on theme: "Chap-7 Robust_Input. A Package for Robust Input Read input values by calls to procedure Get overloading “Get” procedure for Integer and Float values There."— Presentation transcript:

1 Chap-7 Robust_Input

2 A Package for Robust Input Read input values by calls to procedure Get overloading “Get” procedure for Integer and Float values There are 3 parameters for Get the first one to read item the second one for Min Value range the third one for Max Value range

3 Specification for Robust Input PACKAGE Robust_Input IS -------------------------------------------------------------- --| Package for getting numeric input robustly. --------------------------------------------------------------

4 PROCEDURE Get (Item : OUT Integer; MinVal : IN Integer; MaxVal : IN Integer); -- Gets an integer value in the range MinVal..MaxVal -- Pre: MinVal and MaxVal are defined -- Post: MinVal <= Item <= MaxVal PROCEDURE Get (Item : OUT Float; MinVal : IN Float; MaxVal : IN Float); -- Gets a float value in the range MinVal..MaxVal -- Pre: MinVal and MaxVal are defined -- Post: MinVal <= Item <= MaxVal END Robust_Input;

5 Body of Robust Input PROCEDURE Get (Item : OUT Integer; MinVal : IN Integer; MaxVal : IN Integer) IS Declare SUBTYPE in the RANGE MinVal..MaxVal; Declare local variables BEGIN -- Get END Get;

6 PROCEDURE Get (Item : OUT Integer; MinVal : IN Integer; MaxVal : IN Integer) IS Declare SUBTYPE in the RANGE MinVal..MaxVal; Declare local variables BEGIN -- Get LOOP BEGIN --- Exception Handler Block END -- Exception Handler Block END LOOP; END Get;

7 PROCEDURE Get (Item : OUT Integer; MinVal : IN Integer; MaxVal : IN Integer) IS Declare SUBTYPE in the RANGE MinVal..MaxVal; Declare local variables BEGIN -- Get LOOP BEGIN -- exception handler block Sequence of statements for reading EXIT Exception Handling END; -- exception handler block END LOOP; END Get;

8 PROCEDURE Get (Item : OUT Integer; MinVal : IN Integer; MaxVal : IN Integer) IS Declare SUBTYPE in the RANGE MinVal..MaxVal; Declare local variables BEGIN -- Get LOOP BEGIN -- exception handler block Sequence of statements to read EXIT; -- valid data EXCEPTION -- invalid data WHEN condition => END; -- exception handler block END LOOP; END Get;

9 The Body of Robust_Input WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; WITH Ada.Float_Text_IO; PACKAGE BODY Robust_Input IS -------------------------------------------------------------- --| Body of package for robust numeric input handling -------------------------------------------------------------- PROCEDURE Get (Item : OUT Integer; MinVal : IN Integer; MaxVal : IN Integer) IS SUBTYPE TempType IS Integer RANGE MinVal..MaxVal; TempItem : TempType; -- temporary copy of Item

10 BEGIN -- Get LOOP BEGIN -- exception handler block Ada.Text_IO.Put(Item => "Enter an integer between "); Ada.Integer_Text_IO.Put(Item => MinVal, Width => 0); Ada.Text_IO.Put(Item => " and "); Ada.Integer_Text_IO.Put(Item => MaxVal, Width => 0); Ada.Text_IO.Put(Item => " > "); Ada.Integer_Text_IO.Get(Item => TempItem); Item := TempItem; EXIT; -- valid data

11 EXCEPTION -- invalid data WHEN Constraint_Error => Ada.Text_IO.Put (Item => "Value is out of range. Please try again."); Ada.Text_IO.New_Line; Ada.Text_IO.Skip_Line; WHEN Ada.Text_IO.Data_Error => Ada.Text_IO.Put (Item => "Value is not an integer. Please try again."); Ada.Text_IO.New_Line; Ada.Text_IO.Skip_Line; END; -- exception handler block END LOOP; END Get;

12 PROCEDURE Get (Item : OUT Float; MinVal : IN Float; MaxVal : IN Float) IS SUBTYPE TempType IS Float RANGE MinVal..MaxVal; TempItem : TempType; -- temporary copy of Item

13 BEGIN -- Get LOOP BEGIN -- exception handler block Ada.Text_IO.Put (Item => "Enter a floating-point value between "); Ada.Float_Text_IO.Put (Item => MinVal, Fore=> 1, Aft => 2, Exp => 0); Ada.Text_IO.Put(Item => " and "); Ada.Float_Text_IO.Put (Item => MaxVal, Fore=> 1, Aft => 2, Exp => 0); Ada.Text_IO.Put(Item => " > "); Ada.Float_Text_IO.Get(Item => TempItem); Item := TempItem; EXIT; -- valid data

14 EXCEPTION -- invalid data WHEN Constraint_Error => Ada.Text_IO.Put (Item => "Value is out of range. Please try again."); Ada.Text_IO.New_Line; Ada.Text_IO.Skip_Line; WHEN Ada.Text_IO.Data_Error => Ada.Text_IO.Put (Item => "Value is not floating point. Please try again."); Ada.Text_IO.New_Line; Ada.Text_IO.Skip_Line; END; -- exception handler block END LOOP; END Get; END Robust_Input;

15 A Program that use Robust_Input WITH Robust_Input; PROCEDURE Test_Robust_Input IS -------------------------------------------------------------- --| Demonstrates Robust_Input package -------------------------------------------------------------- SUBTYPE SmallInt IS Integer RANGE -10..10; SUBTYPE LargerInt IS Integer RANGE -100..100; SUBTYPE SmallFloat IS Float RANGE -10.0..10.0; SUBTYPE LargerFloat IS Float RANGE -100.0..100.0;

16 Small : SmallInt; SmallF : SmallFloat; Larger : LargerInt; LargerF : LargerFloat; BEGIN -- Test_Robust_Input Robust_Input.Get(Small,SmallInt'First,SmallInt'Last); Robust_Input.Get(Larger,LargerInt'First,LargerInt'Last); Robust_Input.Get(SmallF,SmallFloat'First,SmallFloat'Last); Robust_Input.Get(LargerF,LargerFloat'First,LargerFloat'Last); END Test_Robust_Input;


Download ppt "Chap-7 Robust_Input. A Package for Robust Input Read input values by calls to procedure Get overloading “Get” procedure for Integer and Float values There."

Similar presentations


Ads by Google