chap-7 robust_input. a package for robust input read input values by calls to procedure get...

16
Chap-7 Robust_Input

Upload: jason-jennings

Post on 17-Dec-2015

216 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 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

Chap-7

Robust_Input

Page 2: 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

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

Page 3: 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

Specification for Robust Input

PACKAGE Robust_Input IS-------------------------------------------------------

---------| Package for getting numeric input robustly.-------------------------------------------------------

-------

Page 4: 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

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;

Page 5: 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

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;

Page 6: 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

• 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;

Page 7: 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

• 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;

Page 8: 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

• 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 =>• WHEN condition =>• END; -- exception handler block• END LOOP;• END Get;

Page 9: 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

The Body of Robust_InputWITH 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

Page 10: 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

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

Page 11: 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

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;

Page 12: 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

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

Page 13: 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

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

Page 14: 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

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;

Page 15: 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

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;

Page 16: 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

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;