Ada Programming: Implementing Predecessor and Successor Functions for an Enumeration Type, Exercises of Aeronautical Engineering

An ada code example of implementing predecessor and successor functions for an enumeration type named day. The code includes package specification, implementation, and testing. The day type consists of seven enumerated values: monday through sunday.

Typology: Exercises

2011/2012

Uploaded on 07/20/2012

savitri_122
savitri_122 🇮🇳

4.6

(14)

184 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C9-1
Algorithm
Package Specification
1. Declare the type.
2. Create a new package within the package specification to provide predefined
functions for the enumeration type.
3. Declare the function prototypes for both the predecessor and successor functions.
Package Implementation
1. Successor function:
a. if the input_value = Type’Last then
return Type’First
b. else return Type’Succ(input_value);
2. Predecessor function:
a. if the input_value = Type’First then
return Type’Last
b. else return Type’Pred(input_value);
Code Listing
Checking: c:/docume~2/jk/desktop/16070/codeso~1/my_type_package.ads (source file time stamp: 2003-
09-24 01:59:20)
1. -----------------------------------------------------------
2. -- Package specified to declare the type and two functions
3. -- Specifier : Jayakanth Srinivasan
4. -- Date Last Modified : 09/23/2003
5. -----------------------------------------------------------
6.
7. with Ada.Text_Io;
8.
9. package My_Type_Package is
10.
11. type Day is
12. (Monday,
13. Tuesday,
14. Wednesday,
15. Thursday,
16. Friday,
17. Saturday,
18. Sunday);
docsity.com
pf3

Partial preview of the text

Download Ada Programming: Implementing Predecessor and Successor Functions for an Enumeration Type and more Exercises Aeronautical Engineering in PDF only on Docsity!

C9-

Algorithm

Package Specification

1. Declare the type.

2. Create a new package within the package specification to provide predefined

functions for the enumeration type.

3. Declare the function prototypes for both the predecessor and successor functions.

Package Implementation

1. Successor function:

a. if the input_value = Type’Last then

return Type’First

b. else

return Type’Succ(input_value);

2. Predecessor function:

a. if the input_value = Type’First then

return Type’Last

b. else

return Type’Pred(input_value);

Code Listing

Checking: c:/docume~2/jk/desktop/16070/codeso~1/my_type_package.ads (source file time stamp: 2003- 09-24 01:59:20)


  1. -- Package specified to declare the type and two functions
  2. -- Specifier : Jayakanth Srinivasan
  3. -- Date Last Modified : 09/23/

  4. with Ada.Text_Io;
  5. package My_Type_Package is
  6. type Day is
  7. (Monday,
  8. Tuesday,
  9. Wednesday,
  10. Thursday,
  11. Friday,
  12. Saturday,
  13. Sunday);
  1. package Day_Io is new Ada.Text_Io.Enumeration_Io(Enum => Day);
  2. function Successor (
  3. Day_In : Day )
  4. return Day;
  5. function Predecessor (
  6. Day_In : Day )
  7. return Day;
  8. end My_Type_Package;

28 lines: No errors

Compiling: c:/docume~2/jk/desktop/16070/codeso~1/my_type_package.adb (source file time stamp: 2003- 09-24 02:00:04)


  1. -- Package implementation of My_type package
  2. -- Implementer : Jayakanth Srinivasan
  3. -- Date Last Modified : 09/23/

  4. package body My_Type_Package is
  5. function Successor (
  6. Day_In : Day )
  7. return Day is
  8. begin
  9. if Day_In = Day'Last then
  10. return Day'First;
  11. else
  12. return Day'Succ(Day_In);
  13. end if;
  14. end Successor;
  15. function Predecessor (
  16. Day_In : Day )
  17. return Day is
  18. begin
  19. if Day_In = Day'First then
  20. return Day'Last;
  21. else
  22. return Day'Pred(Day_In);
  23. end if;
  24. end Predecessor;
  25. end My_Type_Package;

32 lines: No errors