310 likes | 379 Views
Chapter 3. The General Structure of Ada Programs. General Form of an Ada Program. With package1; With package2; . . . With packagen; Procedure pname IS - - comments declaration of variables, constants, etc. . . Begin Program statement; . . . Program statement; End;.
E N D
Chapter 3 The General Structure of Ada Programs
General Form of an Ada Program With package1; With package2; . . . With packagen; Procedure pname IS - - comments declaration of variables, constants, etc. . . Begin Program statement; . . . Program statement; End;
With clause • To inform the compiler for a package used in the program • at least one “with” clause in Ada program • package for Text Input and Output • WITH Ada.Text_IO; • package for Integer Text Input and Output • WITH Ada.Integer_Text_IO; • package for Floating Text Input and Output • WITH Ada.Float_Text_IO;
Identifiers or names • Begin with a letter • Consist of only letters, digits, and underscores • Cannot use two or more underscore characters in succession • Cannot use an Ada reserved word or pre-defined identifier as an identifier • Pick meaningful names for identifiers • no restriction on the length of an identifier
Type of data • Character • Integer • Float
Example-1Displaying Initials Program WITH Ada.Text_IO; PROCEDURE Hello_Initials IS ------------------------------------------------------------ --| Requests, then displays, user's first and last initials. --| ------------------------------------------------------------ Initial1 : Character; -- objects that hold initials Initial2 : Character;
Cont. of example-1 BEGIN -- Hello_Initials -- Prompt for (request user to enter) user's initials Ada.Text_IO.Put(Item => "Enter your two initials> "); Ada.Text_IO.Get(Item => Initial1); Ada.Text_IO.Get(Item => Initial2); -- Display user's initials, with a greeting Ada.Text_IO.Put(Item => "Hello"); Ada.Text_IO.Put(Item => Initial1); Ada.Text_IO.Put(Item => Initial2); Ada.Text_IO.Put(Item => ". Enjoy studying Ada!"); Ada.Text_IO.New_Line; END Hello_Initials;
Reserve Words and Identifiers • Reserve Words With Procedure IS Begin END
Predefined Identifiers Ada.Text_IO Put New_Line Character Get
Input and Output • package for Text Input and Output • WITH Ada.Text_IO; • package for Integer Text Input and Output • WITH Ada.Integer_Text_IO; • package for Floating Text Input and Output • WITH Ada.Float_Text_IO;
Read Float Number • To read a float number into identifier “Inches” WITH Ada.Float_Text_IO; Inches : Float; Ada.Float_Text._IO.Get ( Item => Inches);
Read Integer Number • To read an integer number into identifier “Age” WITH Ada.Integer_Text_IO; Age : Integer; Ada.Integer_Text._IO.Get ( Item => Age);
Read Character • To read a character into identifier “Initial1” WITH Ada.Text_IO; Initial1 : Character; Ada.Text._IO.Get ( Item => Initial1);
Write Float Number • To write a float number into identifier “Inches” WITH Ada.Float_Text_IO; Inches : Float; Ada.Float_Text._IO.Put ( Item => Inches);
Integer and Character output WITH Ada.Integer_Text_IO; Age : Integer; Ada.Integer_Text._IO.Put ( Item => Age); WITH Ada.Text_IO; Initial1 : Character; Ada.Text._IO.Put ( Item => Initial1);
Example-2Displaying the User’s Name WITH Ada.Text_IO; PROCEDURE Hello_Name IS ------------------------------------------------------------------------ --| Requests, then displays, user's name --| ------------------------------------------------------------------------ FirstName: String(1..10);-- object to hold user's name
Cont. of example 2 BEGIN -- Hello_Name -- Prompt for (request user to enter) user's name Ada.Text_IO.Put (Item => "Enter your first name, exactly 10 letters."); Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item => "Add spaces at the end if it's shorter.> "); Ada.Text_IO.Get(Item => FirstName); -- Display the entered name, with a greeting Ada.Text_IO.Put(Item => "Hello "); Ada.Text_IO.Put(Item => FirstName); Ada.Text_IO.Put(Item => ". Enjoy studying Ada!"); Ada.Text_IO.New_Line; END Hello_Name;
Constant Object Declaration Form some_constant : CONSTANT type := value; Example Pi : CONSTANT Float := 3.14159;
Assignment Statement Result_Variable := expression; Example X := Y + Z + 2.0; X := X +1;
Example-3Converting Inches to Centimeters WITH Ada.Text_IO; WITH Ada.Float_Text_IO; PROCEDURE Inch_to_CM IS ------------------------------------------------------------------------ --| Converts inches to centimeters ------------------------------------------------------------------------ CMPerInch : CONSTANT Float := 2.54; Inches : Float; Centimeters : Float;
Cont. of example-3 BEGIN -- Inch_to_CM -- Prompt user for value in inches Ada.Text_IO.Put (Item => "Enter a length in inches>"); Ada.Float_Text_IO.Get (Item => Inches); -- Compute equivalent value in centimeters Centimeters := CMPerInch * Inches; -- Display result Ada.Text_IO.Put (Item => "That equals"); Ada.Float_Text_IO.Put (Item => Centimeters); Ada.Text_IO.Put (Item => " centimeters"); Ada.Text_IO.New_Line; END Inch_to_CM;
New Line Procedure Form Ada.Text_IO.New_Line ( Spacing => positive number); Example Ada.Text_IO.New_Line ( Spacing =>3);
Put Procedure ( Integer) with Width Ada.Integer_Text._IO.Put ( Item => variable , Width => field width); Example Ada.Integer_Text._IO.Put ( Item =>How_Long , Width => 5); See page 93 for more examples
Put Procedure ( Float) with Fore, Aft, Exp Ada.Float_Text._IO.Put ( Item => variable , Fore => n, Aft => n, Exp=> n ); Example Ada.Float_Text._IO.Put ( Item =>Centimeter , Fore => 5, Aft,=> 2, Exp => 0); 77.47 rather than 7.74700E+01 See Page 94 for more examples
Data Types and Expressions • Integer • Natural and Positive are subtypes of Integer. REM operation • 3 REM 2 is 1 • 17 REM 3 is 2 • 2 REM 3 is 2
Exponentiation • X ** 2 is 9 ( if X is 3 ) • X ** 3 is 27 ( if X is 3 ) • Y ** 2 is 1.44 ( if Y is 1.2 ) • Y ** 3 is 1.728 ( if Y is 1.2) • Y ** 1.5 is not allowed . The power must be an integer.
Float Literals • A Float literal is a number that begins with a digit contains a decimal point. Example 3.14159 0.0005 1234.0 15.0E-05 -1.2E+6 1.15E-3 2.3E2 But not 150 .1234 12345. 15E-03 12.5E.3 -.123E3
Expression Precedence Rules ( more in chap 8) • Parentheses, multiplication, division, addition and subtraction A. W := Z * X + Y; B. W := Z * ( X + Y) ; if X=1, Y=2 and Z= 3, then A will get the result “5” and B will be “9”.
Example -4 Coin Collection Program WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; PROCEDURE Coin_Collection IS ------------------------------------------------------------------------ --| Finds the value of a coin collection, --| given pennies and nickels ------------------------------------------------------------------------ Pennies : Natural; -- input - number of pennies Nickels : Natural; -- input - number of nickels Dollars : Natural; -- output - value in dollars Cents : Natural; -- output - value in cents TotalCents : Natural;
Cont - 1 for Example 4 BEGIN -- Coin_Collection -- prompt user for number of nickels and pennies Ada.Text_IO.Put (Item => "How many nickels do you have? "); Ada.Integer_Text_IO.Get (Item => Nickels); Ada.Text_IO.Put (Item => "How many pennies do you have? "); Ada.Integer_Text_IO.Get (Item => Pennies); Ada.Text_IO.New_Line; -- compute total value in cents TotalCents := 5 * Nickels + Pennies; -- find the value in dollars and change Dollars := TotalCents / 100; Cents := TotalCents REM 100;
Cont - 2 for Example 4 -- display the value in dollars and change Ada.Text_IO.Put (Item => "Your collection is worth "); Ada.Integer_Text_IO.Put (Item => Dollars, Width => 1); Ada.Text_IO.Put (Item => " dollars and "); Ada.Integer_Text_IO.Put (Item => Cents, Width => 1); Ada.Text_IO.Put (" cents."); Ada.Text_IO.New_Line; END Coin_Collection;