1 / 14

Useful Ada Information

This article provides helpful information and assistance for programming assignments in Ada, including clarifications, handling exceptions, and using user-defined types.

crickey
Télécharger la présentation

Useful Ada Information

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Useful Ada Information Help for programming assignment

  2. Clarifications • You must read and write the moves directly – you may NOT convert them to strings for printing. • You should print out the number of ties as well as the number of computer wins and user wins • You need to handle the situation where the user types in rack or scisssors or piper (for example) • If an exception occurs, you need to handle it and continue until either the user or the computer has won 3 games. • Don’t use use clauses

  3. Declaring a user-defined type (enumeration type) • typetypename is(elementList ); • elementList → element • elementList → element , {elementList} • element can not be numeric • element can not contain a reserved word • element can not have spaces in it

  4. Examples • type car is (ford, chevy, lincoln, bmw); • type city is (rome, naples, bologna); • type die is (1,2,3,4,5,6); • type die is (one, two, three, four, five,six); • type city is (new york, paris); • type city is (old town, paris);

  5. Instantiating an enumeration type • package name_io is new ada.text_io.enumeration_io (enum => type); • instantiation allows reading and writing of enumerated type variables • enum is the formal parameter name • => is called arrow • this example uses named parameter passing • formal parameter name is NOT required

  6. Here’s the Ada specification for the procedure to print an integer Procedure put (item : in Num; width : in Field := Default_Width; base : in Number_Base := Default_base; • The default_base is 10 • The default_width is num’width • Given an integer variable named number • The following are equivalent calls to this procedure Ada.integer_text_io.put (number); Ada.integer_text_io.put (item => number);

  7. Procedure put (continued) • Given an integer variable named number, suppose you want the column width of your output to be 10 and you want to use the default base • The following statements produce the same results • Ada.integer_text_io.put (number, 10); • Ada.integer_text_io.put (item => number, width => 10); • Ada.integer_text_io.put (width => 10, item => number); • Ada.integer_text_io.put (item => number, width => 10, base => 10; • Ada.integer_text_io.put (number, 10, 10); • Ada.integer_text_io.put (base =>10, item => number, width => 10);

  8. Examples • type car is (ford, chevy, lincoln, bmw); • package car_io is new ada.text_io.enumeration_io (enum => car); • type city is (rome, naples, bologna); • package city_io is new ada.text_io.enumeration_io (city);

  9. Examples • type car is (ford, chevy, lincoln, bmw); • package car_io is new ada.text_io.enumeration_io (enum => car); • myCar : car; • car_io.get( myCar); • car_io.put (myCar);

  10. Enumeration types have attributes • typename’pos • typename’val • typename’first • typename’last • The enumeration type’s element list is ordered • The first element is in position 0

  11. Ada Random Number Generator • The information on random number generation can be found in the Language Reference Manual at • http://www.adahome.com/rm95/rm9x-A-05-02.html • Here is an example of code from the above site using Ada’s random number generator • with Ada.Numerics.Discrete_Random; • procedure Dice_Game is • subtype Die is Integer range 1 .. 6; • subtype Dice is Integer range 2*Die'First .. 2*Die'Last; • package Random_Die is new Ada.Numerics.Discrete_Random (Die); • use Random_Die; • G : Generator; • D : Dice; • begin • Reset (G); -- Start the generator in a unique state in each run • loop -- Roll a pair of dice; sum and process the results • D := Random(G) + Random(G); ... • end loop; • end Dice_Game;

  12. Ada subprograms • can be separately compiled • can be separately executed • execution only shows if there is output • ada subprogram specifications • have extension .ads • header ends in a semicolon • Ada subprogram bodies • have extension .adb • header ends in reserved word is

  13. Ada functions • Function body (i.e. Dot_Product.adb) below comes from the language reference manual http://www.adahome.com/rm95/rm9x-06-03.html function Dot_Product(Left, Right : Vector) return Real is Sum : Real := 0.0; begin Check(Left'First = Right'First and Left'Last = Right'Last); for J in Left'Range loop Sum := Sum + Left(J)*Right(J); end loop; return Sum; end Dot_Product;

  14. Ada procedures • subprogram_body ::= subprogram_specification is declarative_part begin handled_sequence_of_statements end [designator]; procedure Push(E : in Element_Type; S : in out Stack) is begin if S.Index = S.Size then raise Stack_Overflow; else S.Index := S.Index + 1; S.Space(S.Index) := E; end if; end Push; above Procedure example can be found at http://www.adahome.com/rm95/rm9x-06-03.html

More Related