我是Ada的新朋友。
我的问题是:
使用ADA编程语言创建
有6个湖泊。每个湖泊都有:
4羽羽毛的天鹅:100、200、300、400和
7只鹅,羽毛数:500、600、700、800、900、1000、1100。
有10位猎人。每个猎人计划猎杀3只天鹅和5只鹅。 每个猎人都在尽可能多的不同湖泊中进行狩猎(来自同一湖泊猎人的第二只鸟只有在他成功或未成功尝试在其他湖泊中狩猎后才进行狩猎)。每次猎人选择从他要寻找的可用鸟中搜寻羽毛最多的鸟。
2位猎人首先寻找天鹅(只有在他们猎杀了所需数量的天鹅之后,或者在湖泊中没有更多天鹅时才寻找鹅),然后有8位猎人首先寻找鹅。 当猎人获得所需数量的天鹅和鹅时,他说他已经猎取了羽毛总数。尚未获得所需羽毛数量的猎人说,他缺少多少只天鹅和多少只鹅。
说明:在ADA中将猎人和湖泊都描述为TASK阵列;如果猎人x想要从y湖中猎取一只天鹅,猎人会呼叫从其接收同步过程中接收到的羽毛数量的湖泊,或者如果湖中没有任何天鹅或鹅,他会被拒绝。必须通过在标准输出上打印输出数据来公开输出数据。
所有猎人必须同时完成其工作。程序中的所有任务和程序本身都必须退出工作。禁止使用时间控制来确保程序停止运行。
关于猎人的活动,他们捕猎的鸟类以及每个湖中剩余的天鹅或鹅的信息,严禁将其保留在全局变量或狩猎或湖外的任何其他结构中。仅在消息交换的基础上,在主应用程序和任务簿之间交换信息;任务不使用对在其外部定义的变量的访问。
首先,我想了解如何定义规则。我目前的解决方案是:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Birds is
type Hunter (Swans, Geese);
type Swans(100,200,300,400) of Integer;
type Geese(500,600,700,800,900,1000,1100) of Integer;
Hunter_Number_Of_Birds : INTEGER := 0;
Lake_Number_Of_birds_In_Lake : INTEGER := 66;
-- The main program adds and deletes birds to hunters and from the lakes.
task Hunter is
entry Hunts_Swans(dates: in Integer); -- This adds swans to hunter
entry Hunts_Geese(dates: in Integer); -- This adds geese to hunter
end Hunter;
task Lake is
entry Swans(dates: in Integer); -- This deletes swans from lake
entry Geese(dates: in Integer); -- This deletes geese from lake
end Lake;
task body Hunter is
begin
for Index in 1..8 loop
select
when Hunter_Number_Of_Birds =>8
accept Hunts_Swans do
Hunter_Number_Of_Birds := Hunter_Number_Of_Birds + 1;
Put_line("Hunter adds swans, count =");
Put(Hunter_Number_Of_Birds);
New_Line;
end Hunts_Swans;
or
when Hunter_Number_Of_Birds =>8
accept Hunts_Geese do
Hunter_Number_Of_Birds := Hunter_Number_Of_Birds + 1;
Put_Line("Hunter adds geese, count =");
Put(Hunter_Number_Of_Birds);
end Hunts_Geese;
end select;
end loop;
end Hunter;
task body Lake is
begin
for Index in 1..5 loop
select
when Lake_Number_Of_birds_In_Lake =>66
accept Swans do
Lake_Number_Of_birds_In_Lake := Lake_Number_Of_birds_In_Lake - 1;
Put_line("Hunter hunt swans, remaining number of swans in the lake =");
Put(Lake_Number_Of_birds_In_Lake);
New_Line;
end Swans;
or
when Lake_Number_Of_birds_In_Lake =>66
accept Geese do
Lake_Number_Of_birds_In_Lake:= Lake_Number_Of_birds_In_Lake - 1;
Put_line("Hunter hunt geese, remaining number of geese in the lake =");
Put(Lake_Number_Of_birds_In_Lake);
end Geese;
end select;
end loop;
end Lake;
我不知道如何定义:
答案 0 :(得分:1)
Number of hunters and lakes;
如注释中所述,将任务声明更改为任务类型:
task type Hunter is
entry Hunts_Swans(dates: in Integer); -- This adds swans to hunter
entry Hunts_Geese(dates: in Integer); -- This adds geese to hunter
end Hunter;
task type Lake is
entry Swans(dates: in Integer); -- This deletes swans from lake
entry Geese(dates: in Integer); -- This deletes geese from lake
end Lake;
然后,您只需要将它们声明为数组:
type Hunter_Array is array(1..10) of Hunter;
type Lake_Array is array(1..6) of Lake;
Hunters : Hunter_Array;
Lakes : Lake_Array;
number of feathers of each beards;
在这里,我将创建一个Bird包并使用Ada.Containers.Bounded_Vectors:
package Birds is
type Bird is (Swan,Goose);
type Feather_Count is new Natural;
end Birds;
use type Birds.Feather_Count;
package Vectors is new Ada.Containers.Bounded_Vectors
(Index_Type => Positive,
Element_Type => Birds.Feather_Count);
use type Vectors.Vector;
Swans : aliased Vectors.Vector
:= Vectors.To_Vector(100,1) & 200 & 300 & 400;
Geese : aliased Vectors.Vector
:= Vectors.To_Vector(500,1) & 600 & 700 & 800 & 900 & 1000 & 1100;
使用向量可让您跟踪留在湖中的鸟类数量(例如,您可以称呼天鹅(Swans.Length))。当被狩猎时,您可以通过称呼来获得羽毛
Feathers := Swans.Last_Element;
Swans.Delete_Last; -- Takes it out of the vector
本质上是将向量用作堆栈。
2 hunters first look for swans and 8 hunters first look for geese;
为此,我将您的Hunter任务定义更改为:
task type Hunter(Prefers : Birds.Bird := Birds.Swan) is
entry Hunts_Swans(dates: in Integer); -- This adds swans to hunter
entry Hunts_Geese(dates: in Integer); -- This adds geese to hunter
end Hunter;
并为其添加构造函数:
function Swan_Hunter return Hunter is
begin
return Result : Hunter(Birds.Swan);
end Swan_Hunter;
function Goose_Hunter return Hunter is
begin
return Result : Hunter(Birds.Goose);
end Goose_Hunter;
然后,您可以将Hunter数组声明更改为:
Hunters : Hunter_Array :=
(1 => Swan_Hunter,
2 => Swan_Hunter,
others => Goose_Hunter);
此后,您只需要添加逻辑以跟踪他们当前是否在猎取鹅或天鹅,并将其与他们的偏好进行比较以了解如何进行。
count the feathers
当您从湖中狩猎时,让条目也返回羽毛的数量。然后,只要每次成功就可以将它们加在一起,让猎人为自己保持连续运行。