• 微信公众号：美女很有趣。 工作之余，放松一下，关注即送10G+美女照片！

2周前 (04-08) 10次浏览

``` -- Chapter 16 - Program 4
with DynStrng; use DynStrng;

procedure TryStrng is

Try_This : STRING(1..13);
Name     : DYNAMIC_STRING(0..15);
Stuff    : DYNAMIC_STRING(0..35);
Result   : BOOLEAN;
Neat     : constant STRING := "XYZ";
Good3    : STRING(1..3);
Good4    : STRING(1..4);
Column   : INTEGER;

begin

Name(0) := CHARACTER'VAL(3);
Stuff(0) := CHARACTER'VAL(7);

Put(Size_Of(Name));
Put(Size_Of(Stuff));
Put(Length(Name));
Put(Length(Stuff));
New_Line;

Try_This := "ABCDEFGHIJKL\$";
Copy(Try_This,Stuff,Result);
Put(Size_Of(Stuff));
Put(Length(Stuff));
Put(Stuff); Put(Stuff);
New_Line(2);

Copy(Stuff,Name,Result);
Put(Name); Put(Name); Put(Name); New_Line;

Concat(Name,Name,Stuff,Result);
Put(Stuff); New_Line;

Delete(Stuff,5,3,Result);
Put(Stuff); New_Line;
Delete(Stuff,6,3,Result);
Put(Stuff); New_Line;
Delete(Stuff,6,3,Result);
Put(Stuff); New_Line;
Delete(Stuff,6,3,Result);
Put(Stuff); New_Line;
Delete(Stuff,6,3,Result);
Put(Stuff); New_Line;
Delete(Stuff,6,3,Result);
Put(Stuff); New_Line;
Delete(Stuff,6,3,Result);
Put(Stuff); New_Line;
Delete(Stuff,6,3,Result);
Put(Stuff); New_Line(2);

Try_This := "1234567890123";
Copy(Try_This,Stuff,Result);
Copy(Neat,Name,Result);
Put(Stuff); Put(Name); New_Line;

Insert(Stuff,Name,5,Result);
Put(Stuff); New_Line;
Insert(Stuff,Name,50,Result);
Put(Stuff); New_Line;
Insert(Stuff,Name,2,Result);
Put(Stuff); New_Line;
Insert(Stuff,Name,24,Result);
Put(Stuff); New_Line;
Insert(Stuff,Name,5,Result);
Put(Stuff); New_Line;
Insert(Stuff,Name,5,Result);
Put(Stuff); New_Line;
Insert(Stuff,Name,5,Result);
Put(Stuff); New_Line;
Insert(Stuff,Name,5,Result);
Put(Stuff); New_Line(2);

Good3 := "123";
Try_This := "1234567890123";
Copy(Try_This,Stuff,Result);
Copy(Good3,Name,Result);
Pos(Stuff,Name,1,Column,Result);
Ada.Text_IO.Put("Found in column number"); Put(Column); New_Line;
Pos(Stuff,Name,2,Column,Result);
Ada.Text_IO.Put("Found in column number"); Put(Column); New_Line;
Pos(Stuff,Name,7,Column,Result);
Ada.Text_IO.Put("Found in column number"); Put(Column); New_Line;
Pos(Stuff,Name,12,Column,Result);
Ada.Text_IO.Put("Found in column number"); Put(Column); New_Line;
Pos(Stuff,Name,18,Column,Result);
Ada.Text_IO.Put("Found in column number"); Put(Column); New_Line;
Pos(Stuff,Name,50,Column,Result);
Ada.Text_IO.Put("Found in column number"); Put(Column); New_Line;

end TryStrng;```

`  Copy("Line of text.", Stuff, Result);`

`   Copy(STRING'("Line of text."), Stuff, Result);`

``` -- Chapter 16 - Program 5
-- This program will calculate the number of days old you are.
-- It is a rather dumb program, but illustrates some interesting
-- programming techniques.  It checks all input to see that they
-- are in the correct range before continuing.  Since the number
-- of days can easily exceed the limits of type INTEGER, and we
-- cannot count on LONG_INTEGER being available, a fixed point
-- variable is used for the total number of days since Jan 1, 1880.
-- This program also passes a record to a procedure, where it is
-- modified and returned.

procedure Age is

LOW_YEAR    : constant := 1880;
MAX         : constant := 365.0 * (2100 - LOW_YEAR);
type AGES is delta 1.0 range -MAX..MAX;
Present_Age : AGES;

use Fix_IO;

type DATE is record
Month : INTEGER range 1..12;
Day   : INTEGER range 1..31;
Year  : INTEGER range LOW_YEAR..2100;
Days  : AGES;
end record;

Today       : DATE;
Birth_Day   : DATE;

procedure Get_Date(Date_To_Get : in out DATE) is
Temp : INTEGER;
begin
Put(" month --> ");
loop
Get(Temp);
if Temp in 1..12 then
Date_To_Get.Month := Temp;
exit;                       -- month OK
else
Put_Line(" Month must be in the range of 1 to 12");
Put("                    ");
Put(" month --> ");
end if;
end loop;

Put("                    ");
Put(" day ----> ");
loop
Get(Temp);
if Temp in 1..31 then
Date_To_Get.Day := Temp;
exit;                       -- day OK
else
Put_Line(" Day must be in the range of 1 to 31");
Put("                    ");
Put(" day ----> ");
end if;
end loop;

Put("                    ");
Put(" year ---> ");
loop
Get(Temp);
if Temp in LOW_YEAR..2100 then
Date_To_Get.Year := Temp;
exit;                       -- year OK
else
Put_Line(" Year must be in the range of 1880 to 2100");
Put("                    ");
Put(" year ---> ");
end if;
end loop;
Date_To_Get.Days := 365 * AGES(Date_To_Get.Year - LOW_YEAR)
+ AGES(31 * Date_To_Get.Month + Date_To_Get.Day);

end Get_Date;

begin
Put("Enter Today's date; ");
Get_Date(Today);
New_Line;

Get_Date(Birth_Day);
New_Line(2);

Present_Age := Today.Days - Birth_Day.Days;
if Present_Age < 0.0 then
Put("You will be born in ");
Present_Age := abs(Present_Age);
Put(Present_Age, 6, 0, 0);
Put_Line(" days.");
elsif Present_Age = 0.0 then
Put_Line("Happy birthday, you were just born today.");
else
Put("You are now ");
Put(Present_Age, 6, 0, 0);
Put_Line(" days old.");
end if;

end Age;```

—————————————————————————————————————————